Skip to content

Instantly share code, notes, and snippets.

@VorontsovIE
Created January 18, 2017 11:05
Show Gist options
  • Save VorontsovIE/bdebb79c7a9d0e3d0011893955f408c2 to your computer and use it in GitHub Desktop.
Save VorontsovIE/bdebb79c7a9d0e3d0011893955f408c2 to your computer and use it in GitHub Desktop.
def check_sum(xs_head_sum, xs_tail, goal_sum, equation_head):
if len(xs_tail) == 0:
if xs_head_sum == goal_sum:
return '{}={}'.format(equation_head, goal_sum)
else:
return None
result_1 = check_sum(xs_head_sum + xs_tail[0], xs_tail[1:], goal_sum, '{}+{}'.format(equation_head, xs_tail[0]))
if result_1:
return result_1
result_2 = check_sum(xs_head_sum - xs_tail[0], xs_tail[1:], goal_sum, '{}-{}'.format(equation_head, xs_tail[0]))
if result_2:
return result_2
n, s = [int(x) for x in input().split()]
xs = [int(x) for x in input().split()]
result = check_sum(xs[0], xs[1:], s, str(xs[0]))
if result:
print(result)
else:
print("No solution")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment