A Fun Code Challenge: Counting Smiley Faces with Ruby

Raghavendra S
3 min readJul 23, 2023

--

Photo by Jacqueline Munguía on Unsplash

Introduction:

As developers, we often come across code challenges that not only test our technical skills but also bring a smile to our faces. In this blog post, we’ll tackle a fun code challenge using Ruby. Our task is to count the number of smiley faces in a given array of strings. So, let’s dive in and put our Ruby skills to the test!

  1. Understanding the Challenge:
  2. The challenge is to count the number of smiley faces in an array of strings. A smiley face can be represented by either a colon followed by a closing parenthesis `:)` or a semicolon followed by a closing parenthesis `;)`. The smiley face can also have a nose, represented by a hyphen `-`. Our goal is to write a Ruby function that takes in an array of strings and returns the count of smiley faces found.

2. Writing the Solution:

Let’s start by creating a method called `count_smiley_faces` that takes an array of strings as input:

```ruby

def count_smiley_faces(arr)

count = 0

arr.each do |str|

count += str.scan(/(:|;)(-)?(\)|D)/).count

end

count

end

```

In this solution, we iterate over each string in the array and use the `scan` method with a regular expression to find smiley faces. The regular expression `(/(:|;)(-)?(\)|D)/)` matches either a colon or a semicolon, followed by an optional hyphen, and then a closing parenthesis or capital D. We use the `count` method to count the number of matches found and increment the `count` variable accordingly.

3. Testing the Solution:

To ensure our solution works correctly, let’s test it with some sample inputs:

```ruby

puts count_smiley_faces([‘Hello :) World’, ‘I am happy ;)’]) # Output: 2

puts count_smiley_faces([‘Smile :D’, ‘No smile here’, ‘Another :) smile’]) # Output: 2

puts count_smiley_faces([‘No smiley faces’]) # Output: 0

```

By running these test cases, we can verify that our solution correctly counts the number of smiley faces in the given array of strings.

4. Handling Edge Cases:

To make our solution more robust, we should consider edge cases. For example, what if the smiley face has additional characters before or after it? We can modify our regular expression to handle such cases:

```ruby

count += str.scan(/.*(:|;)(-)?(\)|D).*/).count

```

This updated regular expression (`/.*(:|;)(-)?(\)|D).*/`) matches any characters before and after the smiley face, allowing us to count smiley faces even if they are surrounded by other characters.

Conclusion:

Solving code challenges not only sharpens our programming skills but also brings a sense of joy and accomplishment. In this blog post, we tackled a fun code challenge using Ruby: counting smiley faces in an array of strings. By leveraging regular expressions and the power of Ruby, we created a solution that accurately counts the number of smiley faces.

Remember, code challenges are a great way to improve your problem-solving abilities and explore different aspects of a programming language. So, embrace the challenges, have fun, and keep coding with a smile!

Note: This blog post provides a solution to a specific code challenge. The implementation details and code structure may vary based on your preferences and the specific requirements of the challenge.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Raghavendra S
Raghavendra S

Written by Raghavendra S

Artificial enthusiast. Rubyist.

No responses yet

Write a response