Find and replace

Find and replace

A popular utility for applications.

Roly Poly is a classic nursery rhyme for teaching young children opposites such as up and down, big and small, fast and slow. In the rhyme, the word, “up” can be replaced with “big”, and the word, “down” replaced with “small” in the second verse.

Make


Write a program that asks the user which word they want to replace in the rhyme: roly poly roly poly up up up roly poly roly poly down down down. The individual words can be stored in a list. All occurances of that word are replaced with a word chosen by the user.

Note: do not use the .replace() method if you are aware of it. You are writing the code that might have been used to make that method.

Use this boilerplate code as a starting point:

Success Criteria

Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.

Complete the subprogram called `replace` that:

  1. Takes three parameters: x is the word to be replaced, y is the word to replace x with, and l is a list of words.
  2. Every occurance of the word x is replaced with the word y.
  3. Returns the list.

Complete the subprogram called `output` that:

  1. Takes one parameter, a list of words.
  2. Outputs all the words in the list, each one on a new line.

Complete the `main program` so that:

  1. The ryhme is output by calling the `output` subprogram.
  2. The user can input which word to replace.
  3. The user can input the new word.
  4. The `replace` subprogram is called to replace the word in the list.
  5. The new ryhme is output by calling the `output` subprogram.

Typical inputs and outputs from the program would be:

roly

poly

roly

poly

up

up

up

roly

poly

roly

poly

down

down

down

Which word to replace? :up

Replace with what? :big

roly

poly

roly

poly

big

big

big

roly

poly

roly

poly

down

down

down

Knowledge Organiser

Use these resources as a reference to help you meet the success criteria.

Programming guide:

Evaluate


Run the unit tests below to check that your program has met the success criteria.

Which word to replace? :down

Replace with what? :slow

roly

poly

roly

poly

up

up

up

roly

poly

roly

poly

slow

slow

slow

Which word to replace? :down

Replace with what? :small

roly

poly

roly

poly

up

up

up

roly

poly

roly

poly

small

small

small


Which word to replace? :up

Replace with what? :big

roly

poly

roly

poly

big

big

big

roly

poly

roly

poly

down

down

down

Which word to replace? :up

Replace with what? :fast

roly

poly

roly

poly

fast

fast

fast

roly

poly

roly

poly

down

down

down

Check that you have:

  • Used comments within the code to describe the purpose of subprograms, conditions and iterations.
  • Used meaningful identifier names. That means the names of subprograms and variables indicate what they are for.
Craig'n'Dave logo

Craig ‘n’ Dave

Bett Awards 2024 Finalist