Skip to content

Instantly share code, notes, and snippets.

@pocari
Last active August 29, 2015 14:21
Show Gist options
  • Save pocari/f643c740d5e36837d700 to your computer and use it in GitHub Desktop.
Save pocari/f643c740d5e36837d700 to your computer and use it in GitHub Desktop.
1時間以内に解けなければプログラマ失格となってしまう5つの問題 ref: http://qiita.com/pocari/items/8b9f6b31dfe551e39693
echo 5 2 1 9 50 56 | ruby -e "puts gets.split(/\s+/).permutation.map(&:join).max"
def gen_pattern(depth, size, acc = [])
if depth >= size
exp = (1 .. (size + 1)).zip(acc).flatten.join.gsub(/ /, "").gsub(/(\+|-)/, ' \1 ')
puts exp if eval(exp) == 100
else
[" ", "+", "-"].each do |op|
gen_pattern(depth + 1, size, acc + [op])
end
end
end
gen_pattern(0, 8)
puts [" ", "+", "-"].repeated_permutation(8).map{|e|
(1 .. 9).zip(e).flatten.join.gsub(/ /, '').gsub(/(\+|-)/, ' \1 ')
}.select {|e|
eval(e) == 100
}
[["1", "2", ["3"]], ["4", "5"]].join
#=> "12345"
irb(main):001:0> ary = [1, 2, 3]
=> [1, 2, 3]
irb(main):002:0> ary[0] = ary
=> [[...], 2, 3]
irb(main):003:0> ary[0][1]
=> 2
irb(main):004:0> ary[0][0][1]
=> 2
irb(main):005:0> ary[0][0][0][1]
=> 2
irb(main):006:0> ary.join
ArgumentError: recursive array join
from (irb):6:in `join'
from (irb):6
from D:/Ruby21-x64/bin/irb:11:in `<main>'
puts [" ", "+", "-"].repeated_permutation(8).map{|e|
(1 .. 9).zip(e).join.gsub(/ /, '').gsub(/(\+|-)/, ' \1 ')
}.select {|e|
eval(e) == 100
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment