5 lines in Ruby, could it be shorter ?


  • 0
    A
    def compare_version(v1,v2)
      v1 = v1.split(".").map(&:to_i)
      v2 = v2.split(".").map(&:to_i)
      (v1.size - v2.size).times { v2 << 0 }
      (v2.size - v1.size).times { v1 << 0 }
      v1.zip(v2).each.map { | x, y | x <=> y }.detect{ |n| n != 0 } || 0
    end

  • 0

    After all that preparation, the last line should be a lot shorter.
    Just wrote some solutions myself (spoiler alert, obviously).


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.