File tree Expand file tree Collapse file tree 5 files changed +48
-23
lines changed
src/test/kotlin/com/igorwojda/integer Expand file tree Collapse file tree 5 files changed +48
-23
lines changed Original file line number Diff line number Diff line change @@ -16,23 +16,19 @@ fun main() {
1616}*/
1717
1818private fun fibonacci (n : Int ): Int {
19- if (n < 2 ) {
20- return n
21- }
22-
2319 var first = 0
2420 var second = 1
25- var current = 0
21+ val listItems : MutableList < Int > = mutableListOf< Int >(first, second)
2622
27- (2 .. n).forEach {
28- println (" it value --> $n " )
29- current = first + second
23+ if (n == 0 ) return 0
24+ (2 until n).forEach { _ ->
25+ val next = first + second
26+ listItems.add(next)
3027 first = second
31- second = current
32- println (" it current value --> $current " )
28+ second = next
3329 }
34-
35- return current
30+ println ( " fib list --> $listItems " )
31+ return listItems.sum()
3632}
3733
3834private class Test {
Original file line number Diff line number Diff line change @@ -22,17 +22,26 @@ private fun fizzBuzz(n: Int): List<String> {
2222 }
2323 }*/
2424
25- (1 until n + 1 ).forEach {
25+ /* (1 until n +1).forEach {
2626 val item = when {
2727 (it % 3 == 0 && it % 5 == 0) -> { "FizzBuzz" }
2828 (it % 3 == 0) -> { "Fizz" }
2929 (it % 5 == 0) -> { "Buzz" }
3030 else -> { it.toString() }
3131 }
3232 fizzBuzzList.add(item)
33- }
33+ }*/
3434
35- return fizzBuzzList
35+ for (i in n downTo 1 ) {
36+ val item = when {
37+ (i % 3 == 0 && i % 5 == 0 ) -> { " FizzBuzz" }
38+ (i % 3 == 0 ) -> { " Fizz" }
39+ (i % 5 == 0 ) -> { " Buzz" }
40+ else -> { i.toString() }
41+ }
42+ fizzBuzzList.add(item)
43+ }
44+ return fizzBuzzList.reversed()
3645}
3746
3847private class Test {
Original file line number Diff line number Diff line change @@ -6,14 +6,18 @@ import org.junit.jupiter.api.Test
66fun main () {
77 getAllPairs(10 )
88}
9+
910private fun getAllPairs (n : Int ): List <Pair <Int , Int >> {
10- val listPairValue = arrayListOf<Pair <Int , Int >>()
11- (0 until n + 1 ).forEach { i ->
12- (0 until n + 1 ).forEach { j ->
13- listPairValue.add(i to j)
11+ val pairedList: MutableList <Pair <Int , Int >> = mutableListOf ()
12+ (0 until n).forEach { i ->
13+ // for every i
14+ (0 until n).forEach { j ->
15+ // take every j and attach
16+ pairedList.add(Pair (i, j))
1417 }
1518 }
16- return listPairValue
19+ println (pairedList)
20+ return pairedList
1721}
1822
1923private class Test {
Original file line number Diff line number Diff line change @@ -4,7 +4,14 @@ import org.amshove.kluent.shouldBeEqualTo
44import org.junit.jupiter.api.Test
55
66private fun power (base : Int , exponent : Int ): Int {
7- TODO (" not implemented" )
7+ // loop through the exponent and multiply the result variable with base that many times and return
8+ var result = 1
9+ if (base == 0 ) return 0
10+ for (power in 0 until exponent) {
11+ println (" loop data:$power $result " )
12+ result * = base
13+ }
14+ return result
815}
916
1017private class Test {
Original file line number Diff line number Diff line change @@ -3,8 +3,17 @@ package com.igorwojda.integer.printnumber.steps
33import org.amshove.kluent.shouldBeEqualTo
44import org.junit.jupiter.api.Test
55
6- private fun printNumber (n : Int , step : Int = 1): List <Int > {
7- TODO (" not implemented" )
6+ private fun printNumber (n : Int , step : Int ): List <Int > {
7+ // Use a while loop, that helps to reduce via step while iterating with the reduced value
8+ if (n == 0 ) return emptyList()
9+ val resultList: MutableList <Int > = mutableListOf<Int >()
10+ var current = n
11+ while (current > 0 ) {
12+ println (current)
13+ resultList.add(current)
14+ current - = step
15+ }
16+ return resultList
817}
918
1019private class Test {
You can’t perform that action at this time.
0 commit comments