Skip to content

Instantly share code, notes, and snippets.

@timothyandrew
Created May 5, 2012 07:27
Show Gist options
  • Save timothyandrew/2600633 to your computer and use it in GitHub Desktop.
Save timothyandrew/2600633 to your computer and use it in GitHub Desktop.
Inverse Fizzbuzz
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