I want to convert python3 code to ruby.
def digitSum (n):
# Determine number of digits
if len (str (n)) is 1:
return n
dst = sum (list (map (int, str (n))))
return digitSum (dst)
if __name__ == "__main__":
n = 12345
result = digitSum (n)
print (int (result)) # 15
The output will be 6.
I tried to write in ruby, but I didn't understand well.
gets;a = gets.split.map (&: to_i) .inject (: *)
b = a.to_s.chars.map (&: to_i) .sum
p b.to_s.chars.map (&: to_i) .sum if b.to_s.size>= 1
I don't know how to use if modifier or while modifier.
I don't even know about recursion ...
Reference
How to find the sum of integer digits | Python-suzu6
https://www.suzu6.net/posts/30/
"Repeat the calculation until the specified number of digits is reached.
"" "
Calculate the total value for each digit
Repeat until the number of digits reaches 1
"" "
def digitSum (n):
if len (str (n)) is 1:
return n
dst = sum (list (map (int, str (n))))
return digitSum (dst)
ifname== "main":
n = 12345
result = digitSum (n)
print (int (result)) # 15 "
Additional
25
39532 77816 39481 47622 16931 32489 56334 22924 71026 26191 10335 11788 77781 95817 89898 65677 23947 98056 61947 55674 21198 52752 21623 62340 82524
When
9
I want to put out
18 is output
yukicoder
https://yukicoder.me/problems/no/933/test#Test06.txt
"39532 77816 39481 47622 16931 32489 56334 22924 71026 26191 10335 11788 77781 95817 89898 65677 23947 98056 61947 55674 21198 52752 21623 62340 82524
"
-
Answer # 1
-
Answer # 2
a = a.digits.sum while a.digits.size! = 1 p a
Related articles
- i want to convert chinese numerals to alphanumeric characters using gsub with ruby
- [ruby + selenium] how to get the number of characters for each individual page
- i want to convert the 4-digit number stored in sql server to the time format (hh:mm) and save it (since it is converted to time,
- how can i convert a number to a string and concatenate it in c++?
- how to convert python3 list dictionary to dictionary
- python 3x - python3 i want to extract a specific number of list elements and store them in a new list
- ruby - i want to test that the number contained in the character string is 3 digits with rspec!
- python 3x - i want to convert the output result of python3 subprocess from a byte string
- ruby - the wrong number of arguments (given 5, expected 2) is not fixed
- swift - i want to convert an int to a string with a specified number of digits padded with spaces
- i want to get an n-digit integer as a random number with javascript
- excel - i want to generate an integer random number without duplication in distant cells
- ruby - [rails] i want to convert html ↔ erb
- ruby on rails - [rails] how to specify the number of date_select to be displayed
- ruby on rails - i want to display the number of people in the group
- ruby on rails - dealing with wrong number of arguments (given 1, expected 2)
- ruby on rails 6 - about validation (number of characters) of rails tweet posting
- ruby on rails - i want to resolve rails wrong number of arguments
- i want to convert hash values in ruby under specific conditions
- i want to convert wav to mp3 with ruby on rails and pass it to active strage
Another solution is to use the method of "leave the remainder divided by 9 (however, if it is 0, it will be 9 unless the original is 0)".
Number root-Wikipedia