A2 - Advanced List Indexing
A common thing to do with lists is to swap two values. Here's an example of swapping two values in a list of names:
names = ["Dave", "Kelly", "Savannah", "Erica"]
# In the names list, swap 'Kelly' and 'Erica'
temp = names[1] # copy 'Kelly' into a temporary variable
names[1] = names[3] # copy 'Erica' into Kelly's old position
names[3] = temp # copy 'Kelly' into Erica's old position
Task 1
In your Unit 4 folder, create a file called a2a.py.
Create a list of either five family members, or other people you know. The list should be sorted from the oldest to youngest person.
Next, use the technique shown above to do some swaps so that the list ends up being sorted alphabetically. Please choose a list of people that requires at least two swaps to complete the alphabetic sort (you can lie about their age rankings if you'd like).
Recall that when we access a list item by specifying its index, we can put a variable inside the square brackets instead of a number, like this:
days = ["Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat"]
num = int(input("Select a number between 0 and 6: "))
print(f"The day you selected is {days[num]}")
Here, we are asking the user to specify which position of the list they want us to print. Remember that the positions are numbered starting a zero, which is why we ask for a number between 0 and 6. If we'd rather have them enter a number between 1 and 7, this is an easy change:
days = ["Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat"]
num = int(input("Select a number between 1 and 7: "))
print(f"The day you selected is {days[num-1]}")
days[num] to days[num-1]. This will subtract 1 from whatever number they typed in, so that the correct list item is selected.
Task 2
In your Unit 4 folder, create a file called a2b.py.
Write a program similar to the example above, but instead of using days of the week, use months of the year. Ask them for a number between 1 and 12, then print: 1. The name of the month their number corresponds to 2. The name of the month prior to the one they selected 3. The name of the month after the one they selected
For example, if they type in the number 3, your output would look something like this:
At minimum this should work for the numbers ranging from 2 to 11 (corresponding to February to November), but for full marks try to also make if work if they choose January or December. You'll need a couple of if statements to handle these special cases.
Extra Challenge: There is a very elegant solution to this problem that works with all twelve months but doesn't require any if statements. It uses the % (mod) operator and takes advantage of the fact that months of the year cycle. If you figure it out, call me over and explain what you did.
Extension
Consider the following program that goes through a list of people, and outputs the current name on the list as well as the name after it on the list:
# Create a list of names
names = ["Dave", "Kelly", "Savannah", "Erica"]
# Go through the list, print some information
for i in range(len(names)):
if i < len(names) - 1:
print("Current name is {names[i]}, next name is {names[i+1]}.")
else:
print("Current name is {names[i]}. It is the last name on the list.")
Current name is Dave, next name is Kelly.
Current name is Kelly, next name is Savannah.
Current name is Savannah, next name is Erica.
Current name is Erica. It is the last name on the list.
names[i] gives us the current list item, and accessing names[i+1] gives us the next list item.
Extension - The Dating Game
In your Unit 4 folder, create a file called a2ext.py.
In a small town in a far away land, there is only one rule for dating: you cannot date anybody who is more than two years apart from you in age.
Here is a list that represents the ages of five people all standing in a line:
Your task will be to determine how far apart each person is in age from the next person in line, and whether they are allowed to date or not. Your program output should look something like this:
Person 1 and person 2 are 2 years apart.
They can be a couple.
Person 2 and person 3 are 4 years apart.
They are forbidden to date.
... etc ...
for i in range(...) loop in a similar manner to the example above.
I would recommend starting by just figuring out how far apart in age each set of people are. You may find the abs(x) function is useful here, which returns the absolute value of the expression inside the brackets.