{{announcement.body}}
{{announcement.title}}

Day 3 of 30 - Ruby Coding Challenge - Better Prime Algorithm :)

DZone 's Guide to

Day 3 of 30 - Ruby Coding Challenge - Better Prime Algorithm :)

It's time to use some Ruby power to solve the previous coding challenge, which was writing an algorithm to count how many prime items exist in an array

· Web Dev Zone ·
Free Resource

Hey!

Day 3 of 30: Ruby Coding Challenge number 3.

This is the post version of this video on Youtube.

In the previous video, we solved a really common challenge:

How many prime items are in a given array?

The final code was terrible. However, I’m not even sorry. Remembering why:

  • How does my brain work when it comes to solving algorithms? Just get the job done! Decorate after that.

When it comes to algorithms, decoration means a few things:

  • Better performance.
  • Better readability.
  • Better maintainability.

Well, better code design. Today, I’ll solve the same problem with (I hope) a better code design:

Below you can see the first ugly version:

#1 - Ugly Algorithm Version

Ruby
 




xxxxxxxxxx
1
24


 
1
def count_prime_number_version_1(array)
2
  prime_count = 0
3
  for item in array
4
    next if item == 1
5
    is_prime = true
6
 
          
7
    number = item - 1
8
    while number > 1
9
      if item % number == 0
10
        is_prime = false
11
        break
12
      else
13
        number = number - 1
14
      end
15
    end
16
 
          
17
    if is_prime
18
      prime_count = prime_count + 1
19
    end
20
  end
21
  return prime_count
22
end
23
 
          
24
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]



It’s time to organize the kitchen. It would be great to have a method that verifies if a given number is prime.

#2 - Method Organization

We have 2 main responsibilities:

  • Iterate over the array, asking questions.
  • Validate if a given number is prime or not.

I’m going to split this into 2 methods, which will have those responsibilities above:

Ruby
 


Cool. It’s time to bring some Ruby power and use Ruby Blocks with the count method, which we did in in the first video.

Ruby


#3 - Ruby Arrays

I don’t know about you, but I hate local variables.

It’s time to get rid of them. In Ruby, it’s possible to indicate which items should be created in an array, given:

  • The initial number.
  • The pattern to generate the next number.
Ruby
 




xxxxxxxxxx
1
10


 
1
def is_prime_number(item)
2
  return false if item == 1
3
 
          
4
  (2..(item - 1)).each do |number|
5
    if item % number == 0
6
      return false
7
    end
8
  end
9
  return true
10
end



Amazing, isn’t it?

#4 - Cheating: Ruby Prime Library

Now that we know how to create the Algorithm manually, it usually doesn’t make sense in real life. Ruby has a Prime Library, which can get the job done easily.

We just ask if the number is prime or not. Notice that we’re importing the Prime library

Ruby


Going ahead and replacing the is_prime(item) method:

Ruby
 


And that’s our final version — a little bit different than the first one.

Ruby
 


That’s it. I hope that you’ve learned 1 thing or 2 : )


Thinking out loud here, probably the next coding challenge will be rewriting the Algorithm manually using recursion.

Thanks for your visit, let’s keep in touch and see you!

Twitter Youtube Instagram Linkedin

Topics:
algorithm, design patterns, good practices, ruby, tutorial, web dev

Published at DZone with permission of Alexandre Gama . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}