Pseudocode CS Thinking Example 4

Follow the full solution, then compare it with the other examples linked below.

Example 4

hard
A colleague writes pseudocode that mixes Python syntax, uses unclear variable names, and skips steps. Identify three problems with: 'x = input(); for y in x: if y == z: cnt+=1; print(cnt)'. Rewrite it properly.

Solution

  1. 1
    Step 1: Problems: (1) Uses Python syntax (input, print, +=) instead of pseudocode keywords. (2) Variable names x, y, z, cnt are unclear. (3) z is undefined โ€” where does it come from?
  2. 2
    Step 2: Rewrite: searchTerm = INPUT('Enter search term'). text = INPUT('Enter text'). SET count = 0. FOR EACH character IN text: IF character == searchTerm THEN count = count + 1. OUTPUT count.
  3. 3
    Step 3: Good pseudocode uses descriptive variable names, standard keywords, and includes all necessary steps.

Answer

Problems: Python syntax, unclear names, missing definition for z. Good pseudocode uses clear names, universal keywords, and complete logic.
Pseudocode quality matters because it is a communication tool. Poor pseudocode defeats its purpose โ€” it should be clearer than actual code, not more confusing.

About Pseudocode

An informal, human-readable description of an algorithm using structured language that resembles code but is not tied to any specific programming language. Pseudocode uses plain English mixed with programming constructs like IF, WHILE, and FOR to describe logic without worrying about syntax rules.

Learn more about Pseudocode โ†’

More Pseudocode Examples