Ad
  • Custom User Avatar

    There weren't tests like that?! I just wasted time then.

  • Custom User Avatar

    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) }
    
  • Custom User Avatar

    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

    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!

  • Custom User Avatar

    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...

  • Custom User Avatar

    Nah, it's so simple. How is that so amazing? 🤣

  • Custom User Avatar

    I removed list(, which makes it shorter.