Skip to content

Instantly share code, notes, and snippets.

@siosio
Created May 17, 2012 23:50
Show Gist options
  • Save siosio/2722329 to your computer and use it in GitHub Desktop.
Save siosio/2722329 to your computer and use it in GitHub Desktop.
import java.util.Collection
fun IntRange.fizzBuzz() = this.map {
when (#(it % 3, it % 5)) {
is #(0, 0) -> "fizzbuzz"
is #(0, *) -> "fizz"
is #(*, 0) -> "buzz"
else -> ""
}
}
fun IntRange.findResult(closure : (Int)->Collection<Int>?) : Collection<Int>? {
for (i in this) {
val ret = closure(i)
if (ret != null) {
return ret
}
}
return null;
}
fun toPos(input : Collection<String>) = (1..15).findResult (){
val ret = (it..it + input.size - 1)
if (ret.fizzBuzz() == input) {
ret.toList()
} else {
null
}
} ?: arrayList<Int>()
fun main(args : Array<String>) {
println(toPos(arrayList("fizz"))) // [3]
println(toPos(arrayList("buzz"))) // [5]
println(toPos(arrayList("fizzbuzz"))) // [15]
println(toPos(arrayList("fizz", "buzz"))) // [9, 10]
println(toPos(arrayList("buzz", "fizz"))) // [5, 6]
println(toPos(arrayList("fizz", "", "buzz", "fizz"))) // [3, 4, 5, 6]
println(toPos(arrayList("fizz", "", "", "fizz"))) // [6, 7, 8, 9]
println(toPos(arrayList("fizz", "", "", "fizz", "buzz"))) // [6, 7, 8, 9, 10]
println(toPos(arrayList("fizzbuzz", "", "", "fizz"))) // [15, 16, 17, 18]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment