Created
March 5, 2016 15:32
-
-
Save STashakkori/d6d93d20f57b15df1067 to your computer and use it in GitHub Desktop.
Triplebyte Programming Challenge Python
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
__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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.