Created
October 8, 2017 03:07
-
-
Save vetional/7ad8d5ff0b777e33f0dcd18f849eb39e to your computer and use it in GitHub Desktop.
caterpillar created by vetional - https://repl.it/MPFx/1
This file contains hidden or 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
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