Skip to content

Instantly share code, notes, and snippets.

@stefania11
Created October 13, 2015 03:36
Show Gist options
  • Save stefania11/e73393205c00dd6bcee1 to your computer and use it in GitHub Desktop.
Save stefania11/e73393205c00dd6bcee1 to your computer and use it in GitHub Desktop.
class Addition
attr_reader :arr1, :arr2
def initialize(arr1, arr2)
@arr1 = arr1
@arr2 = arr2
@sum = []
end
def assign_based_on_size
if @arr1.size > @arr2.size
@longer = @arr1
@shorter = @arr2
else
@longer = @arr2
@shorter = @arr1
end
end
def longer
@longer ||= assign_based_on_size && @longer
end
def shorter
@shorter ||= assign_based_on_size && @shorter
end
def sum
counter = 0
longer.reverse.each.with_index do |n, i|
@sum[i] = n + shorter.reverse[i].to_i + counter
counter = 0
while @sum[i] >= 10
counter += 1
@sum[i] -= 10
end
end
if counter > 0
@sum << counter
end
p @sum.reverse
end
end
example = Addition.new([9,2,3,5,2,3,4][1,3,4,5,5])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment