Created
May 16, 2012 03:40
-
-
Save rummelonp/2707145 to your computer and use it in GitHub Desktop.
逆FizzBuzz問題 (Inverse FizzBuzz) http://d.hatena.ne.jp/matarillo/20120515/p1
This file contains hidden or 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
# -*- 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) |
This file contains hidden or 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
$*.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