Skip to content

Commit c4cd826

Browse files
add_one
1 parent 71f5eaf commit c4cd826

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

add-one.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
def add_one(arr):
2+
output = 1
3+
for i in range(len(arr),0,-1):
4+
output = output + arr[i-1]
5+
borrow = output//10
6+
if(borrow == 0):
7+
arr[i-1] = output
8+
break
9+
else:
10+
arr[i-1] = output%10
11+
output = borrow
12+
arr = [borrow] + arr
13+
index = 0
14+
while arr[index] == 0:
15+
index += 1
16+
return arr[index: ]
17+
18+
def test_function(test_case):
19+
arr = test_case[0]
20+
solution = test_case[1]
21+
22+
output = add_one(arr)
23+
for index, element in enumerate(output):
24+
if element != solution[index]:
25+
print("Fail")
26+
return
27+
print("Pass")
28+
29+
arr = [0]
30+
solution = [1]
31+
test_case = [arr, solution]
32+
test_function(test_case)

0 commit comments

Comments
 (0)