Skip to content

Commit 346bef7

Browse files
author
evitwilly
committed
added finite state machine to calculate the number of ones and zeros in a binary string
1 parent b363e9e commit 346bef7

File tree

7 files changed

+107
-0
lines changed

7 files changed

+107
-0
lines changed
Binary file not shown.
Binary file not shown.

.gradle/7.1/fileHashes/fileHashes.bin

450 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
Binary file not shown.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package other
2+
3+
/**
4+
* This algorithm counts the number of ones and zeros in a binary string
5+
* and implemented on a finite state machine
6+
*
7+
*/
8+
class BinaryDigitsCounter {
9+
10+
/**
11+
* Stores the result of the algorithm
12+
*
13+
*/
14+
data class Result(private val ones: Int = 0, private val zeros: Int = 0)
15+
16+
/**
17+
* Represents two states
18+
*
19+
*/
20+
private enum class State {
21+
ONE, ZERO
22+
}
23+
24+
fun compute(binaryString: String): Result { // 1010010011
25+
if (binaryString.isEmpty()) {
26+
return Result()
27+
}
28+
29+
// define initial state
30+
var currentState = if (binaryString.first() == '1') State.ONE else State.ZERO
31+
32+
var onesCount = 0
33+
var zerosCount = 0
34+
35+
binaryString.forEach { symbol ->
36+
// we use 'when' statement to toggle the state
37+
when (currentState) {
38+
State.ONE -> {
39+
if (symbol == '0') {
40+
zerosCount++
41+
// move to another state
42+
currentState = State.ZERO
43+
} else {
44+
onesCount++
45+
}
46+
}
47+
State.ZERO -> {
48+
if (symbol == '1') {
49+
onesCount++
50+
// move to another state
51+
currentState = State.ONE
52+
} else {
53+
zerosCount++
54+
}
55+
}
56+
}
57+
}
58+
59+
return Result(onesCount, zerosCount)
60+
}
61+
62+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package other
2+
3+
import org.junit.Test
4+
import org.junit.jupiter.api.Assertions
5+
6+
internal class BinaryDigitsCounterTest {
7+
8+
private val counter = BinaryDigitsCounter()
9+
10+
@Test
11+
fun test_empty_string() {
12+
val result = counter.compute("")
13+
14+
Assertions.assertEquals(BinaryDigitsCounter.Result(), result)
15+
}
16+
17+
@Test
18+
fun test_binary_string_1() {
19+
val result = counter.compute("10101111000")
20+
21+
Assertions.assertEquals(BinaryDigitsCounter.Result(6, 5), result)
22+
}
23+
24+
@Test
25+
fun test_binary_string_2() {
26+
val result = counter.compute("0100000111110010101010")
27+
28+
Assertions.assertEquals(BinaryDigitsCounter.Result(10, 12), result)
29+
}
30+
31+
@Test
32+
fun test_binary_string_3() {
33+
val result = counter.compute("1111111111")
34+
35+
Assertions.assertEquals(BinaryDigitsCounter.Result(10, 0), result)
36+
}
37+
38+
@Test
39+
fun test_binary_string_4() {
40+
val result = counter.compute("0000000000")
41+
42+
Assertions.assertEquals(BinaryDigitsCounter.Result(0, 10), result)
43+
}
44+
45+
}

0 commit comments

Comments
 (0)