A has invented a new card game to play with B. A made a deck of cards with random values between 1 and 52. B picks 5 cards. Then, he has to rearrange the cards so that by utilizing the operations plus, minus, or times, the value of the cards reach A's favorite number, 42. More precisely, find operations such that ((((val1 op1 val2) op2 val3) op3 val4) op4 val5) = 42.
Print "YES" if it is possible to reach the value 42 according to the rules of the game, or "NO" otherwise.
How to solve this problem in any coding language?
Code :
from random import sample
import itertools
#creates a deck of cards having numbers from 1 to 52
deck_cards = []
for i in range(1,53):
deck_cards.append(i)
#randomly selects 5 numbers from the created deck of cards
nums = sample(deck_cards,5)
print(nums)
#operands list
ops = ['+','-','*']
a = []
for p in itertools.permutations(nums,len(nums)):
for op1 in ops:
for op2 in ops:
for op3 in ops:
for op4 in ops:
expr = 'p[0]'+op1+'p[1]'+op2+'p[2]'+op3+'p[3]'+op4+'p[4]'
result = eval(expr)
a.append(result)
#if 42 is produced and stored then prints YES else NO
if 42 in a:
print("YES")
else:
print("NO")
Screenshot of Code :
Screenshot of OUTPUT :
Get Answers For Free
Most questions answered within 1 hours.