I would like to ask for advice on Ruby algorithm problems.
The problem is what time is 7 hours before the entered time in 24 hour notation.
From standard input
Is entered in the format.
condition is

When it was said that it was 7 hours ago
I want to be

~Example 2~

I'm at the level of tackling Ruby beginner problems, but I couldn't figure out how to approach it.
I thought to delete unnecessary parts (minutes and colons) by receiving as an array or a character string, but the time of input is 2 digits or 1 digit
I didn't know, so I wondered how to delete it, but I think it may be because the way of thinking is different.
I'm sorry if the explanation is insufficient, but how should it be resolved?

  • Answer # 1

    I thought it would be easy to split the hour and minute values ​​with split.
    Here is an example answer.

    str = gets
    str = "6:30"
    # str = "12:59"
    Use the # split method to separate them with a colon into an array ["6", "30"].
    # Next, map and to_i make an array of numbers [6, 30].
    #2 I will write it down.
    h, m = str.split(":").map(&:to_i)
    h, m = str.split(":").map{|t| t.to_i}
    If you subtract #7 and divide by 24, it will be 24 hours notation 7 hours ago.
    h = (h-7)% 24
    There are two ways to write the composition of the # character string.
    res = "#{h}:#{m}"
    res = h.to_s + ":" + m.to_s
    puts res