DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

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
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. Day 11 of 30 - Ruby Coding Challenge - Repdigit Number Algorithm

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

Alexandre Gama user avatar by
Alexandre Gama
·
Jun. 26, 20 · Tutorial
Like (1)
Save
Tweet
Share
6.17K Views

Join the DZone community and get the full member experience.

Join For Free

Hey!

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

Ruby
xxxxxxxxxx
1
 
1
array = number.to_s

Step 2

Now it’s time to create the array based on that string

Ruby
xxxxxxxxxx
1
 
1
array = number.to_s.split('')

Step 3

Finally, let’s convert each string in the array back to an integer

Ruby
xxxxxxxxxx
1
 
1
array = number.to_s.split('').map(&:to_i)

Step 4

I’m going to take the first digit to compare with all the numbers

Ruby
xxxxxxxxxx
1
 
1
digit = array.first

Step 5

Then the final step is to count how many digits in that number are different from the first digit

Ruby
xxxxxxxxxx
1
 
1
array = number.to_s.split('').map(&:to_i)
2
digit = array.first
3
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:

Ruby
xxxxxxxxxx
1
13
 
1
def is_repdigit(number)
2
  array = number.to_s.split('').map(&:to_i)
3
  digit = array.first
4
  count = array.count { |item| item != digit }
5
6
  if count == 0
7
    return true
8
  else
9
    return false
10
  end
11
end
12
13
puts is_repdigit(777) # true

#2 A Leaner Solution in Ruby

A little bit leaner solution would be the following:

Ruby
xxxxxxxxxx
1
 
1
def is_repdigit_2(number)
2
  array = number.to_s.split('').map(&:to_i)
3
  array.count { |item| item != array.first } == 0
4
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

Algorithm Coding (social sciences)

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

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: