Skip to content

Instantly share code, notes, and snippets.

@masui
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masui/8a5a6ae7e5bb7acf83ee to your computer and use it in GitHub Desktop.
Save masui/8a5a6ae7e5bb7acf83ee to your computer and use it in GitHub Desktop.
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