Created
July 29, 2020 12:12
-
-
Save houssemzaier/13626d13fa88051e239cfc886b8ac5ca 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 given an array (which will have a length of at least 3, but could be very large) containing integers. The array is either entirely comprised of odd integers or entirely comprised of even integers except for a single integer N. Write a method that takes the array as an argument and returns this "outlier" N. | |
fun find(integers: Array<Int>): Int { | |
var outlierInteger: Int = 0 | |
val oddIntegers = mutableListOf<Int>() | |
val evenIntegers = mutableListOf<Int>() | |
var i = 0 | |
while (i <= integers.lastIndex) { | |
val integer = integers[i] | |
if (integer.isEven()) { | |
evenIntegers += integer | |
} else { | |
oddIntegers += integer | |
} | |
if (evenIntegers.size >= 2 && oddIntegers.isNotEmpty()) { | |
outlierInteger = oddIntegers.first() | |
break | |
} | |
if (oddIntegers.size >= 2 && evenIntegers.isNotEmpty()) { | |
outlierInteger = evenIntegers.first() | |
break | |
} | |
++i | |
} | |
if (i > integers.lastIndex) { | |
throw IllegalStateException("no outlierInteger was found") | |
} | |
return outlierInteger | |
} | |
private fun Int.isEven() = rem(2) == 0 | |
fun main() { | |
val exampleTest1 = arrayOf(2, 6, 8, -10, 3) | |
val exampleTest2 = arrayOf(206847684, 1056521, 7, 17, 1901, 21104421, 7, 1, 35521, 1, 7781) | |
val exampleTest3 = arrayOf(Integer.MAX_VALUE, 0, 1) | |
assertEquals(3, find(exampleTest1)) | |
assertEquals(206847684, find(exampleTest2)) | |
assertEquals(0, find(exampleTest3)) | |
} | |
fun assertEquals(i: Int, find: Int): 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