Created
May 12, 2018 17:35
-
-
Save WhiteBlue/a6bea495cb9a71b38431106b42f24c1d to your computer and use it in GitHub Desktop.
24 number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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