Created
May 5, 2012 07:27
-
-
Save timothyandrew/2600633 to your computer and use it in GitHub Desktop.
Inverse Fizzbuzz
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
def fizzbuzz(n) | |
str = "" | |
str << (n % 3 == 0 ? "fizz" : "") | |
str << (n % 5 == 0 ? "buzz" : "") | |
end | |
list = ["fizz", "fizz", "buzz"] | |
arr = (1..100).map { |n| [n, fizzbuzz(n)] }.reject { |h| h.last.empty? } | |
res = arr.each_slice(list.length).to_a.find_all do |slice| | |
slice.map { |s| s.last }.join == list.join | |
end.sort_by { |slice| slice.size } | |
if res.empty? | |
puts "Invalid" | |
exit | |
else | |
res = res.first | |
end | |
(res.first.first..res.last.first).each do |n| | |
if not res.find { |x| x.first == n } | |
res << [n, nil] | |
end | |
end | |
puts res.sort_by { |slice| slice.first }.inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment