From 899eaf1d3a1b6624fb4454f40ec880ad1935788f Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Wed, 17 Mar 2021 08:17:08 -0700 Subject: [PATCH 1/3] Add add digits challenge --- DIRECTORY.md | 1 + data_structures/arrays/add_digits.rb | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 data_structures/arrays/add_digits.rb diff --git a/DIRECTORY.md b/DIRECTORY.md index 3c396ff..c14d5b6 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -4,6 +4,7 @@ ## Data Structures * 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 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) diff --git a/data_structures/arrays/add_digits.rb b/data_structures/arrays/add_digits.rb new file mode 100644 index 0000000..63e1d64 --- /dev/null +++ b/data_structures/arrays/add_digits.rb @@ -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 \ No newline at end of file From 5588587bc1b22bf20af5b4278627f343400dd4f5 Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Thu, 18 Mar 2021 08:55:05 -0700 Subject: [PATCH 2/3] Add recursion solution --- data_structures/arrays/add_digits.rb | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/data_structures/arrays/add_digits.rb b/data_structures/arrays/add_digits.rb index 63e1d64..e18d768 100644 --- a/data_structures/arrays/add_digits.rb +++ b/data_structures/arrays/add_digits.rb @@ -15,10 +15,26 @@ # @return {Integer} # -# Approach 1: Brute Force +# Approach 1: Recursion +# +# Time complexity: O(n) # def add_digits(num) + if num.to_s.length < 2 + return num + end + + digits_to_sum = num.to_s.split('') + sum = 0 + digits_to_sum.each do |num| + sum += num.to_i + end + + add_digits(sum) end puts(add_digits(38)) -# Output: 2 \ No newline at end of file +# => 2 + +puts(add_digits(284)) +# => 5 \ No newline at end of file From fd3add587c806cc511eaa36265597108b15c8765 Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Thu, 18 Mar 2021 13:15:19 -0700 Subject: [PATCH 3/3] Add solution without recursion --- data_structures/arrays/add_digits.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/data_structures/arrays/add_digits.rb b/data_structures/arrays/add_digits.rb index e18d768..731236d 100644 --- a/data_structures/arrays/add_digits.rb +++ b/data_structures/arrays/add_digits.rb @@ -33,6 +33,27 @@ def add_digits(num) add_digits(sum) end +# puts(add_digits(38)) +# # => 2 + +# puts(add_digits(284)) +# # => 5 + +# +# Approach 2: Without recursion +# +def add_digits(num) + until num.to_s.length < 2 + digits_to_sum = num.to_s.split('') + num = 0 + + digits_to_sum.each do |number| + num += number.to_i + end + end + num +end + puts(add_digits(38)) # => 2