Recently, I was taking up this challenge and choose my favorite language 'Ruby' to do it. My following solution got accepted (Gist).
For each test case, I am to reverse two numbers, add them, and print the reverse of the sum. Reversing means taking the base-10 digits of a number in reverse order, stripping any leading or trailing zeroes.
### Code
cases = gets.chomp.to_i
if cases.zero?
puts "Enter number > 0"
exit
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
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 }
### End of Code
It showed that I have used 7.3MB
memory which I don't have any idea.
I know this is not the optimized solution. I have used lots of looping which could be minizied. Could you review it and help me to optimize it?
valid_numeric_input?
doesn't affect the program at all. \$\endgroup\$ – Jay Mitchell Apr 10 '15 at 14:27