How to resolve the algorithm 24 game/Solve step by step in the Ruby programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm 24 game/Solve step by step in the Ruby programming language
Table of Contents
Problem Statement
Write a program that takes four digits, either from user input or by random generation, and computes arithmetic expressions following the rules of the 24 game. Show examples of solutions generated by the program.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm 24 game/Solve step by step in the Ruby programming language
The provided Ruby code determines solutions to the 24 game, where the goal is to use mathematical operations to combine four provided integers to equal 24.
-
Class Definition (TwentyFourGame):
- The TwentyFourGame class encapsulates the game logic.
-
EXPRESSIONS:
- An array of expression templates with placeholders for integers and operators.
-
OPERATORS:
- An array of all permutations of mathematical operators (+, -, *, /).
-
Solve Method:
- It takes a list of digits as input.
- Generates all unique permutations of the digits.
- Iterates over the permutations, operator permutations, and expression templates.
- For each combination, it constructs an expression using string interpolation and evaluates it using rational arithmetic to prevent division by zero.
- If the result equals 24, it adds the expression to the solutions array.
-
Main Logic:
- Validates user-provided digits as integers and checks if there are exactly four.
- Calls the TwentyFourGame.solve method to find solutions.
- Prints the number of solutions found and the first solution.
- Sorts and prints all solutions.
Source code in the ruby programming language
class TwentyFourGame
EXPRESSIONS = [
'((%dr %s %dr) %s %dr) %s %dr',
'(%dr %s (%dr %s %dr)) %s %dr',
'(%dr %s %dr) %s (%dr %s %dr)',
'%dr %s ((%dr %s %dr) %s %dr)',
'%dr %s (%dr %s (%dr %s %dr))',
]
OPERATORS = [:+, :-, :*, :/].repeated_permutation(3).to_a
def self.solve(digits)
solutions = []
perms = digits.permutation.to_a.uniq
perms.product(OPERATORS, EXPRESSIONS) do |(a,b,c,d), (op1,op2,op3), expr|
# evaluate using rational arithmetic
text = expr % [a, op1, b, op2, c, op3, d]
value = eval(text) rescue next # catch division by zero
solutions << text.delete("r") if value == 24
end
solutions
end
end
# validate user input
digits = ARGV.map do |arg|
begin
Integer(arg)
rescue ArgumentError
raise "error: not an integer: '#{arg}'"
end
end
digits.size == 4 or raise "error: need 4 digits, only have #{digits.size}"
solutions = TwentyFourGame.solve(digits)
if solutions.empty?
puts "no solutions"
else
puts "found #{solutions.size} solutions, including #{solutions.first}"
puts solutions.sort
end
You may also check:How to resolve the algorithm Compile-time calculation step by step in the Rust programming language
You may also check:How to resolve the algorithm Averages/Arithmetic mean step by step in the Maple programming language
You may also check:How to resolve the algorithm XML/Input step by step in the ARM Assembly programming language
You may also check:How to resolve the algorithm Greatest element of a list step by step in the Run BASIC programming language
You may also check:How to resolve the algorithm Circular primes step by step in the Sidef programming language