Editor

Clean Up a Name List

#266 · Working with data · Intermediate · 60 XP · about 12 min

People typed their names into a sign-up form any way they liked: ' ada LOVELACE ', 'Ada Lovelace', "mary-jane o'neil". Write clean_names(names) that tidies every name and returns the cleaned names as a list, without repeats.

To clean a name:

  • Remove the whitespace at both ends, and replace every run of whitespace inside it (spaces, tabs, newlines) with a single space.
  • Fix the capitals: a letter is upper case if it is the first character of the name or comes right after a space, a hyphen - or an apostrophe '. Every other letter is lower case.

After cleaning, leave out names that are empty, and keep only the first copy of names that are now identical. Keep the names in the order they first appear.

Examples

clean_names(['  ada   LOVELACE ', 'Ada Lovelace', 'grace hopper'])
# returns ['Ada Lovelace', 'Grace Hopper']
clean_names(["mary-jane o'neil", 'ALAN  turing', '   '])
# returns ["Mary-Jane O'Neil", 'Alan Turing']

Constraints

  • 0 <= len(names) <= 10000
  • Names contain only English letters, whitespace, hyphens and apostrophes.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.