Last active
August 29, 2015 14:21
-
-
Save pocari/f643c740d5e36837d700 to your computer and use it in GitHub Desktop.
1時間以内に解けなければプログラマ失格となってしまう5つの問題 ref: http://qiita.com/pocari/items/8b9f6b31dfe551e39693
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
echo 5 2 1 9 50 56 | ruby -e "puts gets.split(/\s+/).permutation.map(&:join).max" |
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
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) |
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
puts [" ", "+", "-"].repeated_permutation(8).map{|e| | |
(1 .. 9).zip(e).flatten.join.gsub(/ /, '').gsub(/(\+|-)/, ' \1 ') | |
}.select {|e| | |
eval(e) == 100 | |
} |
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
[["1", "2", ["3"]], ["4", "5"]].join | |
#=> "12345" |
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
irb(main):001:0> ary = [1, 2, 3] | |
=> [1, 2, 3] | |
irb(main):002:0> ary[0] = ary | |
=> [[...], 2, 3] |
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
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>' |
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
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