Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save STashakkori/d6d93d20f57b15df1067 to your computer and use it in GitHub Desktop.
Save STashakkori/d6d93d20f57b15df1067 to your computer and use it in GitHub Desktop.
Triplebyte Programming Challenge Python
__author__ = 'sina'
def main():
f("12365212354", 26)
def f(numbers, target):
for i in range(1, len(numbers)):
current = int(numbers[0:i])
to_end = numbers[i:-1]
evaluate(0, current, to_end, target, current)
def evaluate(sum, previous, numbers, target, out):
if len(numbers) == 0:
if sum + previous == int(target):
print str(out) + "=" + str(target)
else:
for i in range(1, len(numbers)):
current = int(numbers[0:i])
to_end = numbers[i:-1]
evaluate(sum + previous, int(current), to_end, target, str(out) + "+" + str(current))
evaluate(sum, previous * int(current), to_end, target, str(out) + "*" + str(current))
evaluate(sum, previous / int(current), to_end, target, str(out) + "/" + str(current))
evaluate(sum + previous, -int(current), to_end, target, str(out) + "-" + str(current))
main()
@singlerider
Copy link

This was helpful, although it doesn't work with the given test data from the challenge. Also, you get a divide by zero error in some cases with this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment