Skip to content

Instantly share code, notes, and snippets.

@cokia
Created July 22, 2019 11:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cokia/30b4951cfd604c5ac3caa86c372eb371 to your computer and use it in GitHub Desktop.
Save cokia/30b4951cfd604c5ac3caa86c372eb371 to your computer and use it in GitHub Desktop.
import sys
def calc(num1, num2, op):
if op == 0:
return num1 + num2
elif op == 1:
return num1 - num2
elif op == 2:
return num1 * num2
elif op == 3:
if num2 == 0:
error('0으로 나눠짐')
else:
return num1 / num2
else:
error('숫자안나옴')
def char4(candi, dict):
for a in range(4):
if a == 3 and candi[1] == 0:
continue
res1 = calc(candi[0], candi[1], a)
for b in range(4):
if b == 3 and candi[2] == 0:
continue
res2 = calc(res1, candi[2], b)
for c in range(4):
if c == 3 and candi[3] == 0:
continue
res3 = calc(res2, candi[3], c)
if abs(res3 - 24) < 0.01:
if (a == 0 or a == 1) and (b == 2 or b == 3):
print('('+str(int(candi[0]))+dict[a]+str(int(candi[1]))+')' \
+dict[b]+str(int(candi[2]))+dict[c]+str(int(candi[3])))
elif (b == 0 or b == 1) and (c == 2 or c == 3):
print('('+str(int(candi[0]))+dict[a]+str(int(candi[1])) \
+dict[b]+str(int(candi[2]))+')'+dict[c]+str(int(candi[3])))
else:
print(str(int(candi[0]))+dict[a]+str(int(candi[1])) \
+dict[b]+str(int(candi[2]))+dict[c]+str(int(candi[3])))
return True
for a in range(4):
if a == 3 and candi[2] == 0:
continue
res1 = calc(candi[1], candi[2], a)
for b in range(4):
if b == 3 and res1 == 0:
continue
res2 = calc(candi[0], res1, b)
for c in range(4):
if c == 3 and candi[3] == 0:
continue
res3 = calc(res2, candi[3], c)
if abs(res3 - 24) < 0.01:
if (a == 0 or a == 1):
if (b == 0 or b == 1) and (c == 2 or c == 3):
print('('+str(int(candi[0]))+dict[b]+'('+str(int(candi[1])) \
+dict[a]+str(int(candi[2]))+'))'+dict[c]+str(int(candi[3])))
else:
print(str(int(candi[0]))+dict[b]+'('+str(int(candi[1])) \
+dict[a]+str(int(candi[2]))+')'+dict[c]+str(int(candi[3])))
else:
if (b == 0 or b == 1) and (c == 2 or c == 3):
print('('+str(int(candi[0]))+dict[b]+str(int(candi[1])) \
+dict[a]+str(int(candi[2]))+')'+dict[c]+str(int(candi[3])))
else:
print(str(int(candi[0]))+dict[b]+str(int(candi[1])) \
+dict[a]+str(int(candi[2]))+dict[c]+str(int(candi[3])))
return True
for a in range(4):
if a == 3 and candi[2] == 0:
continue
res1 = calc(candi[1], candi[2], a)
for b in range(4):
if b == 3 and candi[3] == 0:
continue
res2 = calc(res1, candi[3], b)
for c in range(4):
if c == 3 and res2 == 0:
continue
res3 = calc(candi[0], res2, c)
if abs(res3 - 24) < 0.01:
print(str(int(candi[0]))+dict[c]+'(('+str(int(candi[1]))+dict[a] \
+str(int(candi[2]))+')'+dict[b]+str(int(candi[3]))+')')
return True
for a in range(4):
if a == 3 and candi[1] == 0:
continue
res1 = calc(candi[0], candi[1], a)
for b in range(4):
if b == 3 and candi[3] == 0:
continue
res2 = calc(candi[2], candi[3], b)
for c in range(4):
if c == 3 and res2 == 0:
continue
res3 = calc(res1, res2, c)
if abs(res3 - 24) < 0.01:
print('('+str(int(candi[0]))+dict[a]+str(int(candi[1]))+')'+dict[c] \
+'('+str(int(candi[2]))+dict[b]+str(int(candi[3]))+')')
return True
for a in range(4):
if a == 3 and candi[3] == 0:
continue
res1 = calc(candi[2], candi[3], a)
for b in range(4):
if b == 3 and res1 == 0:
continue
res2 = calc(candi[1], res1, b)
for c in range(4):
if c == 3 and res2 == 0:
continue
res3 = calc(candi[0], res2, c)
if abs(res3 - 24) < 0.01:
print('('+str(int(candi[0]))+dict[c]+'('+str(int(candi[1]))+dict[b] \
+'('+str(int(candi[2]))+dict[a]+str(int(candi[3]))+')))')
return True
return False
if __name__ == '__main__':
nums = input().split(" ")
for i in range(len(nums)):
nums[i] = float(nums[i])
dict = {0:'+', 1:'-', 2:'*', 3:'/'}
set = set()
candi = [None] * 4
for i in range(4):
candi[0] = nums[i]
set.add(i)
for j in range(4):
if j in set:
continue
candi[1] = nums[j]
set.add(j)
for k in range(4):
if k in set:
continue
candi[2] = nums[k]
set.add(k)
for l in range(4):
if l in set:
continue
candi[3] = nums[l]
set.add(l)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment