Codecademy Pseudocode Searching Algorithm



This content originally appeared on DEV Community and was authored by David Wishart

Introduction

The task in the Codecademy Intro to IT Course is to create pseudocode for a pattern searching algorithm, searching for a specific value in a larger set of data.

Steps to outline algorithm

  1. Input the text to be searched through and the pattern to look for.
  2. Create a text_length variable set to the number of characters in the text (redundant!)
  3. Create a pattern_length variable set to the number of characters in the pattern
  4. Create a match_count variable set to 0
  5. Has the entire text been searched? If not continue to 6, if so skip to 9
  6. Iterate to the next character in text
  7. Increment text_length (no!)
  8. Does the current character + pattern_length = pattern a. If yes match_count + 1 b. Else go back to 5.
  9. Display match_count

Flowchart

Image description

Pseudocode

Define text
Define pattern
Create a patt_length variable and set it to the length of the pattern input
Create a match_count variable and set it to 0
if the entire text hasn’t been searched:
iterate to the next character in text
if current character of text + (pattern_length – 1) = pattern
+1 to match_count
Display “The text contains “pattern” “match_count” times.

Conclusion

I guess this should work?! I realised while doing the flowchart that the text_length variable shouldn’t be needed so I omitted that from the pseudocode. Once I learn some languages I would be able to say whether the iteration bit is clear enough to make it through the text from start to finish.


This content originally appeared on DEV Community and was authored by David Wishart