Skip to content

Commit 378d70d

Browse files
author
evitwilly
committed
added new algorithms: ParenthesisCheck and StringEqualsHash; edited some comments in other package
1 parent d714fc1 commit 378d70d

17 files changed

+213
-6
lines changed
Binary file not shown.
Binary file not shown.

.gradle/7.1/fileHashes/fileHashes.bin

1.03 KB
Binary file not shown.
0 Bytes
Binary file not shown.
Binary file not shown.

src/main/kotlin/other/Euclid.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
package other
22

33
/**
4+
*
45
* Euclid's algorithm for finding the greatest common divisor
56
*
67
*/
78

89
class Euclid {
910

1011
/**
12+
*
1113
* finds the greatest common divisor of two numbers
1214
*
1315
* @return returns the greatest common divisor
16+
*
1417
*/
1518
fun compute(num1: Int, num2: Int) : Int {
1619
var copyNum1 = num1

src/main/kotlin/other/LevensteinLength.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,19 @@ package other
33
import java.lang.Integer.min
44

55
/**
6+
*
67
* Algorithm for determining the Levenshtein distance
78
*
89
*/
910

1011
class LevenshteinLength {
1112

1213
/**
14+
*
1315
* determines the Levenshtein distance for two strings and returns it
1416
*
1517
* @return returns the Levenshtein distance for two strings
18+
*
1619
*/
1720
fun compute(str1: String, str2: String) : Int {
1821
val matrix = Array(str1.length + 1) {
@@ -39,4 +42,5 @@ class LevenshteinLength {
3942

4043
return matrix[str1.length][str2.length]
4144
}
45+
4246
}

src/main/kotlin/other/Max.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Max<T : Comparable<T>> {
1212
/**
1313
*
1414
* @return returns the maximum element from the list
15+
*
1516
*/
1617
fun compute(items: List<T>) : T {
1718
if (items.isEmpty()) {
@@ -29,6 +30,7 @@ class Max<T : Comparable<T>> {
2930
/**
3031
*
3132
* @return returns the maximum element from the list recursively
33+
*
3234
*/
3335
fun computeRecursive(items: List<T>) : T {
3436
if (items.size == 1) {

src/main/kotlin/other/Palindrome.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
package other
22

33
/**
4+
*
45
* Algorithm for checking a string for a palindrome
56
*
67
*/
78

89
class Palindrome(private val text: String) {
910

1011
/**
12+
*
1113
* checks a string for a palindrome
1214
*
1315
* @return returns true if the string is a palindrome
16+
*
1417
*/
1518
fun isYes() = text == text.reversed()
1619

1720
/**
1821
* checks if a string is not a palindrome
1922
*
2023
* @return returns true if the string is not a palindrome
24+
*
2125
*/
2226
fun isNot() = !isYes()
2327

src/main/kotlin/other/PalindromeAdvanced.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
package other
22

33
/**
4+
*
45
* Algorithm for checking a string for a palindrome
56
*
67
*/
78

89
class PalindromeAdvanced(private val text: String) {
910

1011
/**
12+
*
1113
* checks a string for a palindrome
1214
*
1315
* @return returns true if the string is a palindrome
16+
*
1417
*/
1518
fun isYes() : Boolean {
1619
if (text.length <= 1) {
@@ -25,9 +28,11 @@ class PalindromeAdvanced(private val text: String) {
2528
}
2629

2730
/**
31+
*
2832
* checks if a string is not a palindrome
2933
*
3034
* @return returns true if the string is not a palindrome
35+
*
3136
*/
3237
fun isNot() = !isYes()
3338

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package other
2+
3+
/**
4+
*
5+
* checks a string for correct placement of parentheses using stack
6+
*
7+
* ([]) - correctly
8+
* ()(){} - correctly
9+
* (() - incorrectly
10+
* (())[][]}{ - incorrectly
11+
*
12+
*/
13+
14+
class ParenthesisCheck {
15+
16+
/**
17+
*
18+
* we use a regular kotlin list to create a stack
19+
*
20+
* @return returns true if parentheses are correctly spaced otherwise false
21+
*
22+
*/
23+
fun check(code: String = defaultCode): Boolean {
24+
val stack = mutableListOf<Char>()
25+
26+
var index = 0
27+
while (index < code.length) {
28+
29+
when (val symbol = code[index]) {
30+
'(', '{', '[' -> stack.add(symbol)
31+
')', '}', ']' -> {
32+
val value = bracketRelations[stack.removeLastOrNull()]
33+
if (symbol != value) {
34+
return false
35+
}
36+
}
37+
}
38+
39+
index++
40+
}
41+
42+
return true
43+
}
44+
45+
companion object {
46+
/**
47+
*
48+
* correct C program
49+
*
50+
*/
51+
private const val defaultCode = """
52+
void main() {
53+
printf("Hello, World!");
54+
}
55+
"""
56+
57+
private val bracketRelations = mapOf('(' to ')', '{' to '}', '[' to ']')
58+
}
59+
60+
}

src/main/kotlin/other/ReverseArray.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package other
22

33
/**
4-
* Reverse Array
4+
*
5+
* reverse array
56
*
67
* algorithm complexity: n/2 operations
8+
*
79
*/
810

911
class ReverseArray<T> {

src/main/kotlin/other/SieveOfEratosthenes.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
package other
22

33
/**
4+
*
45
* The sieve of Eratosthenes allows you to efficiently calculate a series of prime numbers
56
*
67
* algorithm complexity: nlog(logn) operations
8+
*
79
*/
810

911
class SieveOfEratosthenes {
1012

1113
/**
14+
*
1215
* computes a series of primes for the maximum value
1316
*
14-
* @maxNumber - maximum value
15-
* @return - returns a list of prime numbers
17+
* @param maxNumber - maximum value
18+
* @return returns a list of prime numbers
19+
*
1620
*/
1721
fun compute(maxNumber: Int) : List<Int> {
1822
val numbers = Array(maxNumber + 1) { index -> index >= 2 }
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package other
2+
3+
/**
4+
*
5+
* comparing two strings with a hash
6+
*
7+
*/
8+
9+
class StringEqualsHash {
10+
11+
/**
12+
*
13+
* computes the hash of a string according to the formula:
14+
*
15+
* hash(abc) = a.code * primeCoefficient⁰ + b.code * primeCoefficient¹ + c.code * primeCoefficient²
16+
*
17+
* @return returns the hash of the string
18+
*
19+
*/
20+
private fun String.hash() : Int {
21+
var result = 0
22+
var factor = 1
23+
forEach { symbol ->
24+
result += symbol.code * factor
25+
factor *= primeCoefficient
26+
}
27+
return result.mod(Int.MAX_VALUE)
28+
}
29+
30+
fun equals(source: String, pattern: String) : Boolean =
31+
if (source.length != pattern.length) false else source.hash() == pattern.hash()
32+
33+
companion object {
34+
/**
35+
*
36+
* I chose the nearest prime number for the size of the alphabet
37+
*
38+
* my alphabet is [a-z] [A-Z] ! , .
39+
*
40+
* size of the alphabet = 26 + 26 + 3 = 55 (is not prime)
41+
*
42+
*/
43+
private const val primeCoefficient = 53
44+
}
45+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package other
2+
3+
import org.junit.Test
4+
import org.junit.jupiter.api.Assertions
5+
6+
internal class ParenthesisCheckTest {
7+
8+
private val parenthesisCheck = ParenthesisCheck()
9+
10+
@Test
11+
fun test_default_c_program() {
12+
Assertions.assertEquals(true, parenthesisCheck.check())
13+
}
14+
15+
@Test
16+
fun test_failed_c_program() {
17+
val failCode = """
18+
void main({
19+
printf("Hello, World!";
20+
}
21+
""".trimIndent()
22+
Assertions.assertEquals(false, parenthesisCheck.check(failCode))
23+
}
24+
25+
@Test
26+
fun test_statement_1() {
27+
val statement = "(([[]])}".trimIndent()
28+
Assertions.assertEquals(false, parenthesisCheck.check(statement))
29+
}
30+
31+
@Test
32+
fun test_statement_2() {
33+
val statement = "(([[()]])){}{}()".trimIndent()
34+
Assertions.assertEquals(true, parenthesisCheck.check(statement))
35+
}
36+
37+
@Test
38+
fun test_statement_3() {
39+
val statement = "(([[()]])){}{}([)".trimIndent()
40+
Assertions.assertEquals(false, parenthesisCheck.check(statement))
41+
}
42+
43+
44+
}
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
package other
22

33
import org.junit.Test
4-
import org.junit.jupiter.api.Assertions.*
4+
import org.junit.jupiter.api.Assertions
55

66
internal class SieveOfEratosthenesTest {
77

88
@Test
9-
fun test() {
9+
fun test_success() {
1010
val eratosthenes = SieveOfEratosthenes()
1111
val actual = eratosthenes.compute(10)
1212
val expected = listOf(2, 3, 5, 7)
13-
assertEquals(expected, actual)
13+
Assertions.assertEquals(expected, actual)
14+
}
15+
16+
@Test
17+
fun test_fail() {
18+
val eratosthenes = SieveOfEratosthenes()
19+
val actual = eratosthenes.compute(5)
20+
val expected = listOf(4)
21+
Assertions.assertNotEquals(expected, actual)
1422
}
1523

1624
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package other
2+
3+
import org.junit.Test
4+
import org.junit.jupiter.api.Assertions
5+
6+
internal class StringEqualsHashTest {
7+
8+
private val stringEqualsHash = StringEqualsHash()
9+
10+
@Test
11+
fun test_two_the_same_strings() {
12+
val str1 = "Twilight Sparkle"
13+
val str2 = "Twilight Sparkle"
14+
15+
Assertions.assertEquals(true, stringEqualsHash.equals(str1, str2))
16+
}
17+
18+
@Test
19+
fun test_two_different_strings() {
20+
val greeting = "How are you?"
21+
val pattern = "Happy birthday to me!"
22+
23+
Assertions.assertEquals(false, stringEqualsHash.equals(greeting, pattern))
24+
}
25+
26+
}

0 commit comments

Comments
 (0)