added new algorithms: ParenthesisCheck and StringEqualsHash; edited some comments in other package
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -1,6 +1,7 @@
|
||||
package other
|
||||
|
||||
/**
|
||||
*
|
||||
* Euclid's algorithm for finding the greatest common divisor
|
||||
*
|
||||
*/
|
||||
@ -8,9 +9,11 @@ package other
|
||||
class Euclid {
|
||||
|
||||
/**
|
||||
*
|
||||
* finds the greatest common divisor of two numbers
|
||||
*
|
||||
* @return returns the greatest common divisor
|
||||
*
|
||||
*/
|
||||
fun compute(num1: Int, num2: Int) : Int {
|
||||
var copyNum1 = num1
|
||||
|
||||
@ -3,6 +3,7 @@ package other
|
||||
import java.lang.Integer.min
|
||||
|
||||
/**
|
||||
*
|
||||
* Algorithm for determining the Levenshtein distance
|
||||
*
|
||||
*/
|
||||
@ -10,9 +11,11 @@ import java.lang.Integer.min
|
||||
class LevenshteinLength {
|
||||
|
||||
/**
|
||||
*
|
||||
* determines the Levenshtein distance for two strings and returns it
|
||||
*
|
||||
* @return returns the Levenshtein distance for two strings
|
||||
*
|
||||
*/
|
||||
fun compute(str1: String, str2: String) : Int {
|
||||
val matrix = Array(str1.length + 1) {
|
||||
@ -39,4 +42,5 @@ class LevenshteinLength {
|
||||
|
||||
return matrix[str1.length][str2.length]
|
||||
}
|
||||
|
||||
}
|
||||
@ -12,6 +12,7 @@ class Max<T : Comparable<T>> {
|
||||
/**
|
||||
*
|
||||
* @return returns the maximum element from the list
|
||||
*
|
||||
*/
|
||||
fun compute(items: List<T>) : T {
|
||||
if (items.isEmpty()) {
|
||||
@ -29,6 +30,7 @@ class Max<T : Comparable<T>> {
|
||||
/**
|
||||
*
|
||||
* @return returns the maximum element from the list recursively
|
||||
*
|
||||
*/
|
||||
fun computeRecursive(items: List<T>) : T {
|
||||
if (items.size == 1) {
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package other
|
||||
|
||||
/**
|
||||
*
|
||||
* Algorithm for checking a string for a palindrome
|
||||
*
|
||||
*/
|
||||
@ -8,9 +9,11 @@ package other
|
||||
class Palindrome(private val text: String) {
|
||||
|
||||
/**
|
||||
*
|
||||
* checks a string for a palindrome
|
||||
*
|
||||
* @return returns true if the string is a palindrome
|
||||
*
|
||||
*/
|
||||
fun isYes() = text == text.reversed()
|
||||
|
||||
@ -18,6 +21,7 @@ class Palindrome(private val text: String) {
|
||||
* checks if a string is not a palindrome
|
||||
*
|
||||
* @return returns true if the string is not a palindrome
|
||||
*
|
||||
*/
|
||||
fun isNot() = !isYes()
|
||||
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package other
|
||||
|
||||
/**
|
||||
*
|
||||
* Algorithm for checking a string for a palindrome
|
||||
*
|
||||
*/
|
||||
@ -8,9 +9,11 @@ package other
|
||||
class PalindromeAdvanced(private val text: String) {
|
||||
|
||||
/**
|
||||
*
|
||||
* checks a string for a palindrome
|
||||
*
|
||||
* @return returns true if the string is a palindrome
|
||||
*
|
||||
*/
|
||||
fun isYes() : Boolean {
|
||||
if (text.length <= 1) {
|
||||
@ -25,9 +28,11 @@ class PalindromeAdvanced(private val text: String) {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* checks if a string is not a palindrome
|
||||
*
|
||||
* @return returns true if the string is not a palindrome
|
||||
*
|
||||
*/
|
||||
fun isNot() = !isYes()
|
||||
|
||||
|
||||
60
src/main/kotlin/other/ParenthesisCheck.kt
Normal file
60
src/main/kotlin/other/ParenthesisCheck.kt
Normal file
@ -0,0 +1,60 @@
|
||||
package other
|
||||
|
||||
/**
|
||||
*
|
||||
* checks a string for correct placement of parentheses using stack
|
||||
*
|
||||
* ([]) - correctly
|
||||
* ()(){} - correctly
|
||||
* (() - incorrectly
|
||||
* (())[][]}{ - incorrectly
|
||||
*
|
||||
*/
|
||||
|
||||
class ParenthesisCheck {
|
||||
|
||||
/**
|
||||
*
|
||||
* we use a regular kotlin list to create a stack
|
||||
*
|
||||
* @return returns true if parentheses are correctly spaced otherwise false
|
||||
*
|
||||
*/
|
||||
fun check(code: String = defaultCode): Boolean {
|
||||
val stack = mutableListOf<Char>()
|
||||
|
||||
var index = 0
|
||||
while (index < code.length) {
|
||||
|
||||
when (val symbol = code[index]) {
|
||||
'(', '{', '[' -> stack.add(symbol)
|
||||
')', '}', ']' -> {
|
||||
val value = bracketRelations[stack.removeLastOrNull()]
|
||||
if (symbol != value) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
index++
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
companion object {
|
||||
/**
|
||||
*
|
||||
* correct C program
|
||||
*
|
||||
*/
|
||||
private const val defaultCode = """
|
||||
void main() {
|
||||
printf("Hello, World!");
|
||||
}
|
||||
"""
|
||||
|
||||
private val bracketRelations = mapOf('(' to ')', '{' to '}', '[' to ']')
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,9 +1,11 @@
|
||||
package other
|
||||
|
||||
/**
|
||||
* Reverse Array
|
||||
*
|
||||
* reverse array
|
||||
*
|
||||
* algorithm complexity: n/2 operations
|
||||
*
|
||||
*/
|
||||
|
||||
class ReverseArray<T> {
|
||||
|
||||
@ -1,18 +1,22 @@
|
||||
package other
|
||||
|
||||
/**
|
||||
*
|
||||
* The sieve of Eratosthenes allows you to efficiently calculate a series of prime numbers
|
||||
*
|
||||
* algorithm complexity: nlog(logn) operations
|
||||
*
|
||||
*/
|
||||
|
||||
class SieveOfEratosthenes {
|
||||
|
||||
/**
|
||||
*
|
||||
* computes a series of primes for the maximum value
|
||||
*
|
||||
* @maxNumber - maximum value
|
||||
* @return - returns a list of prime numbers
|
||||
* @param maxNumber - maximum value
|
||||
* @return returns a list of prime numbers
|
||||
*
|
||||
*/
|
||||
fun compute(maxNumber: Int) : List<Int> {
|
||||
val numbers = Array(maxNumber + 1) { index -> index >= 2 }
|
||||
|
||||
45
src/main/kotlin/other/StringEqualsHash.kt
Normal file
45
src/main/kotlin/other/StringEqualsHash.kt
Normal file
@ -0,0 +1,45 @@
|
||||
package other
|
||||
|
||||
/**
|
||||
*
|
||||
* comparing two strings with a hash
|
||||
*
|
||||
*/
|
||||
|
||||
class StringEqualsHash {
|
||||
|
||||
/**
|
||||
*
|
||||
* computes the hash of a string according to the formula:
|
||||
*
|
||||
* hash(abc) = a.code * primeCoefficient⁰ + b.code * primeCoefficient¹ + c.code * primeCoefficient²
|
||||
*
|
||||
* @return returns the hash of the string
|
||||
*
|
||||
*/
|
||||
private fun String.hash() : Int {
|
||||
var result = 0
|
||||
var factor = 1
|
||||
forEach { symbol ->
|
||||
result += symbol.code * factor
|
||||
factor *= primeCoefficient
|
||||
}
|
||||
return result.mod(Int.MAX_VALUE)
|
||||
}
|
||||
|
||||
fun equals(source: String, pattern: String) : Boolean =
|
||||
if (source.length != pattern.length) false else source.hash() == pattern.hash()
|
||||
|
||||
companion object {
|
||||
/**
|
||||
*
|
||||
* I chose the nearest prime number for the size of the alphabet
|
||||
*
|
||||
* my alphabet is [a-z] [A-Z] ! , .
|
||||
*
|
||||
* size of the alphabet = 26 + 26 + 3 = 55 (is not prime)
|
||||
*
|
||||
*/
|
||||
private const val primeCoefficient = 53
|
||||
}
|
||||
}
|
||||
44
src/test/kotlin/other/ParenthesisCheckTest.kt
Normal file
44
src/test/kotlin/other/ParenthesisCheckTest.kt
Normal file
@ -0,0 +1,44 @@
|
||||
package other
|
||||
|
||||
import org.junit.Test
|
||||
import org.junit.jupiter.api.Assertions
|
||||
|
||||
internal class ParenthesisCheckTest {
|
||||
|
||||
private val parenthesisCheck = ParenthesisCheck()
|
||||
|
||||
@Test
|
||||
fun test_default_c_program() {
|
||||
Assertions.assertEquals(true, parenthesisCheck.check())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun test_failed_c_program() {
|
||||
val failCode = """
|
||||
void main({
|
||||
printf("Hello, World!";
|
||||
}
|
||||
""".trimIndent()
|
||||
Assertions.assertEquals(false, parenthesisCheck.check(failCode))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun test_statement_1() {
|
||||
val statement = "(([[]])}".trimIndent()
|
||||
Assertions.assertEquals(false, parenthesisCheck.check(statement))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun test_statement_2() {
|
||||
val statement = "(([[()]])){}{}()".trimIndent()
|
||||
Assertions.assertEquals(true, parenthesisCheck.check(statement))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun test_statement_3() {
|
||||
val statement = "(([[()]])){}{}([)".trimIndent()
|
||||
Assertions.assertEquals(false, parenthesisCheck.check(statement))
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -1,16 +1,24 @@
|
||||
package other
|
||||
|
||||
import org.junit.Test
|
||||
import org.junit.jupiter.api.Assertions.*
|
||||
import org.junit.jupiter.api.Assertions
|
||||
|
||||
internal class SieveOfEratosthenesTest {
|
||||
|
||||
@Test
|
||||
fun test() {
|
||||
fun test_success() {
|
||||
val eratosthenes = SieveOfEratosthenes()
|
||||
val actual = eratosthenes.compute(10)
|
||||
val expected = listOf(2, 3, 5, 7)
|
||||
assertEquals(expected, actual)
|
||||
Assertions.assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun test_fail() {
|
||||
val eratosthenes = SieveOfEratosthenes()
|
||||
val actual = eratosthenes.compute(5)
|
||||
val expected = listOf(4)
|
||||
Assertions.assertNotEquals(expected, actual)
|
||||
}
|
||||
|
||||
}
|
||||
26
src/test/kotlin/other/StringEqualsHashTest.kt
Normal file
26
src/test/kotlin/other/StringEqualsHashTest.kt
Normal file
@ -0,0 +1,26 @@
|
||||
package other
|
||||
|
||||
import org.junit.Test
|
||||
import org.junit.jupiter.api.Assertions
|
||||
|
||||
internal class StringEqualsHashTest {
|
||||
|
||||
private val stringEqualsHash = StringEqualsHash()
|
||||
|
||||
@Test
|
||||
fun test_two_the_same_strings() {
|
||||
val str1 = "Twilight Sparkle"
|
||||
val str2 = "Twilight Sparkle"
|
||||
|
||||
Assertions.assertEquals(true, stringEqualsHash.equals(str1, str2))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun test_two_different_strings() {
|
||||
val greeting = "How are you?"
|
||||
val pattern = "Happy birthday to me!"
|
||||
|
||||
Assertions.assertEquals(false, stringEqualsHash.equals(greeting, pattern))
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user