diff --git a/bin/main/codecollection/algorithms/.gitkeep b/bin/main/codecollection/algorithms/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/bin/main/codecollection/algorithms/BubbleSort.kt b/bin/main/codecollection/algorithms/BubbleSort.kt new file mode 100644 index 0000000..ca071b6 --- /dev/null +++ b/bin/main/codecollection/algorithms/BubbleSort.kt @@ -0,0 +1,21 @@ +package codecollection.algorithms + +fun bubbleSort(list: List): List { + if (list.size <= 1) return list + + val result = list.toMutableList() + val lastIndex = result.lastIndex + + repeat(result.size - 1) { i -> + var isSwapped = false + for (j in 0 until lastIndex - i) { + if (result[j] > result[j + 1]) { + result[j] = result[j + 1].also { result[j + 1] = result[j] } + isSwapped = true + } + } + if (!isSwapped) return result.toList() + } + + return result.toList() +} diff --git a/bin/main/codecollection/algorithms/InsertionSort.kt b/bin/main/codecollection/algorithms/InsertionSort.kt new file mode 100644 index 0000000..91d8431 --- /dev/null +++ b/bin/main/codecollection/algorithms/InsertionSort.kt @@ -0,0 +1,17 @@ +package codecollection.algorithms + +fun insertionSort(list: List): List { + if (list.size <= 1) return list + val result = list.toMutableList() + for (i in 1 until result.size) { + val key = result[i] + var j = i - 1 + while (j >= 0 && result[j] > key) { + result[j + 1] = result[j] + j-- + } + result[j + 1] = key + } + + return result +} diff --git a/bin/main/codecollection/algorithms/MergeSort.kt b/bin/main/codecollection/algorithms/MergeSort.kt new file mode 100644 index 0000000..277343d --- /dev/null +++ b/bin/main/codecollection/algorithms/MergeSort.kt @@ -0,0 +1,30 @@ +package codecollection.algorithms + +fun mergeSort(list: List): List { + if (list.size <= 1) return list + + val middle = list.size / 2 + val left = mergeSort(list.subList(0, middle)) + val right = mergeSort(list.subList(middle, list.size)) + + return merge(left, right) +} + +private fun merge(left: List, right: List): List { + val result = mutableListOf() + var i = 0 + var j = 0 + + while (i < left.size && j < right.size) { + if (left[i] <= right[j]) { + result.add(left[i++]) + } else { + result.add(right[j++]) + } + } + + while (i < left.size) result.add(left[i++]) + while (j < right.size) result.add(right[j++]) + + return result +} diff --git a/bin/main/codecollection/algorithms/README.md b/bin/main/codecollection/algorithms/README.md new file mode 100644 index 0000000..52b0384 --- /dev/null +++ b/bin/main/codecollection/algorithms/README.md @@ -0,0 +1,25 @@ +# 🧠 Algorithms in Kotlin + +This package contains classic and practical algorithms written in **idiomatic Kotlin**. + +Each algorithm is implemented in its own file and is tested with a corresponding unit test - keeping the code clean, minimal, and easy to understand. + +Whether you're learning, referencing, or contributing - this collection is for you. + +--- + +## 🗂️ Available Algorithms + +| Algorithm Name | Description | File | +| -------------- | ---------------------------- | -------------------------------------- | +| Merge Sort | Divide-and-conquer sorting | [`MergeSort.kt`](MergeSort.kt) | +| Insertion Sort | Simple comparison-based sort | [`InsertionSort.kt`](InsertionSort.kt) | +| Bubble Sort | Repeated adjacent-swap sorting | [`BubbleSort.kt`](BubbleSort.kt) | +| _...more coming soon_ | + +--- + +## 🙌 Want to Help? + +- Check out the [issues labeled `type: algorithm`](https://github.com/e5LA/kotlin-code-collection/issues?q=label%3A%22type%3A%20algorithm%22%20state%3Aopen) +- Or submit your own idea as new [Issue](https://github.com/e5LA/kotlin-code-collection/issues/new)! diff --git a/bin/main/codecollection/kotlinfeatures/.gitkeep b/bin/main/codecollection/kotlinfeatures/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/bin/main/codecollection/patterns/.gitkeep b/bin/main/codecollection/patterns/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/bin/main/codecollection/snippets/.gitkeep b/bin/main/codecollection/snippets/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/bin/main/codecollection/snippets/README.md b/bin/main/codecollection/snippets/README.md new file mode 100644 index 0000000..1d824f3 --- /dev/null +++ b/bin/main/codecollection/snippets/README.md @@ -0,0 +1,31 @@ +# ⚡ Kotlin Code Snippets + +This package contains a collection of practical, reusable, and idiomatic Kotlin snippets. + +Each snippet solves a focused task - from string manipulation to working with numbers, collections, dates, or the system. + +Snippets are organized by category, and each is implemented in a self-contained `.kt` file with corresponding tests. + +--- + +## 🗂️ Categories + +| Category | Description | +|---------------|-----------------------------------------| +| `strings/` | String operations and transformations | +| `numbers/` | Math, number theory, formatting | +| `collections/`| List, set, and map utilities | +| `datetime/` | Working with dates and times | +| `fileio/` | Read/write operations with files | +| `system/` | Environment, OS, and runtime info | +| `conversion/` | Type and format conversions | +| `regex/` | Useful regular expression utilities | +| `random/` | Random generation, sampling | + + +## 🤝 Want to Contribute? + +- Pick an [open issue labeled `type: snippet`](https://github.com/e5LA/kotlin-code-collection/issues?q=is%3Aissue%20state%3Aopen%20label%3A%22type%3A%20snippet%22) and add your snippet under the right subpackage +- Or suggest a new one via [Issues](https://github.com/e5LA/kotlin-code-collection/issues/new) + +See [CONTRIBUTING.md](../CONTRIBUTING.md) for more! diff --git a/bin/main/codecollection/snippets/numbers/DigitCount.kt b/bin/main/codecollection/snippets/numbers/DigitCount.kt new file mode 100644 index 0000000..9e4fb60 --- /dev/null +++ b/bin/main/codecollection/snippets/numbers/DigitCount.kt @@ -0,0 +1,6 @@ +package codecollection.snippets.numbers + +fun digitCount(n: Int): Int { + require(n >= 0) { "Input must be non-negative" } + return if (n == 0) 1 else kotlin.math.log10(n.toDouble()).toInt() + 1 +} diff --git a/bin/main/codecollection/snippets/numbers/README.md b/bin/main/codecollection/snippets/numbers/README.md new file mode 100644 index 0000000..744c602 --- /dev/null +++ b/bin/main/codecollection/snippets/numbers/README.md @@ -0,0 +1,15 @@ +# 🔢 Kotlin Numbers Snippets + +This package contains simple, reusable Kotlin functions for working with numbers. + +Each snippet is implemented as an extension function or utility method, and tested. + +--- + +## ✨ Available Snippets + +| File | Description | +|----------------------------------|---------------------------------------| +| [`DigitCount.kt`](DigitCount.kt) | Returns the number of digits in an integer | +| _More coming soon..._ | | + diff --git a/bin/main/codecollection/snippets/strings/ConvertToCamelCase.kt b/bin/main/codecollection/snippets/strings/ConvertToCamelCase.kt new file mode 100644 index 0000000..0535880 --- /dev/null +++ b/bin/main/codecollection/snippets/strings/ConvertToCamelCase.kt @@ -0,0 +1,9 @@ +package codecollection.snippets.strings + +fun String.toCamelCase(): String = + split(" ", "_", "-") + .filter { it.isNotBlank() } + .mapIndexed { index, word -> + val lower = word.lowercase() + if (index == 0) lower else lower.replaceFirstChar { it.uppercaseChar() } + }.joinToString("") diff --git a/bin/main/codecollection/snippets/strings/IsPalindrome.kt b/bin/main/codecollection/snippets/strings/IsPalindrome.kt new file mode 100644 index 0000000..4a03bdf --- /dev/null +++ b/bin/main/codecollection/snippets/strings/IsPalindrome.kt @@ -0,0 +1,6 @@ +package codecollection.snippets.strings + +fun isPalindrome(input: String): Boolean { + val filtered = input.filter { it.isLetterOrDigit() }.lowercase() + return filtered == filtered.reversed() +} diff --git a/bin/main/codecollection/snippets/strings/README.md b/bin/main/codecollection/snippets/strings/README.md new file mode 100644 index 0000000..af14f85 --- /dev/null +++ b/bin/main/codecollection/snippets/strings/README.md @@ -0,0 +1,17 @@ +# 🔤 Kotlin String Snippets + +This package contains Kotlin snippets for common string operations - simple, idiomatic, and reusable. + +Each snippet is implemented as an extension function or utility method, and tested. + +--- + +## ✨ Available Snippets + +| File | Description | +|--------------------------------------------------|---------------------------------------| +| [`ConvertToCamelCase.kt`](ConvertToCamelCase.kt) | Converts strings to camelCase format | +| [`ReplaceMultipleSpaces.kt`](ReplaceMultipleSpaces.kt) | Replaces consecutive whitespace with a single space | +| [`IsPalindrome.kt`](IsPalindrome.kt) | Checks whether the string is a palindrome or not| +| _More coming soon..._ | | + diff --git a/bin/main/codecollection/snippets/strings/ReplaceMultipleSpaces.kt b/bin/main/codecollection/snippets/strings/ReplaceMultipleSpaces.kt new file mode 100644 index 0000000..8d0828f --- /dev/null +++ b/bin/main/codecollection/snippets/strings/ReplaceMultipleSpaces.kt @@ -0,0 +1,4 @@ +package codecollection.snippets.strings + +fun String.replaceMultipleSpaces(): String = + this.replace(Regex("\\s+"), " ").trim() diff --git a/bin/test/codecollection/algorithms/BubbleSortTest.kt b/bin/test/codecollection/algorithms/BubbleSortTest.kt new file mode 100644 index 0000000..ee3e285 --- /dev/null +++ b/bin/test/codecollection/algorithms/BubbleSortTest.kt @@ -0,0 +1,35 @@ +package codecollection.algorithms + +import kotlin.test.Test +import kotlin.test.assertEquals + +class BubbleSortTest { + + @Test + fun `sorts an unsorted list`() { + val input = listOf(5, 2, 9, 1, 3) + val expected = listOf(1, 2, 3, 5, 9) + assertEquals(expected, bubbleSort(input)) + } + + @Test + fun `returns the same list when already sorted`() { + val input = listOf(1, 2, 3, 4) + val expected = listOf(1, 2, 3, 4) + assertEquals(expected, bubbleSort(input)) + } + + @Test + fun `returns empty list when input is empty`() { + val input = emptyList() + val expected = emptyList() + assertEquals(expected, bubbleSort(input)) + } + + @Test + fun `sorts list with duplicates`() { + val input = listOf(3, 1, 2, 1, 3) + val expected = listOf(1, 1, 2, 3, 3) + assertEquals(expected, bubbleSort(input)) + } +} diff --git a/bin/test/codecollection/algorithms/InsertionSortTest.kt b/bin/test/codecollection/algorithms/InsertionSortTest.kt new file mode 100644 index 0000000..b7b87c0 --- /dev/null +++ b/bin/test/codecollection/algorithms/InsertionSortTest.kt @@ -0,0 +1,63 @@ +package codecollection.algorithms + +import kotlin.test.Test +import kotlin.test.assertEquals + +class InsertionSortTest { + + @Test + fun `sorts an unsorted list`() { + val input = listOf(5, 2, 9, 1, 3) + val expected = listOf(1, 2, 3, 5, 9) + assertEquals(expected, insertionSort(input)) + } + + @Test + fun `returns the same list when already sorted`() { + val input = listOf(1, 2, 3, 4) + val expected = listOf(1, 2, 3, 4) + assertEquals(expected, insertionSort(input)) + } + + @Test + fun `returns empty list when input is empty`() { + val input = emptyList() + val expected = emptyList() + assertEquals(expected, insertionSort(input)) + } + + @Test + fun `sorts list with duplicates`() { + val input = listOf(3, 1, 2, 1, 3) + val expected = listOf(1, 1, 2, 3, 3) + assertEquals(expected, insertionSort(input)) + } + + @Test + fun `sorts single element list`() { + val input = listOf(42) + val expected = listOf(42) + assertEquals(expected, insertionSort(input)) + } + + @Test + fun `sorts reverse sorted list`() { + val input = listOf(5, 4, 3, 2, 1) + val expected = listOf(1, 2, 3, 4, 5) + assertEquals(expected, insertionSort(input)) + } + + @Test + fun `sorts list with negative numbers`() { + val input = listOf(-3, 1, -2, 5, 0) + val expected = listOf(-3, -2, 0, 1, 5) + assertEquals(expected, insertionSort(input)) + } + + @Test + fun `sorts list with two elements`() { + val input = listOf(2, 1) + val expected = listOf(1, 2) + assertEquals(expected, insertionSort(input)) + } +} diff --git a/bin/test/codecollection/algorithms/MergeSortTest.kt b/bin/test/codecollection/algorithms/MergeSortTest.kt new file mode 100644 index 0000000..5f7c14b --- /dev/null +++ b/bin/test/codecollection/algorithms/MergeSortTest.kt @@ -0,0 +1,35 @@ +package codecollection.algorithms + +import kotlin.test.Test +import kotlin.test.assertEquals + +class MergeSortTest { + + @Test + fun `sorts an unsorted list`() { + val input = listOf(5, 2, 9, 1, 3) + val expected = listOf(1, 2, 3, 5, 9) + assertEquals(expected, mergeSort(input)) + } + + @Test + fun `returns the same list when already sorted`() { + val input = listOf(1, 2, 3, 4) + val expected = listOf(1, 2, 3, 4) + assertEquals(expected, mergeSort(input)) + } + + @Test + fun `returns empty list when input is empty`() { + val input = emptyList() + val expected = emptyList() + assertEquals(expected, mergeSort(input)) + } + + @Test + fun `sorts list with duplicates`() { + val input = listOf(3, 1, 2, 1, 3) + val expected = listOf(1, 1, 2, 3, 3) + assertEquals(expected, mergeSort(input)) + } +} diff --git a/bin/test/codecollection/snippets/numbers/DigitCountTest.kt b/bin/test/codecollection/snippets/numbers/DigitCountTest.kt new file mode 100644 index 0000000..dd3eeba --- /dev/null +++ b/bin/test/codecollection/snippets/numbers/DigitCountTest.kt @@ -0,0 +1,28 @@ +package codecollection.snippets.numbers + +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFailsWith + +class DigitCountTest { + + @Test + fun `returns correct digit count for positive numbers`() { + assertEquals(1, digitCount(5)) + assertEquals(2, digitCount(42)) + assertEquals(3, digitCount(999)) + assertEquals(6, digitCount(123456)) + } + + @Test + fun `returns 1 for zero`() { + assertEquals(1, digitCount(0)) + } + + @Test + fun `throws for negative numbers`() { + assertFailsWith { + digitCount(-7) + } + } +} diff --git a/bin/test/codecollection/snippets/strings/ConvertToCamelCaseTest.kt b/bin/test/codecollection/snippets/strings/ConvertToCamelCaseTest.kt new file mode 100644 index 0000000..5ae806b --- /dev/null +++ b/bin/test/codecollection/snippets/strings/ConvertToCamelCaseTest.kt @@ -0,0 +1,27 @@ +package codecollection.snippets.strings + +import kotlin.test.Test +import kotlin.test.assertEquals + +class ConvertToCamelCaseTest { + + @Test + fun `converts space-separated to camelCase`() { + assertEquals("helloWorld", "Hello world".toCamelCase()) + } + + @Test + fun `converts underscore-separated to camelCase`() { + assertEquals("myVariableName", "my_variable_name".toCamelCase()) + } + + @Test + fun `converts dash-separated to camelCase`() { + assertEquals("convertToCamelCase", "convert-to-camel-case".toCamelCase()) + } + + @Test + fun `trims and ignores extra separators`() { + assertEquals("multipleWordsHere", " multiple___words--here ".toCamelCase()) + } +} diff --git a/bin/test/codecollection/snippets/strings/IsPalindromeTest.kt b/bin/test/codecollection/snippets/strings/IsPalindromeTest.kt new file mode 100644 index 0000000..bd19622 --- /dev/null +++ b/bin/test/codecollection/snippets/strings/IsPalindromeTest.kt @@ -0,0 +1,23 @@ +package codecollection.snippets.strings + +import kotlin.test.Test +import kotlin.test.assertFalse +import kotlin.test.assertTrue + +class IsPalindromeTest { + + @Test + fun `recognizes simple word palindrome`() { + assertTrue(isPalindrome("madam")) + } + + @Test + fun `recognizes palindrome with punctuation and spaces`() { + assertTrue(isPalindrome("A man, a plan, a canal: Panama")) + } + + @Test + fun `recognizes non-palindrome`() { + assertFalse(isPalindrome("hello")) + } +} diff --git a/bin/test/codecollection/snippets/strings/ReplaceMultipleSpacesTest.kt b/bin/test/codecollection/snippets/strings/ReplaceMultipleSpacesTest.kt new file mode 100644 index 0000000..7de10f3 --- /dev/null +++ b/bin/test/codecollection/snippets/strings/ReplaceMultipleSpacesTest.kt @@ -0,0 +1,31 @@ +package codecollection.snippets.strings + +import kotlin.test.Test +import kotlin.test.assertEquals + +class ReplaceMultipleSpacesTest { + + @Test + fun `replaces multiple spaces with one`() { + val input = "This is a test" + val expected = "This is a test" + assertEquals(expected, input.replaceMultipleSpaces()) + } + + @Test + fun `handles leading and trailing spaces`() { + val input = " Kotlin is awesome " + val expected = "Kotlin is awesome" + assertEquals(expected, input.replaceMultipleSpaces()) + } + + @Test + fun `returns empty string when only spaces`() { + assertEquals("", " ".replaceMultipleSpaces()) + } + + @Test + fun `handles empty string`() { + assertEquals("", "".replaceMultipleSpaces()) + } +} diff --git a/src/main/kotlin/codecollection/snippets/strings/IsPalindrome.kt b/src/main/kotlin/codecollection/snippets/strings/IsPalindrome.kt new file mode 100644 index 0000000..4a03bdf --- /dev/null +++ b/src/main/kotlin/codecollection/snippets/strings/IsPalindrome.kt @@ -0,0 +1,6 @@ +package codecollection.snippets.strings + +fun isPalindrome(input: String): Boolean { + val filtered = input.filter { it.isLetterOrDigit() }.lowercase() + return filtered == filtered.reversed() +} diff --git a/src/main/kotlin/codecollection/snippets/strings/README.md b/src/main/kotlin/codecollection/snippets/strings/README.md index 5409c96..af14f85 100644 --- a/src/main/kotlin/codecollection/snippets/strings/README.md +++ b/src/main/kotlin/codecollection/snippets/strings/README.md @@ -12,5 +12,6 @@ Each snippet is implemented as an extension function or utility method, and test |--------------------------------------------------|---------------------------------------| | [`ConvertToCamelCase.kt`](ConvertToCamelCase.kt) | Converts strings to camelCase format | | [`ReplaceMultipleSpaces.kt`](ReplaceMultipleSpaces.kt) | Replaces consecutive whitespace with a single space | +| [`IsPalindrome.kt`](IsPalindrome.kt) | Checks whether the string is a palindrome or not| | _More coming soon..._ | | diff --git a/src/test/kotlin/codecollection/snippets/strings/IsPalindromeTest.kt b/src/test/kotlin/codecollection/snippets/strings/IsPalindromeTest.kt new file mode 100644 index 0000000..bd19622 --- /dev/null +++ b/src/test/kotlin/codecollection/snippets/strings/IsPalindromeTest.kt @@ -0,0 +1,23 @@ +package codecollection.snippets.strings + +import kotlin.test.Test +import kotlin.test.assertFalse +import kotlin.test.assertTrue + +class IsPalindromeTest { + + @Test + fun `recognizes simple word palindrome`() { + assertTrue(isPalindrome("madam")) + } + + @Test + fun `recognizes palindrome with punctuation and spaces`() { + assertTrue(isPalindrome("A man, a plan, a canal: Panama")) + } + + @Test + fun `recognizes non-palindrome`() { + assertFalse(isPalindrome("hello")) + } +}