Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
1時間以内に解けなければプログラマ失格となってしまう5つの問題(3)
#
# https://blog.svpino.com/2015/05/08/solution-to-problem-5-and-some-other-thoughts-about-this-type-of-questions
#
def power(len, sels, arr=[], &block)
if len == 0 then
yield arr
else
sels.to_a.each { |sel|
power len-1, sels, arr + [sel], &block
}
end
end
power(8,['+','-','']){ |arg|
# arg = ['', '+', '+', '-', '', '+', '-', ''] => exp = '12+3+4-56+7-89'
exp = ""
(1..9).each_with_index{ |v,i|
exp += v.to_s
exp += arg[i].to_s
}
val = eval(exp)
puts "#{exp} ==> #{val}" if val == 100
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.