Skip to content

Instantly share code, notes, and snippets.

@rummelonp
Created May 16, 2012 03:40
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rummelonp/2707145 to your computer and use it in GitHub Desktop.
Save rummelonp/2707145 to your computer and use it in GitHub Desktop.
逆FizzBuzz問題 (Inverse FizzBuzz) http://d.hatena.ne.jp/matarillo/20120515/p1
# -*- coding: utf-8 -*-
class Integer
def to_fizzbuzz
[[:fizz][self % 3], [:buzz][self % 5]] * ''
end
end
class String
def present?
size > 0
end
end
module InverseFizzBuzz
RANGE = 1..100
module_function
def start(list)
list.size.upto(RANGE.last) do |size|
answer = RANGE.each_cons(size).find do |cons|
cons.map(&:to_fizzbuzz).select(&:present?) == list
end
break p answer if answer
end
end
end
InverseFizzBuzz.start(ARGV)
$*.size.upto(100){|s|a=(1..100).each_cons(s).find{|c|c.map{|i|[[:fizz][i%3],[:buzz][i%5]]*''}.select{|s|s.size>0}==$*};break p a if a}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment