DEV Community

Will Diep
Will Diep

Posted on

An Introduction to Algorithms Using Ruby

The string reversal algorithm is perhaps the most common JavaScript code challenge on the internet. In this article, we explore various string reversal techniques as a good number of string manipulation algorithms are dependent on ones ability to reverse a string.

The string reversal algorithms is one of the most common introductory code challenges. In this section, we will explore various string reversal techniques

Write a method that will take a string as input, and return a new string with the same letters in reverse order without using the built-in #reverse method

Alt Text

Code Implementations:

1) While loop

2) #each enumerator

While Loop

Before diving into the code, it is best to process your logic using pseudocode to solve the problem. In other words, what are the sequence steps, in plain English, that you will need in order to take a string of letter as an input and output that string in reverse order?

Pseudocode Steps:
  1. String is input to the method
  2. Create an empty string called 'string_reversed' in which will hold the result
  3. Find access all of the characters in the string
  4. Put the characters from the string into ‘reversed_string’, one at a time
  5. With each iteration, put each character IN FRONT OF the character currently in the string (for example if ‘reversed_string’ currently holds ‘a’, when we put in the next letter, we want ‘reversed_string’ to hold ‘ba’…)
  6. Return my answer — ‘reversed_string’

Alt Text

#each enumerator

Pseudocode steps:
  1. Split the input string to an array of single letter and lowercase each one
  2. Create an empty array called 'string_reverse'
  3. Use the #each enumerator to traverse through each 'char'
  4. Within each iteration, use #unshift method to prepend that first character of input string into 'string_reversed'
  5. Call the #join method to convert the array back to a string

Alt Text

Our next challenge is build on top on using the #reverse_string and further strengthen our string manipulation skills by implementing simple algorithms to test if a string of text is a palindrome.

First off, what is a palindrome?

A palindrome is a word, number, or other sequence of characters which reads the same backward as forward, such as "racecar" or tacocat"

Code Implementations

  1. An Intuitive Approach
  2. Looping Through and Comparing Characters

An Intuitive Approach

Write a method that takes a string and returns true if it is a palindrome.

Pseudocode steps:
  1. Create a conditional statement to test if the input string matches with invoking the string of the #reversed_string method
  2. Return true if it does
  3. Otherwise return a string stating it's not a palindrome

Alt Text

Looping through and Comparing Characters

In this approach, we are going to apply the Two Pointers Technique in which loops through the string as it was passed in and compare each character with the character currently in the position it'd have taken if the string was reversed.

For instance, if we were testing the string "coding", we would compare "c" with "g" because if the string was reversed d would take r's position.

Correspondingly, we'd compare "o" in position 2 with "n" in position 2 from the end as well and if the string were a palindrome, all of these would test true.

Pseudocode steps:

  1. Lowercase and split the input string into an array; assign that to 'char_array'
  2. Traverse through each character and index to check if that character matches with the position if it was going backwards.
  3. Return false immediately if it does not match
  4. Otherwise, return true

Alt Text

In our third challenge, we are going to continue to manipulate arrays and strings to count the number of vowels in a given string of text.

Code Implementations:

  1. While loop
  2. #each enumerable

Write a method that takes a string and returns the number of vowels# in the string. For example:

Alt Text

While loop

Pseudocode steps:
  1. Create 'vowels' and 'counter" variables and set to zero
  2. Use a while loop to check if each character matches with a vowel
  3. If it does, increment the "vowels" to 1
  4. Once out of the conditional statement inside the while loop, increment the counter by 1 to move onto the next character
  5. At the end, return the 'vowels' variable

Alt Text

#each Enumerable

Pseudocode steps:
  1. Create an array of vowels; assign it to a 'vowels' variable
  2. Create a counter to keep track of the number of vowels
  3. Iterate over the string using #each
  4. Within each pass, check if the current character matches with the vowels array
  5. if it does, increment the counter variable by 1
  6. In the end, return the counter variable

Alt Text

Stay tune for more algorithm challenges in part 2!

Top comments (1)

Collapse
 
dauncy profile image
Daniel Wilder

Super useful topic and enjoyable read! Looking forward to part 2...