There weren't tests like that?! I just wasted time then.
Code here if you want to try:
require 'benchmark' strings = [] 10000.times do string = Array.new(rand(500..1000)) { rand(10).to_s }.join('') strings << string end def solution1(array) array.each do |s| s.to_i.digits.sum end end def solution2(array) array.each do |s| s.chars.sum(&:to_i) end end puts Benchmark.measure{ solution1(strings) } puts Benchmark.measure{ solution2(strings) }
His solution is actually much faster than mine for larger data sets and longer numbers. Benchmark testing on Repl.it given the following parameters:
10,000 strings of length L where 500 <= L < 1,000
L
string.to_i.digits.sum -- 21.563347 seconds
string.chars.sum(&:to_i) -- 3.311112 seconds
However, my solution is like 2 or 3 characters shorter so clearly mine is more efficient!
ja wäre am besten... Das sollte aber auch einfach nur ein test sein, um meinem lehrer zu zeigen, was man hier alles machen kann...
Nah, it's so simple. How is that so amazing? 🤣
I removed list(, which makes it shorter.
list(
Loading collection data...
There weren't tests like that?! I just wasted time then.
Code here if you want to try:
His solution is actually much faster than mine for larger data sets and longer numbers. Benchmark testing on Repl.it given the following parameters:
10,000 strings of length
L
where 500 <=L
< 1,000string.to_i.digits.sum -- 21.563347 seconds
string.chars.sum(&:to_i) -- 3.311112 seconds
However, my solution is like 2 or 3 characters shorter so clearly mine is more efficient!
ja wäre am besten... Das sollte aber auch einfach nur ein test sein, um meinem lehrer zu zeigen, was man hier alles machen kann...
Nah, it's so simple. How is that so amazing? 🤣
I removed
list(
, which makes it shorter.