Reverse an array without using a loop in ruby

后端 未结 8 525
时光说笑
时光说笑 2021-01-24 12:12

I have a coding challenge to reverse a an array with 5 elements in it. How would I do this without using the reverse method?

Code:

def reverse(array)
 a         


        
8条回答
  •  难免孤独
    2021-01-24 13:01

    Gentlemen, start your engines!

    [Edit: added two method from @Grych and results for n = 8_000.]

    @Grych, @ArupRakshit, @konsolebox and @JörgWMittag: please check that I've written your method(s) correctly.

    Methods

    def grych_reduce(array)
      array.reduce([]) {|acc, x| [x] + acc}
    end
    
    def grych_prebuild(array)
      reversed = Array.new(array.count)
      array.each_with_index do |item, index|
        reversed[-(index + 1)] = item
      end
      reversed
    end
    
    def arup(ary)
      ary.values_at(*(ary.size-1).downto(0))
    end
    
    def konsolebox(array)
      t = array.pop
      konsolebox(array) if array.length > 0
      array.unshift t
    end    
    
    def jorg_recurse(array)
      return array if array.size < 2
      reverse(array.drop(1)) + array.first(1)
    end
    
    def jorg_tail(array, accum=[])
      return accum if array.empty?
      reverse(array.drop(1), array.first(1) + accum)
    end
    
    def jorg_fold(array)
      array.reduce([]) {|accum, el| [el] + accum }
    end
    
    def jorg_loop(array)
      array.each_with_object([]) {|el, accum| accum.unshift(el) }
    end
    
    def cary_rotate(arr)
      arr.size.times.with_object([]) { |_,a| a << arr.rotate!(-1).first }
    end
    
    def cary_boring(arr)
      (arr.size-1).downto(0).with_object([]) { |i,a| a << arr[i] }
    end
    

    Benchmark

    require 'benchmark'
    
    arr = [*(1..n)]
    puts "n = #{n}"    
    
    Benchmark.bm(16) do |bm|
      bm.report('grych_reduce')    { grych_reduce(arr) }
      bm.report('grych_prebuild')  { grych_prebuild(arr) }
      bm.report('arup')            { arup(arr)  }
      bm.report('konsolebox')      { konsolebox(arr) }
      bm.report('jorg_recurse')    { jorg_recurse(arr) }
      bm.report('jorg_tail')       { jorg_tail(arr)  }
      bm.report('jorg_fold')       { jorg_fold(arr)  }
      bm.report('jorg_loop')       { jorg_loop(arr)  }
      bm.report('cary_rotate')     { cary_rotate(arr)  }
      bm.report('cary_boring')     { cary_boring(arr) }
      bm.report('grych_destructo') { grych_destructo(arr) }
    end
    

    Wednesday: warm-up (n = 8_000)

                           user     system      total        real
    grych_reduce       0.060000   0.060000   0.120000 (  0.115510)
    grych_prebuild     0.000000   0.000000   0.000000 (  0.001150)
    arup               0.000000   0.000000   0.000000 (  0.000563)
    konsolebox         0.000000   0.000000   0.000000 (  0.001581)
    jorg_recurse       0.060000   0.040000   0.100000 (  0.096417)
    jorg_tail          0.210000   0.070000   0.280000 (  0.282729)
    jorg_fold          0.060000   0.080000   0.140000 (  0.138216)
    jorg_loop          0.000000   0.000000   0.000000 (  0.001174)
    cary_rotate        0.060000   0.000000   0.060000 (  0.056863)
    cary_boring        0.000000   0.000000   0.000000 (  0.000961)
    grych_destructo    0.000000   0.000000   0.000000 (  0.000524)
    

    Thursday: trials #1 (n = 10_000)

                           user     system      total        real
    grych_reduce       0.090000   0.080000   0.170000 (  0.163276)
    grych_prebuild     0.000000   0.000000   0.000000 (  0.001500)
    arup               0.000000   0.000000   0.000000 (  0.000706)
    jorg_fold          0.080000   0.060000   0.140000 (  0.139656)
    jorg_loop          0.000000   0.000000   0.000000 (  0.001388)
    cary_rotate        0.090000   0.000000   0.090000 (  0.087327)
    cary_boring        0.000000   0.000000   0.000000 (  0.001185)
    grych_destructo    0.000000   0.000000   0.000000 (  0.000694)
    

    konsolebox, jorg_recurse and jorg_tail eliminated (stack level too deep).

    Friday: trials #2 (n = 50_000)

                           user     system      total        real
    grych_reduce       2.430000   3.490000   5.920000 (  5.920393)
    grych_prebuild     0.010000   0.000000   0.010000 (  0.007000)
    arup               0.000000   0.000000   0.000000 (  0.003826)
    jorg_fold          2.430000   3.590000   6.020000 (  6.026433)
    jorg_loop          0.010000   0.010000   0.020000 (  0.008491)
    cary_rotate        2.680000   0.000000   2.680000 (  2.686009)
    cary_boring        0.010000   0.000000   0.010000 (  0.006122)
    grych_destructo    0.000000   0.000000   0.000000 (  0.003288)
    

    Saturday: qualifications (n = 200_000)

                           user     system      total        real
    grych_reduce      43.720000  66.140000 109.860000 (109.901040)
    grych_prebuild     0.030000   0.000000   0.030000 (  0.028287)
    jorg_fold         43.700000  66.490000 110.190000 (110.252620)
    jorg_loop          0.030000   0.010000   0.040000 (  0.030409)
    cary_rotate       43.060000   0.050000  43.110000 ( 43.118151)
    cary_boring        0.020000   0.000000   0.020000 (  0.024570)
    grych_destructo    0.010000   0.000000   0.010000 (  0.013338)
    

    arup_verse eliminated (stack level too deep); grych_reduce, jorg_fold and cary_rotate eliminated (uncompetitive).

    Sunday: final (n = 10_000_000)

                           user     system      total        real
    grych_prebuild     1.450000   0.020000   1.470000 (  1.478903)
    jorg_loop          1.530000   0.040000   1.570000 (  1.649403)
    cary_boring        1.250000   0.040000   1.290000 (  1.288357)
    grych_destructo    0.640000   0.030000   0.670000 (  0.689819)
    

提交回复
热议问题