Skip to content

Instantly share code, notes, and snippets.

@vetional
Created October 8, 2017 03:07
Show Gist options
  • Save vetional/7ad8d5ff0b777e33f0dcd18f849eb39e to your computer and use it in GitHub Desktop.
Save vetional/7ad8d5ff0b777e33f0dcd18f849eb39e to your computer and use it in GitHub Desktop.
caterpillar created by vetional - https://repl.it/MPFx/1
def caterpillarMethod(A, s):
n = len(A)
front, total = 0, 0
for back in xrange(n):
print "back: ",back
while (front < n and total + A[front] <= s):
total += A[front]
front += 1
print "total:", total, "back:", back, "front:",front
if total == s:
return True
total -= A[back]
return False
A = [6, 2, 7, 4, 1, 3, 6]
print caterpillarMethod(A,12)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment