Created
July 29, 2020 12:11
-
-
Save houssemzaier/d78ba5487b87f1cedc90d5c40c090382 to your computer and use it in GitHub Desktop.
algo test for arc() dev
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fr.francetv.francetvsport.arch.infrastructure.data.source.remote.pic | |
//You are going to be given a word. Your job is to return the middle character of the word. If the word's length is odd, return the middle character. If the word's length is even, return the middle 2 characters. | |
fun getMiddle(word: String): String { | |
val middleIndex = word.length / 2 | |
return if (word.length.isEven()) { | |
word.substring((middleIndex - 1)..middleIndex) | |
} else { | |
word[middleIndex].toString() | |
} | |
} | |
private fun Int.isEven() = rem(2) == 0 | |
fun main() { | |
assertEquals(getMiddle("test"), "es") | |
assertEquals(getMiddle("testing"), "t") | |
assertEquals(getMiddle("middle"), "dd") | |
} | |
fun assertEquals(i: String, find: String): Boolean { | |
if (i != find) throw Exception("$i not equal $find") | |
return true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment