mirror of
https://github.com/TheAlgorithms/Ruby
synced 2025-02-07 08:46:40 +01:00
Add add digits challenge
This commit is contained in:
parent
5a9a9c0058
commit
899eaf1d3a
2 changed files with 25 additions and 0 deletions
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
## Data Structures
|
## Data Structures
|
||||||
* Arrays
|
* Arrays
|
||||||
|
* [Add Digits](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/add_digits.rb)
|
||||||
* [Find All Duplicates In An Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/find_all_duplicates_in_an_array.rb)
|
* [Find All Duplicates In An Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/find_all_duplicates_in_an_array.rb)
|
||||||
* [Find The Highest Altitude](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/find_the_highest_altitude.rb)
|
* [Find The Highest Altitude](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/find_the_highest_altitude.rb)
|
||||||
* [Get Products Of All Other Elements](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/get_products_of_all_other_elements.rb)
|
* [Get Products Of All Other Elements](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/get_products_of_all_other_elements.rb)
|
||||||
|
|
24
data_structures/arrays/add_digits.rb
Normal file
24
data_structures/arrays/add_digits.rb
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
# Challenge name: Add Digits
|
||||||
|
#
|
||||||
|
# Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
|
||||||
|
#
|
||||||
|
# Example:
|
||||||
|
#
|
||||||
|
# Input: 38
|
||||||
|
# Output: 2
|
||||||
|
# Explanation: The process is like: 3 + 8 = 11, 1 + 1 = 2.
|
||||||
|
# Since 2 has only one digit, return it.
|
||||||
|
#
|
||||||
|
# Follow up:
|
||||||
|
# Could you do it without any loop/recursion in O(1) runtime?
|
||||||
|
# @param {Integer} num
|
||||||
|
# @return {Integer}
|
||||||
|
|
||||||
|
#
|
||||||
|
# Approach 1: Brute Force
|
||||||
|
#
|
||||||
|
def add_digits(num)
|
||||||
|
end
|
||||||
|
|
||||||
|
puts(add_digits(38))
|
||||||
|
# Output: 2
|
Loading…
Add table
Reference in a new issue