Created
November 25, 2021 03:10
-
-
Save berdoezt/edb5d72104a2399e99a21e1237c2e9f9 to your computer and use it in GitHub Desktop.
Given array representing a digit (123 = [1, 2, 3]) write a function to increment the number by 1 digit. Source problem: https://www.youtube.com/watch?v=gcxyiFoEpyU
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
# Given array representing a digit (123 = [1, 2, 3]) write a function to increment the number by 1 digit | |
def incr(a): | |
l = 8 | |
for i in range(len(a) - 1, -1, -1): | |
a[i] += l | |
l = a[i] / 10 | |
a[i] = a[i] % 10 | |
if l == 0: | |
break | |
if l > 0: | |
a = [l] + a | |
return a | |
print(incr([9,9,9])) | |
print(incr([1,2,3])) | |
print(incr([5,2,8])) | |
print(incr([5,9,1])) | |
print(incr([5,9,9])) | |
print(incr([1,1,2])) | |
print(incr([1,1])) | |
print(incr([4])) | |
print(incr([9])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment