Day 11 of 30 - Ruby Coding Challenge - Repdigit Number Algorithm
Day 11 of 30. We're going solve the the Repdigit Algorithm, which validates if a digit is repeated in the whole number
Join the DZone community and get the full member experience.
Join For FreeHey!
This is the blog post version of the Youtube video of the Ruby Coding Challenges in 30 days.
Today we’re going to solve this simple and elegant coding challenge, which is the Repdigit Algorithm:
Given a number, I want to check if the number is composed of repeated instances of the same digit
That was the fancy definition.
Real Life:
999999
This is a Repdigit (repeated digit) because it repeats 9 for the whole number
7777
This is a Repdigit because it repeats 7 for the whole number
666664
This is not a Repdigit because it doesn’t repeat 6 for the whole number
333444777
This is not a Repdigit because it doesn’t repeat 3,4 or 7 for the whole number
I think you got it. Very simple.
#1 Repdigit Algorithm Solution
One possible solution is
- convert the number into an array
- take the first digit of the number
- check if all of the numbers are the same digit
Pretty simple, right?
Let’s see how Ruby handles it.
Step 1
The first step is actually to convert the Integer into a String so that we can be able to manipulate it
xxxxxxxxxx
array = number.to_s
Step 2
Now it’s time to create the array based on that string
xxxxxxxxxx
array = number.to_s.split('')
Step 3
Finally, let’s convert each string in the array back to an integer
xxxxxxxxxx
array = number.to_s.split('').map(&:to_i)
Step 4
I’m going to take the first digit to compare with all the numbers
xxxxxxxxxx
digit = array.first
Step 5
Then the final step is to count how many digits in that number are different from the first digit
xxxxxxxxxx
array = number.to_s.split('').map(&:to_i)
digit = array.first
count = array.count { |item| item != digit }
Perfect! If the count variable is different from zero then we have a digit that is different from the first digit, which means that the number is not Repdigit
The final solution would be:
xxxxxxxxxx
def is_repdigit(number)
array = number.to_s.split('').map(&:to_i)
digit = array.first
count = array.count { |item| item != digit }
if count == 0
return true
else
return false
end
end
puts is_repdigit(777) # true
#2 A Leaner Solution in Ruby
A little bit leaner solution would be the following:
xxxxxxxxxx
def is_repdigit_2(number)
array = number.to_s.split('').map(&:to_i)
array.count { |item| item != array.first } == 0
end
That’s it!
In the next video, we’re going to explore a Ruby Way to solve this algorithm. See you there :)
Don’t forget to come by and say hi Alex
Twitter Youtube Instagram Linkedin GitHub Courses
Published at DZone with permission of Alexandre Gama. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
8 Data Anonymization Techniques to Safeguard User PII Data
-
AI Technology Is Drastically Disrupting the Background Screening Industry
-
IntelliJ IDEA Switches to JetBrains YouTrack
-
How to Load Cypress Chrome Extension
Comments