Cross a Name Off

Python for AIHard+10 XP+10

Cross a Name Off

A party guest list has one name per line. Someone can't come, so their name has to go.

Write remove_name(path, name):

  1. Read every name in the file. Strip each line.
  2. Rewrite the file so it holds every name except name, in the same order, one per line, each followed by \n.
  3. Read the file back and return the names in it as a list (stripped).
  • Remove every line that matches name, not just the first.
  • If name isn't in the file, the names stay as they are.
  • If every name is removed, the file ends up empty and you return [].
  • The file never has blank lines.

Leave the make_file helper as it is.

Examples

path = make_file('party.txt', 'ada\nbo\ncy\n')
name = 'bo'
returns ['ada', 'cy']
path = make_file("rm_a.txt", "ada\ngrace\nlinus\n")
name = "grace"
returns ['ada', 'linus']
path = make_file("rm_b.txt", "ada\ngrace\n")
name = "zoe"
returns ['ada', 'grace']

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run