Created
August 8, 2012 15:23
-
-
Save vpetro/3295892 to your computer and use it in GitHub Desktop.
add1
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
def add1(lst, val, n): | |
start, stop, step = 0, len(lst), 1 | |
cond = n == 0 | |
if n < 0: | |
start, stop = stop-1, start-1 | |
step = -1 | |
n = abs(n) | |
for idx in range(start, stop, step): | |
if (lst[idx] == val and n > 0) or cond: | |
lst[idx] += 1 | |
n -= 1 | |
if __name__ == "__main__": | |
lst = [1,4,1,5,1] | |
add1(lst, 1, 0) | |
print lst | |
lst = [1,4,1,5,1] | |
add1(lst, 1, 2) | |
print lst | |
lst = [1,4,1,5,1] | |
add1(lst, 1, -2) | |
print lst |
Author
vpetro
commented
Aug 8, 2012
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment