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 ...
How to find the sum of integer digits | Python-suzu6
"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:
dst = sum (list (map (int, str (n))))
return digitSum (dst)
n = 12345
result = digitSum (n)
print (int (result)) # 15 "
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
I want to put out
18 is output
"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
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)".
Answer # 2
a = a.digits.sum while a.digits.size! = 1 p a
- convert integer number to string year/month display in java
- ruby - [rails] [asynchronous] like function: the number of likes is not displayed normally
- python 3x - python3 i want to extract a specific number of list elements and store them in a new list
- r i want to convert the number of data counted every 5 minutes to the number of data every 10 minutes
- [c] i want to convert a string to a number, but it doesn't work
- php - get the number of integer characters
- ruby - i want to get a random number token with securerandom when creating a model instance with rails
- python 3x - python3 i want to convert hexadecimal numbers to array components
- ruby on rails - wrong number of arguments (given 1, expected 0) is displayed when updating
- cannot display the total number of elements that the user also has in ruby on rails
- ruby - wrong number of arguments (given 1, expected 3) error message is output when adding a column
- 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
- ruby - i want to test that the number contained in the character string is 3 digits with rspec!
- ruby - i want to fix the number of elements in the array and fill the missing elements with 0 from the end
- 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
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- python - you may need to restart the kernel to use updated packages error
- python 3x - typeerror: 'method' object is not subscriptable
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- xcode - pod install [!] no `podfile 'found in the project directory
- vuejs - [vuetify] unable to locate target [data-app] i want to unit test to avoid warning
- android studio - unresolved reference comes out in kotlin