[Ruby] Get a 3-digit integer and conditionally branch using an if statement (digits method)

Overview

I tried to solve the problem of conditional branching by dividing a 3-digit integer by digit using the ** digits method ** used in Article I wrote last time. Since the viewpoint is a little different from the last time, I thought it would be good to deepen my understanding, so I wrote this article.

table of contents

--Practice --Problem --Conditions --Answer

--Supplement --Processing result of variable num --Processing result of variable sum

--Summary

--References

Practice

problem

There are 3 digit integers. Write a program that outputs the "sum of hundreds, tens, and ones" of the integer and outputs the difference from a multiple of 10.

conditions

--True if the difference from a multiple of 10 is within 2 --Otherwise, the difference from the multiple of 10 is ◯ --However, the one with the closest difference to the multiple of 10 is output.

answer

def near_ten_multiple(i)
  num = i.digits.take(3)   #1st place,10th place,Get the 100's place and store it in the variable num
  sum = (num.sum) % 10     #Get the total of 3 digits and get the 1st place

  if sum <= 2 || sum >= 8  #When 2 or less or 8 or more
    p 'True'
  elsif sum >= 5           # 5, 6,When 7
    p "The difference from multiples of 10#{ 10 - sum }is"
  else                     # 3,When 4
    p "The difference from multiples of 10#{sum}is"
  end
end

#Method call
near_ten_multiple(117)   # 1 + 1 + 7 = 9
near_ten_multiple(111)   # 1 + 1 + 1 = 3
near_ten_multiple(123)   # 1 + 2 + 3 = 6

#Terminal output result
# "True"
# "The difference from multiples of 10 is 3"
# "The difference from multiples of 10 is 4"

Supplement

Processing result of variable num

The number of digits specified by the argument in digits.take (3) is acquired in order from the 1st digit. This time I just want to get the total, so I don't have to think too much about the order, but the result is like this.

num = i.digits.take(3)

#Terminal output result
# 117 => [7, 1, 1]
# 111 => [1, 1, 1]
# 123 => [3, 2, 1]

Processing result of variable sum

** 1's place is obtained by doing % 10 after calculating the total value with ** sum **. For example, suppose you have the number 24. If you do % 10

24 ÷ 10 = 2 The remainder is 4. This remainder is ** 1's place **.

sum = (num.sum) % 10

#Terminal output result
# 9
# 3
# 6

Summary

--The remainder when an integer is divided by 10 is ** 1's place **

References

-Ruby 2.7.0 Reference Manual (Integer # digits)

-[Ruby] How to get the tens place and the ones place

Recommended Posts

[Ruby] Get a 3-digit integer and conditionally branch using an if statement (digits method)
[Ruby] conditional branch if statement
[Ruby] present/blank method and postfix if.
About if statement and branch processing
Write a null case using the Optional type without using an if statement
If it is Ruby, it is efficient to make it a method and stock the processing.
How to get an arbitrary digit from a number of 2 or more digits! !!
[Ruby] Cut out a string using the slice method