Jump to content

24 game/Solve: Difference between revisions

m (alphabetized)
Line 240:
{{trans|Tcl}}
<lang ruby>class TwentyFourGamePlayer
EXPRESSIONS = [
'a.send(op1, b).send(op2, c).send(op3, d)', # '((a op1 b) op2 c) op3 d',
'a.send(op1, b.send(op2, c)).send(op3, d)', # '(a op1 (b op2 c)) op3 d',
'a.send(op1, b).send(op2, c.send(op3, d))', # '(a op1 b) op2 (c op3 d)',
'a.send(op1, b.send(op2, c).send(op3, d))', # 'a op1 ((b op2 c) op3 d)',
'a.send(op1, b.send(op2, c.send(op3, d)))', # 'a op1 (b op2 (c op3 d))',
]
PRINT_FORMATS = [
'((%d %s %d) %s %d) %s %d',
Line 271 ⟶ 264:
OPERATORS.each do |op2|
OPERATORS.each do |op3|
EXPRESSIONSPRINT_FORMATS.each_with_indexeach do |expr, i|
if# eval(expr)change ==to @@objectivefloats so it does floating-point division
test = @solutionsexpr.gsub('%d', << PRINT_FORMATS[i]'%f') % [a, op1, b, op2, c, op3, d]
if eval(test) == @@objective
@solutions << expr % [a, op1, b, op2, c, op3, d]
end
end
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.