Skip to content

Commit f32e3d4

Browse files
committed
forward sum solution, no zero padding
1 parent 79f40d9 commit f32e3d4

File tree

1 file changed

+137
-13
lines changed

1 file changed

+137
-13
lines changed

Python/chapter02/2.5 - Sum Lists/miguel_2.5_soln.py

Lines changed: 137 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -148,32 +148,96 @@ def sum_lists_forward(ll1: LinkedList, ll2: LinkedList) -> LinkedList:
148148
:param ll2: second input linked list
149149
:return: a linked list containing the result of the addition
150150
"""
151-
assert(ll1.size == ll2.size)
152-
n1 = ll1.head
153-
n2 = ll2.head
151+
shorter_ll, longer_ll = (ll1, ll2) if ll1.size < ll2.size else (ll2, ll1)
152+
n_shorter = shorter_ll.head
153+
n_longer = longer_ll.head
154+
length_diff = abs(shorter_ll.size - longer_ll.size)
155+
# first, advance longer list to line up with
156+
# digit of shorter list
157+
# for example, if we have:
158+
# longer_ll = 9 -> 9 -> 9 -> 9
159+
# shorter_ll = 9 -> 9
160+
# we will line up the current pointer
161+
# of longer_ll to correspond to the
162+
# '10s' digit place because we must add
163+
# digits that are in the same place value.
164+
# n_longer will point to the following parenthesis
165+
# 9 -> 9 -> (9) -> 9
166+
# n_shorter will point to the beginning of shorter_ll
167+
# (9) -> 9
168+
for i in range(length_diff):
169+
n_longer = n_longer.next
154170
output_ll = LinkedList()
171+
# perform addition, taking into account a
172+
# 'backwards' carry value since we
173+
# are looping forward digits down to
174+
# 'ones' digit place
155175
prev_node = None
156-
while n1 is not None:
157-
first = n1.data
158-
second = n2.data
176+
leading_bw_carry = False
177+
while n_shorter is not None:
178+
first = n_shorter.data
179+
second = n_longer.data
159180
result = first + second
160181
if result >= 10:
161-
carry = 1
182+
backwards_carry = 1
162183
result -= 10 # extract digit in one's place
163184
if prev_node is None:
164185
# for when the first addition yields a carry
165186
# and the output linked list is empty
166-
output_ll.append_to_head(carry)
187+
leading_bw_carry = True
167188
else:
168189
# add carry to previous digit
169-
prev_node.data += carry
190+
prev_node.data += backwards_carry
170191
output_ll.append_to_tail(result)
171-
n1 = n1.next
172-
n2 = n2.next
192+
n_shorter = n_shorter.next
193+
n_longer = n_longer.next
173194
prev_node = output_ll.tail
174195

175-
# if carry > 0:
176-
# output_ll.append_to_tail(carry)
196+
# first case after adding same digit places,
197+
# both input lists same size
198+
if length_diff == 0:
199+
if leading_bw_carry:
200+
backwards_carry = 1
201+
output_ll.append_to_head(backwards_carry)
202+
return output_ll
203+
# otherwise, we have non-equal lengths
204+
# with or without a leading backward carry
205+
# leading bw carry means the the size diff
206+
# decreased.
207+
# no leading bw carry means size diff stayed the same
208+
if leading_bw_carry:
209+
backwards_carry = 1
210+
else:
211+
backwards_carry = 0
212+
# otherwise, as long as the diff is >= 0
213+
# we want to take care of the leading digits.
214+
# start by decrementing the diff
215+
length_diff -= 1
216+
while length_diff >= 0:
217+
# advance the n_longer pointer from longer_ll head
218+
# up to where the current diff is.
219+
n_longer = longer_ll.head
220+
for i in range(length_diff):
221+
n_longer = n_longer.next
222+
223+
if backwards_carry == 1:
224+
output_ll.append_to_head(backwards_carry)
225+
# output data will have the backwards_carry
226+
# appended from before
227+
result = output_ll.head.data + n_longer.data
228+
if result >= 10:
229+
result -= 10
230+
else:
231+
backwards_carry = 0
232+
output_ll.head.data = result
233+
else:
234+
# if backwards carry is 0, bring corresponding digit down
235+
result = n_longer.data
236+
output_ll.append_to_head(result)
237+
length_diff -= 1
238+
# last backwards carry
239+
if backwards_carry == 1:
240+
output_ll.append_to_head(backwards_carry)
177241
return output_ll
178242

179243

@@ -271,6 +335,16 @@ def setUp(self):
271335
LinkedList(5),
272336
LinkedList(2, 1),
273337
LinkedList(7, 1)
338+
),
339+
(
340+
LinkedList(),
341+
LinkedList(1),
342+
LinkedList(1)
343+
),
344+
(
345+
LinkedList(),
346+
LinkedList(),
347+
LinkedList()
274348
)
275349
]
276350

@@ -299,6 +373,56 @@ def setUp(self):
299373
LinkedList(9, 9, 9, 9),
300374
LinkedList(9, 9, 9, 9),
301375
LinkedList(1, 9, 9, 9, 8)
376+
),
377+
(
378+
LinkedList(9, 9, 9, 9),
379+
LinkedList(9, 9),
380+
LinkedList(1, 0, 0, 9, 8)
381+
),
382+
(
383+
LinkedList(1, 2),
384+
LinkedList(4),
385+
LinkedList(1, 6)
386+
),
387+
(
388+
LinkedList(4, 2),
389+
LinkedList(5),
390+
LinkedList(4, 7)
391+
),
392+
(
393+
LinkedList(4, 2),
394+
LinkedList(9),
395+
LinkedList(5, 1)
396+
),
397+
(
398+
LinkedList(8, 2),
399+
LinkedList(4),
400+
LinkedList(8, 6)
401+
),
402+
(
403+
LinkedList(9, 1, 0, 2),
404+
LinkedList(1, 0),
405+
LinkedList(9, 1, 1, 2)
406+
),
407+
(
408+
LinkedList(9, 1, 9, 2),
409+
LinkedList(1, 0),
410+
LinkedList(9, 2, 0, 2)
411+
),
412+
(
413+
LinkedList(9, 9, 1, 9),
414+
LinkedList(2, 0),
415+
LinkedList(9, 9, 3, 9)
416+
),
417+
(
418+
LinkedList(),
419+
LinkedList(1),
420+
LinkedList(1)
421+
),
422+
(
423+
LinkedList(),
424+
LinkedList(),
425+
LinkedList()
302426
)
303427
]
304428

0 commit comments

Comments
 (0)