This gist is a practice exercise TDDing a solution to a coding interview question on CoderPad. I used this as a template.
Created
August 1, 2023 15:50
-
-
Save Noreaster76/86b99e7be15e004744a61e17fb8d12c1 to your computer and use it in GitHub Desktop.
Add two integers together that were given as linked lists
This file contains hidden or 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
require 'rspec/autorun' | |
# add two integers given as linked lists. | |
# return the answer as a linked list. | |
# all linked lists, both input and input, have their ones digit at the head | |
# of the linked list and their more significant digits descend from there. | |
# | |
# example: | |
# | |
# [2] -> [4] -> [3] -> [x] represents the integer 342 | |
# [0] -> [1] -> [1] -> [x] represents the integer 110 | |
# | |
# adding those two together should return: | |
# | |
# [2] -> [5] -> [4] -> [x] | |
Node = Struct.new(:value, :next) | |
def add_two_linked_lists(first, second) | |
return first unless second | |
return second unless first | |
result = Node.new(0, nil) | |
result_pointer = result | |
carry_over = 0 | |
while first && second | |
sum = carry_over + first.value + second.value | |
this_digit = sum % 10 | |
carry_over = sum >= 10 ? 1 : 0 | |
new_node = Node.new(this_digit, nil) | |
result_pointer.next = new_node | |
result_pointer = result_pointer.next | |
first = first.next | |
second = second.next | |
end | |
while first | |
sum = carry_over + first.value | |
this_digit = sum % 10 | |
carry_over = 0 | |
result_pointer.next = Node.new(this_digit, nil) | |
result_pointer = result_pointer.next | |
first = first.next | |
end | |
while second | |
sum = carry_over + second.value | |
this_digit = sum % 10 | |
carry_over = 0 | |
result_pointer.next = Node.new(this_digit, nil) | |
result_pointer = result_pointer.next | |
second = second.next | |
end | |
result.next | |
end | |
RSpec.describe 'adding two linked list integers' do | |
context 'with given example 1' do | |
let(:first) do | |
Node.new(2, | |
Node.new(4, | |
Node.new(3, nil) | |
) | |
) | |
end | |
let(:second) do | |
Node.new(0, | |
Node.new(1, | |
Node.new(1, nil) | |
) | |
) | |
end | |
it { expect(add_two_linked_lists(first, second).value).to eq 2 } | |
it { expect(add_two_linked_lists(first, second).next.value).to eq 5 } | |
it { expect(add_two_linked_lists(first, second).next.next.value).to eq 4 } | |
it { expect(add_two_linked_lists(first, second).next.next.next).to be_nil } | |
end | |
context 'with another example' do | |
let(:first) do | |
Node.new(9, | |
Node.new(4, | |
Node.new(3, nil) | |
) | |
) | |
end | |
let(:second) do | |
Node.new(2, | |
Node.new(9, nil) | |
) | |
end | |
it { expect(add_two_linked_lists(first, second).value).to eq 1 } | |
it { expect(add_two_linked_lists(first, second).next.value).to eq 4 } | |
it { expect(add_two_linked_lists(first, second).next.next.value).to eq 4 } | |
it { expect(add_two_linked_lists(first, second).next.next.next).to be_nil } | |
end | |
context 'with another example' do | |
let(:second) do | |
Node.new(9, | |
Node.new(4, | |
Node.new(3, nil) | |
) | |
) | |
end | |
let(:first) do | |
Node.new(2, | |
Node.new(9, nil) | |
) | |
end | |
it { expect(add_two_linked_lists(first, second).value).to eq 1 } | |
it { expect(add_two_linked_lists(first, second).next.value).to eq 4 } | |
it { expect(add_two_linked_lists(first, second).next.next.value).to eq 4 } | |
it { expect(add_two_linked_lists(first, second).next.next.next).to be_nil } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment