Created
May 25, 2015 15:44
-
-
Save masui/b688b4275f5d59fea6ec to your computer and use it in GitHub Desktop.
1時間以内に解けなければプログラマ失格となってしまう5つの問題
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
# | |
# https://blog.svpino.com/2015/05/08/solution-to-problem-5-and-some-other-thoughts-about-this-type-of-questions | |
# | |
def power(level, a, &block) | |
if level > 0 then | |
['+', '-', ''].each { |op| | |
a[level-1] = op | |
power level-1, a, &block | |
} | |
else | |
yield a | |
end | |
end | |
power(8,[]){ |arg| | |
s = '1' | |
(2..9).each { |i| | |
s += arg[i-2] | |
s += i.to_s | |
} | |
val = eval(s) | |
puts "#{s} ==> #{val}" if val == 100 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment