DEV Community

Cover image for PREP Technique for Algorithms
pharia-le
pharia-le

Posted on

PREP Technique for Algorithms

What is PREP?

PREP is a methodology in which to approach an algorithm or coding challenge. The acronym stands for Parameters, Return, Example, Pseudocode.

  • Parameters
    What is the type of argument being inputted and are there multiple or default arguments?

  • Return
    What is being asked to be returned and what is the type?

  • Example
    Provide 2-3 examples of what we expect to happen if we call the function with certain arguments. Can you think of any edge cases?

  • Pseudocode
    Write informal steps on how to solve the problem. Think about what the parameters are and what steps need to be taken to get to our expected return. All statements showing "dependency" are to be indented.

Examples

reverse()

Given a string, return a new string with the reversed order of characters.

  • P - Single string argument
  • R - A string that is the reverse of the original string argument
  • E - Account for spaces, capitalizion, and punctuation marks
// reverse('hello world') --> 'dlrow olleh'
// reverse('Love!') --> '!evoL'
// reverse('JavaScript') --> 'tpircSavaJ'
  • P - see below
function reverse(str) {
  // With keyword let, declare & assign a variable reverseStr to ''

  // Iterate over str from the last idx to the 0th idx
    // Add the character at the current idx to reverseStr

  // return reverseStr

}

Now, translate pseudocode procedure to code.

Solution

function reverse(str) {
  // With keyword let, declare & assign a variable reverseStr to ''
  let reverseStr = ''
  // Iterate over str from the last idx to the 0th idx
    // Add the character at the current idx to reverseStr
  for (let i=str.length-1; i>=0; i--) {
    reverseStr+=str[i]
  }
  // return reverseStr
  return reverseStr
}

capitalize()

Write a function that accepts a string. The function should capitalize the first letter of each word in the string then return the capitalized string.

  • P - Single string argument
  • R - A string where the first character of each word of the original string argument is capitalized
  • E - Account for spaces and punctuation marks
//   capitalize('hello world!') --> 'Hello World!'
//   capitalize('i love code') --> 'I Love Code'
//   capitalize('one, two, three...') --> 'One, Two, Three..'
  • P - see below
function capitalize(str) {
  // Split the str argument by the spaces ' ', creating an array of words

  // With the array of words, use map method to capitalize each word's first element at index 0 & join it to the remaining word's letters

  // Rejoin the words by a space ' '

  // Wrap the code above in a return

}

Now, translate pseudocode procedure to code.

function capitalize(str) {
  // Split the str argument by the spaces ' ', creating an array of words
  str.split(' ')
  // With the array of words, use map method to capitalize each word's first element at index 0 & join it to the remaining word's letters
  .map(word => word[0].toUpperCase() + word.slice(1))
  // Rejoin the words by a space ' '
  .join(' ')
  // Wrap the code above in a return

}

For our last step, we'll need to bring the return keyword to the top of the code block.

Solution

function capitalize(str) {
  return str.split(' ')
            .map(word => word[0].toUpperCase() + word.slice(1))
            .join(' ')
}

Conclusion

When solving an algorithm, remember to think of your parameters and return immediately. If examples are not provided, write out a few yourself so that you may test as you go. Pseudocode your thoughts on how to solve and keep it in a syntax in which can be converted to code.

& Remember... Happy coding, friends! =)

Top comments (1)

Collapse
 
ljcdev profile image
ljc-dev

Great post 😀, u explained very well 👍. Didn't hear of this technique before. Had a nice read 😁