added count sort algorithm, edited some comments in different files, added Kotlin variant for Command pattern

This commit is contained in:
evitwilly
2022-08-26 16:41:07 +03:00
parent 37257596b0
commit 9aee12a045
18 changed files with 106 additions and 3 deletions

View File

@ -10,7 +10,8 @@ package design_patterns
/**
* first variant
*
* The first variant
*
*/
@ -62,7 +63,8 @@ class Pony1 {
/**
*
* the second variant
* The second variant
*
*/
class Pony2 {
@ -106,6 +108,7 @@ class Pony2 {
/**
*
* Kotlin variant with default arguments
*
*/
class Pony3(

View File

@ -4,6 +4,8 @@ package design_patterns
* pattern: Command
*
* description: it's a behavioral pattern that allows you to wrap requests or simple operations in separate objects.
*
* P.S. Kotlin variant of this pattern is shown in tests
*/
interface ArithmeticCommand {

View File

@ -7,6 +7,7 @@ package design_patterns
*
* description: classes implement a common interface and to extend the functionality of the previous object,
* the old object is passed through the constructor
*
*/
interface MyPrinter {

View File

@ -6,6 +6,7 @@ package design_patterns
* using: used when we have classes that depend on others
*
* description: all dependencies (classes that ours depends on) are passed through the constructor
*
*/
class NewsApiService {

View File

@ -6,6 +6,7 @@ package design_patterns
* using: used to simplify access to an object with a complex implementation
*
* description: a complex object contains several dependencies within itself, which it combines with each other
*
*/

View File

@ -7,6 +7,7 @@ package design_patterns
*
* description: the object has special methods that change it and return it
* with a new state for further manipulations
*
*/
class View {

View File

@ -7,6 +7,7 @@ package design_patterns
*
* description: the object communicates its changes to observers
* who previously subscribed to its changes
*
*/
fun interface Observer {

View File

@ -7,6 +7,7 @@ package design_patterns
* the entire execution of our program
*
* description: class allows you to create only a single object
*
*/
object LocalData {

View File

@ -4,6 +4,7 @@ package design_patterns
* pattern: Strategy
*
* using: used when we need to change the behavior of an object
*
*/
interface ExchangeStrategy {

View File

@ -5,6 +5,7 @@ package design_patterns
*
* description: it's a behavioral pattern that allows you to add a new operation
* to an entire class hierarchy without changing the code of these classes.
*
*/
interface PonyVisitor {

View File

@ -0,0 +1,36 @@
package sorting
/**
* count sort
*
* worst time: n
* the best time: n
* average time: n
*
* amount of memory: equals to the size of the range of numbers plus 1 (for example: 1001 for numbers from 0 to 1000)
*
* The use of counting sort is useful only when the sorted numbers have (or can be mapped to) a range of possible
* values that is small enough compared to the sorted set, for example, a million natural numbers less than 1000
*
*/
fun Array<Int>.countSort(start: Int, end: Int) { // sorts numbers in the range from start to end
val countedNumbers = Array(end + 1) { 0 }
var index = 0
while (index < size) {
countedNumbers[this[index]]++
index++
}
index = 0
var currentNumber = start
while (currentNumber < countedNumbers.size) {
var frequency = countedNumbers[currentNumber]
while (frequency > 0) {
this[index++] = currentNumber
frequency--
}
currentNumber++
}
}

View File

@ -3,7 +3,7 @@ package design_patterns
import org.junit.Test
import org.junit.jupiter.api.Assertions
internal class Command {
internal class CommandTest {
@Test
fun test_1() {
@ -57,4 +57,20 @@ internal class Command {
Assertions.assertEquals(2000, actual)
}
@Test
fun test_kotlin_variant() {
val commands: List<(Int) -> Int> = listOf(
{ actual: Int -> actual + 49 },
{ actual: Int -> actual - 20 },
{ actual: Int -> actual * 6 }
)
var actual = 1
commands.forEach { command ->
actual = command.invoke(actual)
}
Assertions.assertEquals(180, actual)
}
}

View File

@ -0,0 +1,38 @@
package sorting
import org.junit.Test
import org.junit.jupiter.api.Assertions
class CountSortTest {
@Test
fun test_numbers_from_zero_to_five() {
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)
val expected = numbers.sorted()
numbers.countSort(0, 5)
Assertions.assertEquals(expected, numbers.toList())
}
@Test
fun test_numbers_from_one_to_ten() {
val numbers = arrayOf(9, 9, 9, 10, 10, 5, 4, 4, 4, 1, 1, 1, 3, 3, 3)
val expected = numbers.sorted()
numbers.countSort(1, 10)
Assertions.assertEquals(expected, numbers.toList())
}
@Test
fun test_numbers_from_one_to_thousand() {
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)
val expected = numbers.sorted()
numbers.countSort(1, 1000)
Assertions.assertEquals(expected, numbers.toList())
}
}