Skip to content

Instantly share code, notes, and snippets.

@BGR360
Created January 22, 2016 17:54
Show Gist options
  • Save BGR360/6bd69073255e09426f54 to your computer and use it in GitHub Desktop.
Save BGR360/6bd69073255e09426f54 to your computer and use it in GitHub Desktop.
Excerpt from TwentyFourSolver
def solve(numbers, value):
"""
Uses arithmetic (*, +, -, /) to arrive at value, using my custom recursive algorithm
:param numbers: The list of numbers we're going to use to arrive at value
:param value: The value that we want to arrive at using all of the
:return: If solvable, returns a Solution instance. If not, returns False.
"""
# Referring to the global variables we want to modify
global num_attempts
global current_attempt
# Begin my algorithm
solution = Solution()
n = len(numbers)
# print "Solve %s for %s" % (numbers, value)
# 1. If n = 1
if n < 1:
return False
if n == 1:
num_attempts += 1
if numbers[0] == value:
solution.numbers = [value]
solution.operations = []
return solution
else:
return False
# 2. If n = 2:
if n == 2:
# a) Try multiplying the two numbers
num_attempts += 1
if numbers[0] * numbers[1] == value:
solution.numbers = numbers
solution.operations = [MUL]
return solution
# b) Try adding the two numbers
num_attempts += 1
if numbers[0] + numbers[1] == value:
solution.numbers = numbers
solution.operations = [ADD]
return solution
# Find the larger and the smaller number of the two
smaller = numbers[0]
larger = numbers[1]
if smaller > larger:
smaller = numbers[1]
larger = numbers[0]
# c) If x >= 0
if value >= 0:
# i) Try subtracting the smaller from the larger
num_attempts += 1
if larger - smaller == value:
solution.numbers = [larger, smaller]
solution.operations = [SUB]
return solution
else:
# ii) Else try subtracting the larger from the smaller
num_attempts += 1
if smaller - larger == value:
solution.numbers = [smaller, larger]
solution.operations = [SUB]
return solution
# d) Try dividing the larger by the smaller
num_attempts += 1
if float(larger) / smaller == value:
solution.numbers = [larger, smaller]
solution.operations = [DIV]
return solution
# e) If any of those work, then yes. If not, then no solution.
return False
# 3. Try adding all the numbers together.
num_attempts += 1
if sum(numbers) == value:
solution.numbers = numbers
for i in range(n - 1):
solution.operations.append(ADD)
return solution
# 4. Try multiplying all the numbers together.
num_attempts += 1
product = 1
for num in numbers:
product *= num
if product == value:
solution.numbers = numbers
for i in range(n - 1):
solution.operations.append(MUL)
return solution
# 5. If there are factors of value in numbers, pick one.
# Find the factors
if value > 0:
factors = get_factors(value)
factors_in_list = []
for num in numbers:
if num in factors and num not in factors_in_list:
factors_in_list.append(num)
# Prefer smaller factors
factors_in_list.sort()
# Make an attempt for each factor in the list
for factor in factors_in_list:
other_factor = value / factor
# See if the other n - 1 numbers can arrive at the other_factor
other_numbers = exclude(numbers, factor)
solution = solve(other_numbers, other_factor)
# If so, append a '*' operation to the end of the solution
if solution:
solution.numbers.append(factor)
solution.operations.append(MUL)
return solution
# 6. Try subtracting from value
# Try it for each number in numbers
# Prefer even numbers
numbers_evens_first = sort_evens_first(numbers)
for num in numbers_evens_first:
result = value - num
# See if the other n - 1 numbers can arrive at the result
other_numbers = exclude(numbers, num)
solution = solve(other_numbers, result)
# If so, append a '+' operation to the end of the solution
if solution:
solution.numbers.append(num)
solution.operations.append(ADD)
return solution
# 7. Try adding to value
# Try it for each number in numbers
# Prefer even numbers
for num in numbers_evens_first:
result = value + num
# See if the other n - 1 numbers can arrive at the result
other_numbers = exclude(numbers, num)
solution = solve(other_numbers, result)
# If so, append a '+' operation to the end of the solution
if solution:
solution.numbers.append(num)
solution.operations.append(SUB)
return solution
# 8. Try multiplying value
# Try it for each number in numbers
# Prefer smaller numbers
numbers.sort()
for num in numbers:
result = value * num
# See if the other n - 1 numbers can arrive at the result
other_numbers = exclude(numbers, num)
solution = solve(other_numbers, result)
# If so, append a '/' operation to the end of the solution
if solution:
solution.numbers.append(num)
solution.operations.append(DIV)
return solution
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment