Space Python Loading Python…

Sorted copy

Write the codeEasy 2 stages

The mission report lists the crew alphabetically, but the sign-up order matters too: it decides who gets the first training slots. The same list has to be shown both ways.

Make a sorted copy for the report and leave the original alone, then make the sort cope with names that start with a small letter.

Print the names A–Z for the report, then the sign-up order, which must stay as it was:

Report: Kowalska, Lindqvist, Moreau, Okafor, Tanaka
Sign-up order: Tanaka, Okafor, Kowalska, Lindqvist, Moreau

sorted(signups) gives a new, sorted list and doesn't touch signups. ", ".join(...) turns each list into one line.

Sort a copy
Works for any sign-ups

Finish stage 1 to unlock this stage.

Two new crew members have names that start with a small letter: de Vries and van Dijk. The starter code puts them after Tanaka, because Python sorts all capitals before all small letters. Sort without caring about case:

Report: de Vries, Kowalska, Okafor, Tanaka, van Dijk
Sign-up order: Tanaka, de Vries, Okafor, Kowalska, van Dijk

Give sorted() a key: sorted(signups, key=str.lower) compares the lower-case version of each name but keeps the names as they are.

Sort without caring about case
Works for any names
You're not logged in, so your progress is kept in this browser only. Log in to save it, and to use hints and solutions.

Hints

Sorting and Reversing Lists

sorting-reversing-lists.py
# Sorting: change the list, or build a new one

# The first five asteroids ever found, in discovery order
found = ["Ceres", "Pallas", "Juno", "Vesta", "Astraea"]
diameters = [939, 512, 247, 525, 119]  # km, same order
print("Discovery order:", found)

# sort() rearranges the list itself, smallest first
diameters.sort()
print("Smallest first:", diameters)

# reverse=True flips the direction of the sort
diameters.sort(reverse=True)
print("Largest first:", diameters)

# reverse() does not sort, it only flips what is already there
found.reverse()
print("Newest first:", found)

# sorted() builds a new list and leaves the original alone
alphabetical = sorted(found)
print("Alphabetical copy:", alphabetical)
print("Original still: ", found)

# A common bug: sort() returns None, so this would erase your list
# diameters = diameters.sort()
Open the full example
script.py
PythonLn 1, Col 1Spaces: 4
Loading the Python environment…

Separate arguments with commas. They're passed on every Run as sys.argv[1:].