Skip to content

Instantly share code, notes, and snippets.

@brgnepal
Created April 10, 2015 07:07
Show Gist options
  • Save brgnepal/642775abdc3469cd9453 to your computer and use it in GitHub Desktop.
Save brgnepal/642775abdc3469cd9453 to your computer and use it in GitHub Desktop.
Answer for ADDREV challenge in SPOJ
cases = gets.chomp.to_i
def valid_numeric_input?(input)
input.zero?
end
def reverse_num(num)
num.to_s.reverse.to_i
end
def rev_add(data)
sum = 0
data.each do |rec|
sum += reverse_num(rec)
end
reverse_num(sum)
end
valid_numeric_input?(cases)
results = []
cases.times do |index|
input_str = gets.chomp
nums = input_str.split(' ').map(&:to_i)
results[index] = rev_add(nums)
end
results.each { |e| puts e }
@saritaranbhor05
Copy link

You can remove method 'rev_add' and do the changes as follows:
cases.times do |index|
nums = gets.chomp.split(' ').map { |n| reverse_num(n) }
results[index] = reverse_num(nums.to_a.reduce(&:+))
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment