Skip to content

feat: add IsPalindrome snippet with unit test and README entry #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
21 changes: 21 additions & 0 deletions bin/main/codecollection/algorithms/BubbleSort.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package codecollection.algorithms

fun bubbleSort(list: List<Int>): List<Int> {
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()
}
17 changes: 17 additions & 0 deletions bin/main/codecollection/algorithms/InsertionSort.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package codecollection.algorithms

fun insertionSort(list: List<Int>): List<Int> {
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
}
30 changes: 30 additions & 0 deletions bin/main/codecollection/algorithms/MergeSort.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package codecollection.algorithms

fun mergeSort(list: List<Int>): List<Int> {
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<Int>, right: List<Int>): List<Int> {
val result = mutableListOf<Int>()
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
}
25 changes: 25 additions & 0 deletions bin/main/codecollection/algorithms/README.md
Original file line number Diff line number Diff line change
@@ -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)!
Empty file.
Empty file.
Empty file.
31 changes: 31 additions & 0 deletions bin/main/codecollection/snippets/README.md
Original file line number Diff line number Diff line change
@@ -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!
6 changes: 6 additions & 0 deletions bin/main/codecollection/snippets/numbers/DigitCount.kt
Original file line number Diff line number Diff line change
@@ -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
}
15 changes: 15 additions & 0 deletions bin/main/codecollection/snippets/numbers/README.md
Original file line number Diff line number Diff line change
@@ -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..._ | |

Original file line number Diff line number Diff line change
@@ -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("")
6 changes: 6 additions & 0 deletions bin/main/codecollection/snippets/strings/IsPalindrome.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package codecollection.snippets.strings

fun isPalindrome(input: String): Boolean {
val filtered = input.filter { it.isLetterOrDigit() }.lowercase()
return filtered == filtered.reversed()
}
17 changes: 17 additions & 0 deletions bin/main/codecollection/snippets/strings/README.md
Original file line number Diff line number Diff line change
@@ -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..._ | |

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package codecollection.snippets.strings

fun String.replaceMultipleSpaces(): String =
this.replace(Regex("\\s+"), " ").trim()
35 changes: 35 additions & 0 deletions bin/test/codecollection/algorithms/BubbleSortTest.kt
Original file line number Diff line number Diff line change
@@ -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<Int>()
val expected = emptyList<Int>()
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))
}
}
63 changes: 63 additions & 0 deletions bin/test/codecollection/algorithms/InsertionSortTest.kt
Original file line number Diff line number Diff line change
@@ -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<Int>()
val expected = emptyList<Int>()
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))
}
}
35 changes: 35 additions & 0 deletions bin/test/codecollection/algorithms/MergeSortTest.kt
Original file line number Diff line number Diff line change
@@ -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<Int>()
val expected = emptyList<Int>()
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))
}
}
28 changes: 28 additions & 0 deletions bin/test/codecollection/snippets/numbers/DigitCountTest.kt
Original file line number Diff line number Diff line change
@@ -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<IllegalArgumentException> {
digitCount(-7)
}
}
}
27 changes: 27 additions & 0 deletions bin/test/codecollection/snippets/strings/ConvertToCamelCaseTest.kt
Original file line number Diff line number Diff line change
@@ -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())
}
}
Loading