Ruby - Strings - Indexing

Sort by

recency

|

75 Discussions

|

  • + 0 comments
    string.tap { |s| s[4, 11] = ((s[4, 5].to_f + s[10, 5].to_f) / 2).round(2).to_s }
    
  • + 0 comments

    to_f to convert the string into float.

    def serial_average(str)
        num1, num2, num3 = str.split("-")
        avg = ((num2.to_f+num3.to_f)/2).round(2)
        ans = "#{num1}-#{avg}"
        return ans
    end
    
  • + 0 comments
    def serial_average(n)
        "#{n[0, 3]}-#{((n[4, 5].to_f + n[10, 5].to_f) / 2).round(2)}"
    end
    
  • + 0 comments
    def serial_average(key = "001-12.43-56.78")
        serial = key[0,3]
        number1 = key[4, 5]
        number2 = key[10, 15]
        
        avg = (number1.to_f + number2.to_f) / 2
        avg = avg.round(2)
        key[4,11] = avg.to_s
        return key
    end
    
  • + 0 comments

    Here is ruby - strings - indexing problem solution - https://www.gyangav.com/2022/10/hackerrank-ruby-strings-indexing-problem-solution.html