├── IndexOfMax.kt ├── .github └── FUNDING.yml ├── sum.kt ├── Runs.kt ├── Palíndromo.kt ├── Pairless.kt └── README.md /IndexOfMax.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Your task is to implement the indexOfMax() function so that it returns 3 | * the index of the largest element in the array, or null if the array is empty. 4 | */ 5 | 6 | fun _indexOfMax(a: IntArray): Int? { 7 | var maxIndex = 0 8 | for(elem in a.indices){ 9 | val newElem = a[elem] 10 | if (newElem >= a[maxIndex]){ 11 | maxIndex = elem; 12 | } 13 | } 14 | return maxIndex 15 | } 16 | return if(a.size != 0) _indexOfMax(a) else null 17 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: vicboma1 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 13 | -------------------------------------------------------------------------------- /sum.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Your task is to implement the sum() function so that it computes the sum of 3 | * all elements in the given array a. 4 | */ 5 | package sum 6 | 7 | fun sum(a: IntArray): Int { 8 | // Write your solution here 9 | return 0 10 | } 11 | 12 | // Solution 1 13 | 14 | fun sum1(a: IntArray): Int { 15 | return a.filter { it != null }.sum() 16 | } 17 | 18 | // Solution 2 19 | 20 | fun sum2(a: IntArray): Int { 21 | var _sum = 0 22 | for(element in a) 23 | _sum += element 24 | 25 | return _sum 26 | 27 | // Solution 3 28 | 29 | fun sum3(a: IntArray): Int { 30 | val iterator = a.iterator() 31 | var _sum: Int = 0 32 | while (iterator.hasNext()){ 33 | _sum += iterator.next() 34 | } 35 | 36 | return _sum 37 | } 38 | -------------------------------------------------------------------------------- /Runs.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Any array may be viewed as a number of "runs" of equal numbers. 3 | * For example, the following array has two runs: 4 | * 1, 1, 1, 2, 2 5 | * Three 1's in a row form the first run, and two 2's form the second. 6 | * This array has two runs of length one: 7 | * 3, 4 8 | * And this one has five runs: 9 | * 1, 0, 1, 1, 1, 2, 0, 0, 0, 0, 0, 0, 0 10 | * Your task is to implement the runs() function so that it returns the number 11 | * of runs in the given array. 12 | */ 13 | package runs 14 | 15 | fun runs(a: IntArray): Int { 16 | // Write your solution here 17 | return 0 18 | } 19 | 20 | 21 | // Solution 1 22 | 23 | fun runs(a: IntArray): Int { 24 | var res = 0; 25 | if (a.size == res) 26 | return res 27 | 28 | var base = a[res] 29 | a.forEach({ 30 | if(it != base){ 31 | res ++ 32 | base = it 33 | } 34 | }) 35 | 36 | return (++res); 37 | } 38 | -------------------------------------------------------------------------------- /Palíndromo.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Your task is to implement a palindrome test. 3 | * 4 | * A string is called a palindrome when it reads the same way left-to-right 5 | * and right-to-left. 6 | * 7 | * See http://en.wikipedia.org/wiki/Palindrome 8 | */ 9 | package palindrome 10 | 11 | fun isPalindrome(s: String): Boolean { 12 | // Write your solution here 13 | return false 14 | } 15 | 16 | // Solution 1 17 | 18 | fun isPalindrome(s: String): Boolean { 19 | return s == s.reversed() 20 | } 21 | 22 | // Solution 2 23 | 24 | fun isPalindrome(s: String): Boolean { 25 | var reversed = "" 26 | for(i in s.indices.reversed()) 27 | reversed = "$reversed${s.charAt(i)}" 28 | 29 | return s == reversed 30 | } 31 | 32 | // Solution 3 33 | 34 | fun isPalindrome(s: String): Boolean { 35 | val iterator = s.iterator() 36 | var _reversed = "" 37 | while(iterator.hasNext()){ 38 | val _char = iterator.next() 39 | _reversed = "$_char$_reversed" 40 | } 41 | return s == _reversed 42 | } 43 | -------------------------------------------------------------------------------- /Pairless.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Think of a perfect world where everybody has a soulmate. 3 | * Now, the real world is imperfect: there is exactly one number in the array 4 | * that does not have a pair. A pair is an element with the same value. 5 | * For example in this array: 6 | * 1, 2, 1, 2 7 | * every number has a pair, but in this one: 8 | * 1, 1, 1 9 | * one of the ones is lonely. 10 | * 11 | * Your task is to implement the findPairless() function so that it finds the 12 | * lonely number and returns it. 13 | * 14 | * A hint: there's a solution that looks at each element only once and uses no 15 | * data structures like collections or trees. 16 | */ 17 | package pairless 18 | 19 | fun findPairless(a: IntArray): Int { 20 | // Write your solution here 21 | return 0 22 | } 23 | 24 | /** Solution 1 25 | 26 | INPUT OUTPUT 27 | A B A XOR B 28 | 0 0 0 OK 29 | 0 1 1 30 | 1 0 1 31 | 1 1 0 OK */ 32 | 33 | 34 | fun findPairless(a: IntArray): Int { 35 | return a.reduce { a, b -> a xor b } 36 | } 37 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Kotlin Examples Problems 2 | 3 | --->>> [Repo: Getting Started Kotlin](https://github.com/vicboma1/GettingStartedKotlin) <<<--- 4 | 5 | --->>> [Repo Kotlin Koans](https://github.com/vicboma1/Kotlin-Koans) <<<--- 6 | 7 | --->>> [Repo: GameBoy Emulator Enviroment](https://github.com/vicboma1/GameBoyEmulatorEnvironment) <<<--- 8 | 9 | --->>> [Repo: Kotlin Mobile](https://github.com/vicboma1/KotlinMobilePoC_MasterUV2018) <<<--- 10 | 11 | --->>> [Repo: Kotlin JavaScript](https://github.com/vicboma1/kotlinJavaScript) <<<--- 12 | 13 | --->>> [Repo: Kotlin Native - iOS ](https://github.com/vicboma1/Kotlin-Native-iOS) <<<--- 14 | 15 | --->>> [Repo: Ktor Examples](https://github.com/vicboma1/ktor-API-examples) <<<--- 16 | 17 | 18 | These are the simple solutions of the kotlin example problems ON LINE. If you want to add your answer, you can make a PR. 19 | 20 | ## Indexes for examples problems online 21 | 22 | ### Problems 23 | * [Sum](https://github.com/vicboma1/Kotlin-Examples-Problems#sum---online) 24 | * [Index of Maximum](https://github.com/vicboma1/Kotlin-Examples-Problems#index-of-maximum---online) 25 | * [Runs](https://github.com/vicboma1/Kotlin-Examples-Problems#run---online) 26 | * [Palindrome](https://github.com/vicboma1/Kotlin-Examples-Problems#palindrome---online) 27 | * [Pairless](https://github.com/vicboma1/Kotlin-Examples-Problems#pairless---online) 28 | 29 | # Problems 30 | 31 | ## Sum - [online](http://try.kotlinlang.org/#/Examples/Problems/Sum/Sum.kt) 32 | ``` 33 | 34 | Your task is to implement the sum() function so that it computes the sum of 35 | all elements in the given array a. 36 | 37 | ``` 38 | 39 | Solution 1 40 | ```kotlin 41 | fun sum(a: IntArray): Int { 42 | return a.filter { it != null }.sum() 43 | } 44 | ``` 45 | 46 | Solution 2 47 | ```kotlin 48 | fun sum(a: IntArray): Int { 49 | var _sum = 0 50 | for(element in a) 51 | _sum += element 52 | 53 | return _sum 54 | ``` 55 | 56 | Solution 3 57 | ```kotlin 58 | fun sum(a: IntArray): Int { 59 | val iterator = a.iterator() 60 | var _sum: Int = 0 61 | while (iterator.hasNext()){ 62 | _sum += iterator.next() 63 | } 64 | 65 | return _sum 66 | } 67 | ``` 68 | 69 | ## Index of Maximum - [online](http://try.kotlinlang.org/#/Examples/Problems/Index-of-Maximum/Index-of-Maximum.kt) 70 | ``` 71 | Your task is to implement the indexOfMax() function so that it returns 72 | the index of the largest element in the array, or null if the array is empty 73 | ``` 74 | 75 | Solution 76 | ```kotlin 77 | fun _indexOfMax(a: IntArray): Int? { 78 | var maxIndex = 0 79 | for(elem in a.indices){ 80 | val newElem = a[elem] 81 | if (newElem >= a[maxIndex]){ 82 | maxIndex = elem; 83 | } 84 | } 85 | return maxIndex 86 | } 87 | return if(a.size != 0) _indexOfMax(a) else null 88 | ``` 89 | 90 | ## Runs - [online](http://try.kotlinlang.org/#/Examples/Problems/Runs/Runs.kt) 91 | ``` 92 | Any array may be viewed as a number of "runs" of equal numbers. 93 | For example, the following array has two runs: 94 | 1, 1, 1, 2, 2 95 | Three 1's in a row form the first run, and two 2's form the second. 96 | This array has two runs of length one: 97 | 3, 4 98 | And this one has five runs: 99 | 1, 0, 1, 1, 1, 2, 0, 0, 0, 0, 0, 0, 0 100 | Your task is to implement the runs() function so that it returns the number 101 | of runs in the given array. 102 | ``` 103 | 104 | Solution 105 | ```kotlin 106 | fun runs(a: IntArray): Int { 107 | var res = 0; 108 | if (a.size == res) 109 | return res 110 | 111 | var base = a[res] 112 | a.forEach({ 113 | if(it != base){ 114 | res ++ 115 | base = it 116 | } 117 | }) 118 | 119 | return (++res); 120 | } 121 | ``` 122 | 123 | ## Palindrome - [online](http://try.kotlinlang.org/#/Examples/Problems/Palindrome/Palindrome.kt) 124 | ``` 125 | Your task is to implement a palindrome test. 126 | 127 | A string is called a palindrome when it reads the same way left-to-right 128 | and right-to-left. 129 | ``` 130 | 131 | Solution 1 132 | ```kotlin 133 | fun isPalindrome(s: String): Boolean { 134 | return s == s.reversed() 135 | } 136 | ``` 137 | 138 | Solution 2 139 | ```kotlin 140 | fun isPalindrome(s: String): Boolean { 141 | var reversed = "" 142 | for(i in s.indices.reversed()) 143 | reversed = "$reversed${s.charAt(i)}" 144 | 145 | return s == reversed 146 | } 147 | ``` 148 | 149 | Solution 3 150 | ```kotlin 151 | fun isPalindrome(s: String): Boolean { 152 | val iterator = s.iterator() 153 | var _reversed = "" 154 | while(iterator.hasNext()){ 155 | val _char = iterator.next() 156 | _reversed = "$_char$_reversed" 157 | } 158 | return s == _reversed 159 | } 160 | ``` 161 | 162 | ## Pairless - [online](http://try.kotlinlang.org/#/Examples/Problems/Pairless/Pairless.kt) 163 | ``` 164 | 165 | Think of a perfect world where everybody has a soulmate. 166 | Now, the real world is imperfect: there is exactly one number in the array 167 | that does not have a pair. A pair is an element with the same value. 168 | For example in this array: 169 | 1, 2, 1, 2 170 | every number has a pair, but in this one: 171 | 1, 1, 1 172 | one of the ones is lonely. 173 | 174 | Your task is to implement the findPairless() function so that it finds the 175 | lonely number and returns it. 176 | 177 | A hint: there's a solution that looks at each element only once and uses no 178 | data structures like collections or trees. 179 | 180 | ``` 181 | 182 | Solution 183 | ```kotlin 184 | 185 | INPUT OUTPUT 186 | A B A XOR B 187 | 0 0 0 OK 188 | 0 1 1 189 | 1 0 1 190 | 1 1 0 OK 191 | 192 | 193 | fun findPairless(a: IntArray): Int { 194 | return a.reduce { a, b -> a xor b } 195 | } 196 | ``` 197 | 198 | * @Author: Victor Bolinches Marin 199 | --------------------------------------------------------------------------------