Last active
July 2, 2016 08:52
-
-
Save shakemurasan/a84ab9de9e8faccc1ef9240601bbc7ec to your computer and use it in GitHub Desktop.
プログラマ脳を鍛える数学パズル:Q02
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
# -- 処理時間計測の自作ライブラリ -- | |
require './process_measure.rb' | |
# ------------------------------ | |
def print_enable_combinarion(from, to) | |
(from..to).each do |num| | |
nums = num.to_s.split('') | |
puts num if enable_combinarion?(nums) | |
end | |
end | |
def enable_combinarion?(nums) | |
combi(nums).each do |f| | |
# この辺のガードが苦しいし無駄.... | |
next if f =~ /0[1-9]/ | |
next if f =~ /\/0/ | |
next if f !~ /[\+\-\*\/]/ | |
return true if eval(f).to_s == (nums.join('')).reverse | |
end | |
return false | |
end | |
def combi(a) | |
res_array = [a[0]] | |
res_array = add_sign_and_num(res_array, a[1]) | |
res_array = add_sign_and_num(res_array, a[2]) | |
res_array = add_sign_and_num(res_array, a[3]) | |
res_array | |
end | |
def add_sign_and_num(a, num) | |
a.product(["+", "-", "*", "/", ""]).map {|m| "#{m.join('')}#{num}" } | |
end | |
measure_do { print_enable_combinarion(1000, 9999) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment