Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if (ARGV.length != 4)
- puts 'Usage: ruby comp24.rb <num1> <num2> <num3> <num4>'
- exit
- end
- input = ARGV.map {|s| s.to_f }
- puts "Number List: #{input.inspect}"
- #now start...
- results=[]
- lists = input.permutation(4).to_a
- def for24(list,results)
- ops=['+','-','*','/']
- ops.each do |op|
- exp1="(#{list[0]}#{op}#{list[1]})"
- if(list.length == 2)
- results << exp1 if eval(exp1)==24
- else
- for24([exp1].concat(list[2,4]),results) if (0..24).to_a.include?(eval(exp1))
- if list.length == 3
- exp2="(#{list[1]}#{op}#{list[2]})"
- for24([list[0],exp2],results) if (0..24).to_a.include?(eval(exp2))
- end
- end
- end
- end
- lists.each do |list|
- for24(list,results)
- end
- #compute end....
- #puts results.inspect
- if (results.length == 0)
- puts "No solution!"
- else
- puts "Solutions:"
- results.sort.uniq.each {|r| puts r.gsub(/\.0/,'')[1..-2]}
- end
Add Comment
Please, Sign In to add comment