Skip to content

Commit 9aee12a

Browse files
author
evitwilly
committed
added count sort algorithm, edited some comments in different files, added Kotlin variant for Command pattern
1 parent 3725759 commit 9aee12a

File tree

18 files changed

+106
-3
lines changed

18 files changed

+106
-3
lines changed
Binary file not shown.
Binary file not shown.

.gradle/7.1/fileHashes/fileHashes.bin

700 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
Binary file not shown.

src/main/kotlin/design_patterns/Builder.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ package design_patterns
1010

1111

1212
/**
13-
* first variant
13+
*
14+
* The first variant
1415
*
1516
*/
1617

@@ -62,7 +63,8 @@ class Pony1 {
6263

6364
/**
6465
*
65-
* the second variant
66+
* The second variant
67+
*
6668
*/
6769

6870
class Pony2 {
@@ -106,6 +108,7 @@ class Pony2 {
106108
/**
107109
*
108110
* Kotlin variant with default arguments
111+
*
109112
*/
110113

111114
class Pony3(

src/main/kotlin/design_patterns/Command.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ package design_patterns
44
* pattern: Command
55
*
66
* description: it's a behavioral pattern that allows you to wrap requests or simple operations in separate objects.
7+
*
8+
* P.S. Kotlin variant of this pattern is shown in tests
79
*/
810

911
interface ArithmeticCommand {

src/main/kotlin/design_patterns/Decorator.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package design_patterns
77
*
88
* description: classes implement a common interface and to extend the functionality of the previous object,
99
* the old object is passed through the constructor
10+
*
1011
*/
1112

1213
interface MyPrinter {

src/main/kotlin/design_patterns/Dependency Injection.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package design_patterns
66
* using: used when we have classes that depend on others
77
*
88
* description: all dependencies (classes that ours depends on) are passed through the constructor
9+
*
910
*/
1011

1112
class NewsApiService {

src/main/kotlin/design_patterns/Facade.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package design_patterns
66
* using: used to simplify access to an object with a complex implementation
77
*
88
* description: a complex object contains several dependencies within itself, which it combines with each other
9+
*
910
*/
1011

1112

src/main/kotlin/design_patterns/Fluent Interface Pattern.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package design_patterns
77
*
88
* description: the object has special methods that change it and return it
99
* with a new state for further manipulations
10+
*
1011
*/
1112

1213
class View {

src/main/kotlin/design_patterns/Observer.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package design_patterns
77
*
88
* description: the object communicates its changes to observers
99
* who previously subscribed to its changes
10+
*
1011
*/
1112

1213
fun interface Observer {

src/main/kotlin/design_patterns/Singleton.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package design_patterns
77
* the entire execution of our program
88
*
99
* description: class allows you to create only a single object
10+
*
1011
*/
1112

1213
object LocalData {

src/main/kotlin/design_patterns/Strategy.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package design_patterns
44
* pattern: Strategy
55
*
66
* using: used when we need to change the behavior of an object
7+
*
78
*/
89

910
interface ExchangeStrategy {

src/main/kotlin/design_patterns/Visitor.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package design_patterns
55
*
66
* description: it's a behavioral pattern that allows you to add a new operation
77
* to an entire class hierarchy without changing the code of these classes.
8+
*
89
*/
910

1011
interface PonyVisitor {

src/main/kotlin/sorting/CountSort.kt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package sorting
2+
3+
/**
4+
* count sort
5+
*
6+
* worst time: n
7+
* the best time: n
8+
* average time: n
9+
*
10+
* amount of memory: equals to the size of the range of numbers plus 1 (for example: 1001 for numbers from 0 to 1000)
11+
*
12+
* The use of counting sort is useful only when the sorted numbers have (or can be mapped to) a range of possible
13+
* values that is small enough compared to the sorted set, for example, a million natural numbers less than 1000
14+
*
15+
*/
16+
17+
fun Array<Int>.countSort(start: Int, end: Int) { // sorts numbers in the range from start to end
18+
val countedNumbers = Array(end + 1) { 0 }
19+
20+
var index = 0
21+
while (index < size) {
22+
countedNumbers[this[index]]++
23+
index++
24+
}
25+
26+
index = 0
27+
var currentNumber = start
28+
while (currentNumber < countedNumbers.size) {
29+
var frequency = countedNumbers[currentNumber]
30+
while (frequency > 0) {
31+
this[index++] = currentNumber
32+
frequency--
33+
}
34+
currentNumber++
35+
}
36+
}

src/test/kotlin/design_patterns/Command.kt renamed to src/test/kotlin/design_patterns/CommandTest.kt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package design_patterns
33
import org.junit.Test
44
import org.junit.jupiter.api.Assertions
55

6-
internal class Command {
6+
internal class CommandTest {
77

88
@Test
99
fun test_1() {
@@ -57,4 +57,20 @@ internal class Command {
5757
Assertions.assertEquals(2000, actual)
5858
}
5959

60+
@Test
61+
fun test_kotlin_variant() {
62+
val commands: List<(Int) -> Int> = listOf(
63+
{ actual: Int -> actual + 49 },
64+
{ actual: Int -> actual - 20 },
65+
{ actual: Int -> actual * 6 }
66+
)
67+
68+
var actual = 1
69+
commands.forEach { command ->
70+
actual = command.invoke(actual)
71+
}
72+
73+
Assertions.assertEquals(180, actual)
74+
}
75+
6076
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package sorting
2+
3+
import org.junit.Test
4+
import org.junit.jupiter.api.Assertions
5+
6+
class CountSortTest {
7+
8+
@Test
9+
fun test_numbers_from_zero_to_five() {
10+
val numbers = arrayOf(5, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 3, 4, 3, 4, 3, 3, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 5, 3, 2, 2, 1, 1, 0)
11+
val expected = numbers.sorted()
12+
13+
numbers.countSort(0, 5)
14+
15+
Assertions.assertEquals(expected, numbers.toList())
16+
}
17+
18+
@Test
19+
fun test_numbers_from_one_to_ten() {
20+
val numbers = arrayOf(9, 9, 9, 10, 10, 5, 4, 4, 4, 1, 1, 1, 3, 3, 3)
21+
val expected = numbers.sorted()
22+
23+
numbers.countSort(1, 10)
24+
25+
Assertions.assertEquals(expected, numbers.toList())
26+
}
27+
28+
@Test
29+
fun test_numbers_from_one_to_thousand() {
30+
val numbers = arrayOf(1000, 1000, 555, 555, 555, 333, 222, 222, 1, 1, 1, 222, 222, 555, 587, 587, 1, 587, 1000, 1000, 1000, 6, 7, 6, 7, 7, 7, 6, 1, 1, 222, 555, 587, 3, 3, 3, 1, 3, 3, 6, 6, 49, 587, 587, 49, 49, 49, 100, 100, 1000, 100, 1000, 555, 222)
31+
val expected = numbers.sorted()
32+
33+
numbers.countSort(1, 1000)
34+
35+
Assertions.assertEquals(expected, numbers.toList())
36+
}
37+
38+
}

0 commit comments

Comments
 (0)