Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save WhiteBlue/a6bea495cb9a71b38431106b42f24c1d to your computer and use it in GitHub Desktop.
Save WhiteBlue/a6bea495cb9a71b38431106b42f24c1d to your computer and use it in GitHub Desktop.
24 number
# coding=utf-8
import copy
op_method = ['+', '-', '*', '/']
def copy_arr(old, e):
new_buf = copy.copy(old)
new_buf.append(e)
return new_buf
def check_val(buf):
code_val = ''
for i in buf:
code_val += str(i)
if eval(code_val) == 24:
print code_val
def get_num(buf):
if len(buf) < 7:
if len(buf) != 0 and (len(buf) - 1) % 2 == 0:
for i in op_method:
get_num(copy_arr(buf, i))
else:
for i in range(0, 9):
if i in buf:
continue
if len(buf) != 0 and buf[len(buf) - 1] is '/':
before_val = buf[len(buf) - 2]
if i == 0 or before_val % i != 0:
continue
get_num(copy_arr(buf, i))
else:
check_val(buf)
if __name__ == '__main__':
get_num([])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment