0% found this document useful (0 votes)
15 views10 pages

Solve the 24 Game with Code

The 24 Game is a card game where players use four numbers and basic arithmetic operations to create an equation that equals 24. The document includes examples of valid equations and outlines a code snippet that calculates all possible operations for a given set of numbers to determine if a combination can make 24. It also highlights that while most combinations can achieve 24, some are impossible.

Uploaded by

Zlippy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views10 pages

Solve the 24 Game with Code

The 24 Game is a card game where players use four numbers and basic arithmetic operations to create an equation that equals 24. The document includes examples of valid equations and outlines a code snippet that calculates all possible operations for a given set of numbers to determine if a combination can make 24. It also highlights that while most combinations can achieve 24, some are impossible.

Uploaded by

Zlippy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

## The 24 Game is a card game where

players use four given numbers

(typically 1-9, although the 10, J, Q,

K cards can also be included, with the

latter three standing for 11, 12, and

13 respectively) and basic arithmetic

operations (+, -, ×, ÷) to create an

equation that equals 24, using the four

numbers exactly once.

## P.S. Lineups such as “1, 3, 3, 4” or

“5, 5, 12, 12” are valid, provided each

duplicate is only used once, like this:

# (1 × 3 + 3) × 4 = 24 or
# (3 - 1) × (3 × 4) = 24 or

# 5 ÷ 5 × (12 + 12) or

# (5 ÷ 5) × (12 + 12) = 24.

## The game can be played with the

objective of being the fastest to come

up with a valid equation, or of coming

up with the most number of valid

equations.

## While most combinations of 1-13 can

create 24, there are some combinations

that are simply impossible.


## The following code helps calculate

all possible operations for a given set

of numbers, and will tell you when a

combination cannot make 24.

## START HERE:

from itertools import permutations,

product
n1 = int(input("Please insert the first

number: "))

n2 = int(input("Please insert the

second number: "))

n3 = int(input("Please insert the third

number: "))

n4 = int(input("Please insert the last

number: "))

num_list = [n1, n2, n3, n4]

operations = ['+', '-', '*', '/']

exp_list = []
for permutation in

permutations(num_list):

num1 = permutation[0]

num2 = permutation[1]

num3 = permutation[2]

num4 = permutation[3]

for op1 in operations:

for op2 in operations:

for op3 in operations:

expressions = ["({0}{1}

{2}){3}({4}{5}{6})".format(num1, op1,

num2, op2, num3, op3, num4),


"(({0}

{1}{2}){3}{4}){5}{6}".format(num1, op1,

num2, op2, num3, op3, num4),

# if not

including fraction, the expression

below can be omitted.

"{0}{1}

({2}{3}({4}{5}{6}))".format(num1, op1,

num2, op2, num3, op3, num4)

for exp in expressions:

try:
if

abs(eval(exp) - 24) < 1e-10:

exp_list.append(exp)

except

ZeroDivisionError:

pass

'''

# SHORTCUT

for nums in permutations(num_list):

for ops in product(operations,

repeat=3):
expressions = ["({0}{4}{1}){5}

({2}{6}{3})".format(*nums, *ops),

"(({0}{4}{1}){5}

{2}){6}{3}".format(*nums, *ops),

# if not

including fraction, the expression

below can be omitted.

"{0}{4}({1}{5}

({2}{6}{3}))".format(*nums, *ops)]

for exp in expressions:

try:
if abs(eval(exp) - 24)

< 1e-10:

exp_list.append(exp)

except ZeroDivisionError:

pass

'''

# remove duplicates

exp_list = list(set(exp_list))

if len(exp_list)==0:

print("No Solution!")

else:
print(exp_list)

You might also like