June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
39
Task/24-game-Solve/Python/24-game-solve-2.py
Normal file
39
Task/24-game-Solve/Python/24-game-solve-2.py
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
import operator
|
||||
from itertools import product, permutations
|
||||
|
||||
def mydiv(n, d):
|
||||
return n / d if d != 0 else 9999999
|
||||
|
||||
syms = [operator.add, operator.sub, operator.mul, mydiv]
|
||||
op = {sym: ch for sym, ch in zip(syms, '+-*/')}
|
||||
|
||||
def solve24(nums):
|
||||
for x, y, z in product(syms, repeat=3):
|
||||
for a, b, c, d in permutations(nums):
|
||||
if round(x(y(a,b),z(c,d)),5) == 24:
|
||||
return f"({a} {op[y]} {b}) {op[x]} ({c} {op[z]} {d})"
|
||||
elif round(x(a,y(b,z(c,d))),5) == 24:
|
||||
return f"{a} {op[x]} ({b} {op[y]} ({c} {op[z]} {d}))"
|
||||
elif round(x(y(z(c,d),b),a),5) == 24:
|
||||
return f"(({c} {op[z]} {d}) {op[y]} {b}) {op[x]} {a}"
|
||||
elif round(x(y(b,z(c,d)),a),5) == 24:
|
||||
return f"({b} {op[y]} ({c} {op[z]} {d})) {op[x]} {a}"
|
||||
return '--Not Found--'
|
||||
|
||||
if __name__ == '__main__':
|
||||
#nums = eval(input('Four integers in the range 1:9 inclusive, separated by commas: '))
|
||||
for nums in [
|
||||
[9,4,4,5],
|
||||
[1,7,2,7],
|
||||
[5,7,5,4],
|
||||
[1,4,6,6],
|
||||
[2,3,7,3],
|
||||
[8,7,9,7],
|
||||
[1,6,2,6],
|
||||
[7,9,4,1],
|
||||
[6,4,2,2],
|
||||
[5,7,9,7],
|
||||
[3,3,8,8], # Difficult case requiring precise division
|
||||
]:
|
||||
print(f"solve24({nums}) -> {solve24(nums)}")
|
||||
44
Task/24-game-Solve/Python/24-game-solve-3.py
Normal file
44
Task/24-game-Solve/Python/24-game-solve-3.py
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Python 3
|
||||
from operator import mul, sub, add
|
||||
|
||||
|
||||
def div(a, b):
|
||||
if b == 0:
|
||||
return 999999.0
|
||||
return a / b
|
||||
|
||||
ops = {mul: '*', div: '/', sub: '-', add: '+'}
|
||||
|
||||
def solve24(num, how, target):
|
||||
if len(num) == 1:
|
||||
if round(num[0], 5) == round(target, 5):
|
||||
yield str(how[0]).replace(',', '').replace("'", '')
|
||||
else:
|
||||
for i, n1 in enumerate(num):
|
||||
for j, n2 in enumerate(num):
|
||||
if i != j:
|
||||
for op in ops:
|
||||
new_num = [n for k, n in enumerate(num) if k != i and k != j] + [op(n1, n2)]
|
||||
new_how = [h for k, h in enumerate(how) if k != i and k != j] + [(how[i], ops[op], how[j])]
|
||||
yield from solve24(new_num, new_how, target)
|
||||
|
||||
tests = [
|
||||
[1, 7, 2, 7],
|
||||
[5, 7, 5, 4],
|
||||
[1, 4, 6, 6],
|
||||
[2, 3, 7, 3],
|
||||
[1, 6, 2, 6],
|
||||
[7, 9, 4, 1],
|
||||
[6, 4, 2, 2],
|
||||
[5, 7, 9, 7],
|
||||
[3, 3, 8, 8], # Difficult case requiring precise division
|
||||
[8, 7, 9, 7], # No solution
|
||||
[9, 4, 4, 5], # No solution
|
||||
]
|
||||
for nums in tests:
|
||||
print(nums, end=' : ')
|
||||
try:
|
||||
print(next(solve24(nums, nums, 24)))
|
||||
except StopIteration:
|
||||
print("No solution found")
|
||||
Loading…
Add table
Add a link
Reference in a new issue