If you're interested in functional programming, you might also want to checkout my second blog which i'm actively working on!!

Friday, September 27, 2013

Printing only numbers with non-repeating digits

/**
Print From 45 to 4578 Without Repeating Digits
Write a method that prints all the numbers from 45 to 4578 to the console,
with the constraint that you cannot print any number with repeating digits.
This means the following numbers would be skipped:
11, 22, ...., 122, 110,....
**/
package puzzlers
object NumberPrinter {
def nonRepeatingDigitNumbers(start:Int, end:Int): IndexedSeq[Int] = {
(start to end).filterNot(x => x.toString.toCharArray().groupBy(identity).exists(_._2.size > 1))
}
def main(args: Array[String]): Unit = {
for (number <- nonRepeatingDigitNumbers(45, 4578)) {
println(number)
}
}
}
view raw gistfile1.scala hosted with ❤ by GitHub

No comments:

Post a Comment