Skip to content

Instantly share code, notes, and snippets.

@M-Barnett
Created December 7, 2015 20:19
Show Gist options
  • Save M-Barnett/1212676817f0eb80d99a to your computer and use it in GitHub Desktop.
Save M-Barnett/1212676817f0eb80d99a to your computer and use it in GitHub Desktop.
Code Challenge Problem 5
"""
Problem 5:
Write a program that outputs all the possibilities to put + or - or nothing between
the numbers 1,2,...,9 (in this order) such that the result is always 100. For
example: 1 + 2 + 34 - 5 + 67 - 8 + 9 = 100
"""
# Creates the combinations to be tested
def combination_generator ():
#random operation (temporarily not randomised as I haven't figure that part out yet)
op1 = "+"
op2 = "+"
op3 = "+"
op4 = "+"
op5 = "+"
op6 = "+"
op7 = "+"
op8 = "+"
return ("1%d2%d3%d4%d5%d6%d7%d8%d9", op1, op2, op3, op4, op5, op6, op7, op8)
# Generates result for an input combination
def result_generator (combination):
result = int(combination)
return result
# Function that runs the other functions and returns the final output
def main():
#Still needs loop
while i < (8*7*6*5*4*3*2 + 1):
a = combination_generator()
b = result_generator(a)
if b == 100:
print a
i++
else:
i++
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment