Skip to content

Instantly share code, notes, and snippets.

@houssemzaier
Created July 29, 2020 12:11
Show Gist options
  • Save houssemzaier/d78ba5487b87f1cedc90d5c40c090382 to your computer and use it in GitHub Desktop.
Save houssemzaier/d78ba5487b87f1cedc90d5c40c090382 to your computer and use it in GitHub Desktop.
algo test for arc() dev
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