DEV Community

Cover image for Ruby Challenges P1
Mattias Velamsson
Mattias Velamsson

Posted on

Ruby Challenges P1

As I prepare for a new side project using Ruby, I thought it'd be a good idea to brush up on my logic skills. So, I decided to tackle some fun coding challenges to get back into the groove, and then make a post about it where I walk you through how I solved it.

To be honest, I'm not the strongest at math, and I consider myself more of a creative type. But, even though these challenges were relatively easy, I felt proud of myself for figuring out the solutions on my own and understanding the logic behind them.

It's been only a few months since I started, but I'm already amazed by the progress I've made. I'm really excited to see where I'll be in a year if I keep up this pace!

Either way, lets get started! ✌🏼

Challenge Structure:

  • Challenge Description.
  • My psuedo code I made before completing the challenge
  • My solution

Find The Numbers

Challenge: In a given array, find and count the numbers of integers with a value greater than 5.

# -- Psuedo Code -- #
# 1. Create an array with mixed numbers
# 2. Create an final_array that will hold all the numbers we "find" that are greater than 5.
# 3. Go through each of the numbers in the array, checking it's value and comparing it with 5.
# 4. If the check is positive ( greater than 5 ) then push that number to our final_array
# 5. Get the total numbers of final_array to show result
# 6. Print result
Enter fullscreen mode Exit fullscreen mode
arr = [17,7,3,6,10,1]
final_arr =
arr.each {|number| number >= 5 ? new_arr << number : nil }
puts "this is how many numbers were bigger than 5: #{new_arr.count}"
Enter fullscreen mode Exit fullscreen mode

Prime Number?

Challenge: Create an algorithm that finds prime numbers, you have to decide if the number in question can only be divided by itself and 1.

Going to be honest and say this confused me a lot. I didn't really understand the math/logic of a prime number at all at first

# -- Psuedo Code -- #
# 1. Create method that takes one argument
# 2. Check that the number is less or equal to 1, or if it's even. If it is, it's not a prime number.
# 3. Check if number is greater than 2, and loop through all numbers from 2 to the square root of number.
# 4. Check if it can be divided by the the integer without any remainder = not a prime number
# 5. If the loop completes without finding any of the previous things, then number is prime. Print "PRIME! ⭐️".
Enter fullscreen mode Exit fullscreen mode
def prime_number(number)
  if number <= 1 || number.even?
    puts 'NOT A PRIME NUMBER! ❌'
  elsif (2..Math.sqrt(number)).all? { |i| (number % i).nonzero? }
    puts 'PRIME! ⭐️'
    puts 'NOT A PRIME NUMBER! ❌'

Enter fullscreen mode Exit fullscreen mode

Sum of Prime Numbers

Challenge: # Identify and calculate the sum of prime numbers in an array.

# -- Psuedo Code -- #
# 1. Create a method that takes an array of numbers
# 2. Create a variable containing the sum of found prime numbers
# 3. Iterate over the array, checking if the values are prime or not
# 4. If it's a prime number, add to sum
# 5. Print out total value of all the primes
Enter fullscreen mode Exit fullscreen mode
def find_prime_number(arr)
  total = arr.reduce(0) do |sum, number|
    if number <= 1 || number.even?
    elsif (2..Math.sqrt(number)).all? {|i| (number % i ).nonzero? }
      sum + number
  puts "Total total of primes in array => #{total}"

find_prime_number([3,0,3, 54, 43, 11, 123, 322])
Enter fullscreen mode Exit fullscreen mode

Factorial Numbers?

Challenge: # The factorial of a number is the product of all the positive integers that are less than or equal to the number in question.
For example, for the number 6, the factorial would be 1 x 2 x 3 x 4 x 5 x 6 = 720.

# -- Psuedo Code -- #
# 1. Create method that takes one argument(number)
# 2. Create an array containing all the numbers from 1 to number.
# 3. Iterate over the array and add the value to a sum.
# 4. Print out sum
Enter fullscreen mode Exit fullscreen mode
def factorial_numbers(number)
  arr = (1..number).to_a
  sum = arr.reduce(1) do |accmulator, value|
    accmulator * value
  puts sum

Enter fullscreen mode Exit fullscreen mode

Find repeated digits

Challenge: # Your objective is to use Ruby to validate whether or not the number is made up of a series of the same digit in a row.

I must say I'm not EXACTLY sure what the expected result was here but, I managed to get a logic that outputs if it has a series of the same digit or not at least.

# -- Psuedo Code -- #
# 1. Make a method that takes a number
# 2. Split the number into an array
# 3. Check if the number is unique or not
# 4. Unless 0, disqualify.
Enter fullscreen mode Exit fullscreen mode
def check_repeated_digits_option(number)
  arr =
  puts arr.uniq.length != 1 ? "#{number} => Congrats! Digits are are all unique" : "#{number} => Disqualified. Contains duplicates."
Enter fullscreen mode Exit fullscreen mode

Fibonacci challenge

Challenge: A Fibonacci Sequence is created by adding two numbers to create the next number in the sequence.
You then sum that number with the one preceding it to get the next number,, and so on.
For example, if you have the sequence 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, then the next number in the sequence is 55.

# -- Psuedo Code -- #
# 1. Create a method that takes a max value
# 2. Have a variable that holds the count(total)
# 3. Give two numbers to start with
# 4. Create a loop that runs until given max value
# 5. While the loop runs, take the last item in the array, and add the second to last item in the array to count.
# 6. Add the new number to the array
# 7. Print out result
Enter fullscreen mode Exit fullscreen mode
def fibonnaci_count(max)
  count = 0
  arr = [0,1]
  while count <= max
    count = arr.last + arr[-2]
    arr << count

sequence = fibonnaci_count(2000)
puts "The sequence is: #{sequence}"

Enter fullscreen mode Exit fullscreen mode

Missing Number Game

Challenge: Create a program that takes a series of numbers and examines it to find the numbers missing to make it a perfect range.

For example, the series of numbers could be [2, 1, 5, 4, 6, 9, 7, 8, 10].

This array is missing the number 3. The goal of this problem is to write code that finds which numbers are missing, and print them out.

# -- Psuedo Code -- #
# 1. Create a method that takes an array of numbers.
# 2. Sort the array, to make it work in any order.
# 3. Get the smallest and biggest value to get a start and end point.
# 4. Create a new, perfect array with the range between those numbers
# 5. Use the new array and compare it to the old array
# 6. Create a new missing_numbers array that will be filled with the numbers not included in old_array when comparing.
Enter fullscreen mode Exit fullscreen mode
def missing_number(array)
  array = array.sort
  first = array.min
  last = array.max
  perf_arr = (first..last).to_a
  missing_numbers = []
  if array == perf_arr
    puts 'No missing numbers'
    perf_arr.each do |number|
      missing_numbers << number unless array.include?(number)
    puts "These are the numbers missing: #{missing_numbers}"


Enter fullscreen mode Exit fullscreen mode


It was actually very fun going back to some good ol' logical thinking, as I had to troubleshoot and figure out what I was doing wrong along the way, and learn new techniques to get the same result.

Refactoring was also a big part of it during these tests, as my first versions were often too long and overly complicated for no reason, especially when Ruby is as smart as it is 🤩

My takeaway from all of this is that I'm making daily improvements, and it feels amazing. Although this might be like nothing to others, I'm very proud of my progress, especially in my weak areas such as this.

Live to learn, learn to live, then teach others.

Until next time! ✌🏼

Top comments (0)