Skip to content

Commit a48bd20

Browse files
refactored some algorithms in the other package
1 parent 566145d commit a48bd20

File tree

8 files changed

+65
-65
lines changed

8 files changed

+65
-65
lines changed

src/main/kotlin/other/BinaryDigitsCounter.kt

+5-9
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,18 @@ package other
33
/**
44
*
55
* This algorithm counts the number of ones and zeros in a binary string
6+
*
67
* and implemented on a finite state machine
78
*
89
*/
910

1011
class BinaryDigitsCounter {
11-
12-
/**
13-
* Stores the result of the algorithm
14-
*/
12+
1513
data class Result(private val ones: Int = 0, private val zeros: Int = 0)
1614

17-
/**
18-
* Represents two states
19-
*/
20-
private enum class State {
21-
ONE, ZERO
15+
// represents two states
16+
private enum class State {
17+
ONE, ZERO
2218
}
2319

2420
fun compute(binaryString: String): Result { // 1010010011

src/main/kotlin/other/Euclid.kt

+21-11
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,30 @@ package other
88

99
class Euclid {
1010

11-
/**
12-
* finds the greatest common divisor of two numbers and returns it
13-
*/
14-
fun compute(num1: Int, num2: Int) : Int {
15-
var copyNum1 = num1
16-
var copyNum2 = num2
17-
while (copyNum1 != 0 && copyNum2 != 0) {
18-
if (copyNum1 > copyNum2) {
19-
copyNum1 %= copyNum2
11+
fun computeByDivisionWithRemainder(number1: Int, number2: Int) : Int {
12+
var copyNumber1 = number1
13+
var copyNumber2 = number2
14+
while (copyNumber1 != 0 && copyNumber2 != 0) {
15+
if (copyNumber1 > copyNumber2) {
16+
copyNumber1 %= copyNumber2
2017
} else {
21-
copyNum2 %= copyNum1
18+
copyNumber2 %= copyNumber1
2219
}
2320
}
24-
return copyNum1 + copyNum2
21+
return copyNumber1 + copyNumber2
22+
}
23+
24+
fun computeBySubtraction(number1: Int, number2: Int): Int {
25+
var copyNumber1 = number1
26+
var copyNumber2 = number2
27+
while (copyNumber1 != copyNumber2) {
28+
if (copyNumber1 > copyNumber2) {
29+
copyNumber1 -= copyNumber2
30+
} else {
31+
copyNumber2 -= copyNumber1
32+
}
33+
}
34+
return copyNumber1
2535
}
2636

2737
}

src/main/kotlin/other/Factorial.kt

+3-7
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ package other
99
class Factorial {
1010

1111
/**
12+
* iterative method
1213
* worst time: O(n)
1314
* amount of memory: O(1)
14-
*
15-
* @return returns the factorial of a number by an iterative method
1615
*/
1716
fun compute(number: Int) : Int {
1817
if (number <= 1) {
@@ -27,10 +26,9 @@ class Factorial {
2726
}
2827

2928
/**
29+
* recursive method
3030
* worst time: O(n)
3131
* amount of memory: O(n) - stack for recursion
32-
*
33-
* @return returns factorial recursively
3432
*/
3533
fun computeRecursive(number: Int) : Int {
3634
return if (number <= 1) {
@@ -40,9 +38,7 @@ class Factorial {
4038
}
4139
}
4240

43-
/**
44-
* @see <a href="https://kotlinlang.org/docs/functions.html#tail-recursive-functions">tailrec functions</a>
45-
*/
41+
// read more: https://kotlinlang.org/docs/functions.html#tail-recursive-functions
4642
tailrec fun computeRecursiveWithKotlinOptimization(number: Int, result: Int = 1) : Int =
4743
if (number <= 1) result else computeRecursiveWithKotlinOptimization(number - 1, result * number)
4844

src/main/kotlin/other/FactorialAdvanced.kt

+17-19
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import java.math.BigInteger
1010

1111
class FactorialAdvanced {
1212

13+
// precomputed factorials
1314
private val factorials = longArrayOf(
1415
1L,
1516
1L,
@@ -43,37 +44,37 @@ class FactorialAdvanced {
4344
return BigInteger.valueOf(factorials[n])
4445
}
4546

46-
// Pre-allocate space for our list of intermediate BigIntegers.
47+
// pre-allocate space for our list of intermediate BigIntegers.
4748
val approxSize = divide(n * log2Celling(n), Long.SIZE_BITS)
4849
val bigNumbers = ArrayList<BigInteger>(approxSize)
4950

50-
// Start from the pre-computed maximum long factorial.
51+
// start from the pre-computed maximum long factorial.
5152
val startingNumber = factorials.size
5253
var number = factorials[startingNumber - 1]
53-
// Strip off 2s from this value.
54+
// strip off 2s from this value.
5455
var shift = number.countTrailingZeroBits()
5556
number = number shr shift
5657

57-
// Use floor(log2(num)) + 1 to prevent overflow of multiplication.
58+
// use floor(log2(num)) + 1 to prevent overflow of multiplication.
5859
var numberBits = log2Floor(number) + 1
5960
var bits = log2Floor(startingNumber.toLong()) + 1
60-
// Check for the next power of two boundary, to save us a CLZ operation.
61+
// check for the next power of two boundary, to save us a CLZ operation.
6162
var nextPowerOfTwo = 1 shl bits - 1
6263

63-
// Iteratively multiply the longs as big as they can go.
64+
// iteratively multiply the longs as big as they can go.
6465
for (num in startingNumber..n) {
65-
// Check to see if the floor(log2(num)) + 1 has changed.
66+
// check to see if the floor(log2(num)) + 1 has changed.
6667
if ((num and nextPowerOfTwo) != 0) {
6768
nextPowerOfTwo = nextPowerOfTwo shl 1
6869
bits++
6970
}
70-
// Get rid of the 2s in num.
71+
// get rid of the 2s in num.
7172
val tz = num.toLong().countTrailingZeroBits()
7273
val normalizedNum = (num shr tz).toLong()
7374
shift += tz
74-
// Adjust floor(log2(num)) + 1.
75+
// adjust floor(log2(num)) + 1.
7576
val normalizedBits = bits - tz
76-
// If it won't fit in a long, then we store off the intermediate product.
77+
// if it won't fit in a long, then we store off the intermediate product.
7778
if (normalizedBits + numberBits >= Long.SIZE_BITS) {
7879
bigNumbers.add(BigInteger.valueOf(number))
7980
number = 1
@@ -82,11 +83,11 @@ class FactorialAdvanced {
8283
number *= normalizedNum
8384
numberBits = log2Floor(number) + 1
8485
}
85-
// Check for leftovers.
86+
// check for leftovers.
8687
if (number > 1) {
8788
bigNumbers.add(BigInteger.valueOf(number))
8889
}
89-
// Efficiently multiply all the intermediate products together.
90+
// efficiently multiply all the intermediate products together.
9091
return listNumbers(bigNumbers).shiftLeft(shift)
9192
}
9293

@@ -116,24 +117,21 @@ class FactorialAdvanced {
116117
2 -> numbers[start].multiply(numbers[start + 1])
117118
3 -> numbers[start].multiply(numbers[start + 1]).multiply(numbers[start + 2])
118119
else -> {
119-
// Otherwise, split the list in half and recursively do this.
120+
// otherwise, split the list in half and recursively do this.
120121
val m = end + start ushr 1
121122
listNumbers(numbers, start, m).multiply(listNumbers(numbers, m, end))
122123
}
123124
}
124125
}
125126

126-
/**
127-
* returns the base-2 logarithm of number, rounded according to the celling.
128-
*/
127+
// returns the base-2 logarithm of number, rounded according to the celling.
129128
private fun log2Celling(number: Int): Int {
130129
return Int.SIZE_BITS - (number - 1).countLeadingZeroBits()
131130
}
132131

133-
/**
134-
* returns the base-2 logarithm of number rounded according to the floor.
135-
*/
132+
// returns the base-2 logarithm of number rounded according to the floor.
136133
private fun log2Floor(number: Long): Int {
137134
return Long.SIZE_BITS - 1 - number.countLeadingZeroBits()
138135
}
136+
139137
}

src/main/kotlin/other/FactorialBigWithCache.kt

-3
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,10 @@ class FactorialBigWithCache {
1717
private val cache = hashMapOf<Int, BigInteger>()
1818

1919
/**
20-
*
2120
* worst time: O(n)
2221
* the best time: O(1)
2322
* amount of memory: O(n)
2423
* problem: creating a huge number of BigInteger objects
25-
*
26-
* @return returns the factorial of a number
2724
*/
2825
fun compute(number: Int) : BigInteger {
2926
if (number <= 1) {

src/main/kotlin/other/FactorialWithCache.kt

-3
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,9 @@ class FactorialWithCache {
1313
private val cache = hashMapOf<Int, Int>()
1414

1515
/**
16-
*
1716
* worst time: O(n)
1817
* the best time: O(1)
1918
* amount of memory: O(n)
20-
*
21-
* @return returns the factorial of a number
2219
*/
2320
fun compute(number: Int) : Int {
2421
if (number <= 1) {

src/main/kotlin/other/FizzBuzz.kt

+1-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ package other
1212

1313
class FizzBuzz {
1414

15-
/**
16-
* determines what to say for a number in the FizzBuzz game and returns Fizz, Buzz, FizzBuzz or number
17-
*/
15+
// determines what to say for a number in the FizzBuzz game and returns Fizz, Buzz, FizzBuzz or number
1816
fun compute(number: Int) : String {
1917
return when {
2018
number % 3 == 0 && number % 5 == 0 -> "FizzBuzz"

src/test/kotlin/other/EuclidTest.kt

+18-10
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,27 @@ internal class EuclidTest {
88
private val euclid = Euclid()
99

1010
@Test
11-
fun test_one() {
12-
assertEquals(5, euclid.compute(10, 5))
13-
assertEquals(10, euclid.compute(10, 100))
14-
assertEquals(9, euclid.compute(9, 27))
15-
assertEquals(13, euclid.compute(26, 39))
11+
fun `test computeByDivisionWithRemainder`() {
12+
assertEquals(5, euclid.computeByDivisionWithRemainder(10, 5))
13+
assertEquals(10, euclid.computeByDivisionWithRemainder(10, 100))
14+
assertEquals(9, euclid.computeByDivisionWithRemainder(9, 27))
15+
assertEquals(13, euclid.computeByDivisionWithRemainder(26, 39))
16+
assertEquals(1, euclid.computeByDivisionWithRemainder(135, 13))
17+
assertEquals(1, euclid.computeByDivisionWithRemainder(27, 19))
18+
assertEquals(1, euclid.computeByDivisionWithRemainder(2, 17))
19+
assertEquals(1, euclid.computeByDivisionWithRemainder(4, 9))
1620
}
1721

1822
@Test
19-
fun test_two() {
20-
assertEquals(1, euclid.compute(135, 13))
21-
assertEquals(1, euclid.compute(27, 19))
22-
assertEquals(1, euclid.compute(2, 17))
23-
assertEquals(1, euclid.compute(4, 9))
23+
fun `test computeBySubtraction`() {
24+
assertEquals(5, euclid.computeBySubtraction(10, 5))
25+
assertEquals(10, euclid.computeBySubtraction(10, 100))
26+
assertEquals(9, euclid.computeBySubtraction(9, 27))
27+
assertEquals(13, euclid.computeBySubtraction(26, 39))
28+
assertEquals(1, euclid.computeBySubtraction(135, 13))
29+
assertEquals(1, euclid.computeBySubtraction(27, 19))
30+
assertEquals(1, euclid.computeBySubtraction(2, 17))
31+
assertEquals(1, euclid.computeBySubtraction(4, 9))
2432
}
2533

2634
}

0 commit comments

Comments
 (0)