Skip to content

Instantly share code, notes, and snippets.

@probaldhar
Created February 19, 2020 00:45
Show Gist options
  • Save probaldhar/62f4e6761af3771b9c16f3b4a9ca5144 to your computer and use it in GitHub Desktop.
Save probaldhar/62f4e6761af3771b9c16f3b4a9ca5144 to your computer and use it in GitHub Desktop.
# //"1234", 10
# //"1234", 37
# return true/false
def calculateSum(s, k):
# string to arr
# s = list(s)
return helper(s, k, len(s), 0)
def helper(s, k, end, tempSum):
# print(end, tempSum)
if end == 0:
return False
if tempSum == k or tempSum + int(s) == k:
return True
if tempSum > k:
return False
for i in range(0, end): # for (i = start; i < end; i++)
choose = s[0:i + 1] # 12
# print(choose)
rest = s[i+1:end] # 34
# print(choose, rest)
# 1 + 2 + 3 + 4
return helper(rest, k, len(rest), int(choose) + tempSum)
print(calculateSum('1234', 10))
print(calculateSum('1234', 37))
print(calculateSum('1234', 11))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment