Last active
November 7, 2017 10:37
-
-
Save NightZpy/87dd74ae35200f5a6f7b6599972bae79 to your computer and use it in GitHub Desktop.
null created by NightZpy - https://repl.it/NsLs/1
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
import time | |
def add_one (data, last_pos): | |
last = data[last_pos] | |
if last < 9: | |
data[last_pos] = data[last_pos] + 1 | |
return data | |
if last_pos >= 0: | |
if last == 9: | |
data[last_pos] = 0 | |
if last_pos == 0: | |
data.insert(0, 1) | |
else: | |
add_one (data, last_pos - 1) | |
return data | |
def add_one_itr (data): | |
idx = len(data) - 1 | |
while idx >= 0: | |
num = data[idx] | |
if num < 9: | |
data[idx] = data[idx] + 1 | |
return data | |
if num == 9: | |
data[idx] = 0 | |
if idx == 0: | |
data.insert(0, 1) | |
idx = idx - 1 | |
return data | |
data = [0,0,0,0] | |
start = time.time() | |
print add_one(data, len(data) - 1) | |
end = time.time() | |
print end - start | |
data = [0,0,0,0] | |
start = time.time() | |
print add_one_itr(data) | |
end = time.time() | |
print end - start | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment