程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[computer test questions (implementation language: python3)] 24 point operation ----itertools

編輯:Python

Title Description
Calculation 24 Point is a poker puzzle game , Draw at random 4 A playing card , By adding (+), reduce (-), ride (*), except (/) Four algorithms calculate integers 24, In this question , Playing cards are represented by the following characters or strings , among , A lowercase letter joker It means Xiao Wang , Capitalization JOKER For the king :

3 4 5 6 7 8 9 10 J Q K A 2 joker JOKER

This procedure requires the realization of : Input 4 card , Output a formula , The result of the formula is 24 spot .

Detailed instructions :

1. Operation only considers addition, subtraction, multiplication and division , No special operation symbols such as factorials , There are no brackets , A friendly reminder , Be careful with integer division , It belongs to division , such as 2/3=0,3/2=1;
2. Card face 210 The corresponding weight is zero 210, J、Q、K、A The weights are 11、12、13、1;
3. Input 4 Cards are in string form , Separated by a space , There is no space at the beginning and end ; If you enter 4 Cards contain big and small kings , Then the output string “ERROR”, Indicates that the operation cannot be performed ;
4. The output formula format is 4 Card pass ±/ Four operators are connected , No space in the middle ,4 Cards appear in any order , As long as the result is correct ;
5. The operation sequence of the output formula is from left to right , Do not include parentheses , Such as 1+2+3
4 As the result of the 24,2 A 9 A It can't be (2+1)*(9-1)=24
6. If there are many formulas, you can calculate 24, Just output one , If you can't get 24, The output “NONE” There is no solution .
7. Because it's all playing cards , No single card is 0 The situation of , And no bracket operation , Divisor ( The denominator ) The number of cannot be 0

Input description :

 Input 4 Cards are in string form , Separated by a space , There is no space at the beginning and end ;

Output description :

 How to calculate the output to get 24, If you can't get 24, The output “NONE” There is no solution , If you enter 4 Cards contain big and small kings , Then the output string “ERROR”, Indicates that the operation cannot be performed ;

Example 1
Input

A A A A

Output

NONE
Description cannot be implemented

Example 2
Input

4 2 K A

Output

K-A*4/2
explain
A+K*2-4 It's also an answer , Output any one of them

Example 3
Input

B5 joker 4

Output

ERROR
explain
There is joker, Output ERROR

Example 4
Input

K Q 6 K

Output

NONE
explain
According to the general calculation rules ,K+K-(Q/6)=24, But because the operation of this topic is not allowed to have parentheses , So only for K+K-Q/6=2 , In other cases, we can't calculate 24 spot , So there is no , Output NONE

The code implementation is as follows :

import itertools
import re
def func():
s = {'A':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,
'9':9,'10':10,'J':11,'Q':12,'K':13}
while True:
try:
a = input().split()
if 'joker' in a or 'JOKER' in a:
print('ERROR')
continue
flag = False
str1 = ''
for i in itertools.permutations(a):
#print(i)
b = tuple([s[kk] for kk in i])
#print(b)
for j in itertools.product('+-*/',repeat=3):
str1 = '(({0}{4}{1}){5}{2}){6}{3}'.format(*b,*j)
#print(str1)
if eval(str1) == 24:
flag = True
if 'A' in i or 'J' in i or 'Q' in i or 'K' in i:
str1 = re.sub(r'11','J',str1)
str1 = re.sub(r'12','Q',str1)
str1 = re.sub(r'13','K',str1)
str1 = re.sub(r'([^0-9])1([^0-9])',r'\1A\2',str1)
str1 = re.sub(r'^(1)([^0-9])',r'A\2',str1)
str1 = re.sub(r'([^0-9])1$',r'\1A',str1)
break
if flag:
break
if not flag:
print('NONE')
else:
str1 = str1.replace('(','')
str1 = str1.replace(')','')
print(str1)
except Exception as e:
#print(e)
break
if __name__ == '__main__':
func()

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved