This file contains 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
/** | |
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) | |
} | |
} | |
} |
No comments:
Post a Comment