A1 - Simple List Algorithms
In this assignment we will look at algorithms to manipulate lists. Most programming languages include functions that do this heavy lifting for us, but it is important to also understand what's going on behind the scenes.
Below, you will see three algorithms presented first in pseudocode, then in Python.
You don't need to hand anything in for this assignment, but you will need to work towards being able to implement Python code for each algorithm given only the pseudocode and the Python reference binder/document. On the test for this unit, you'll be asked to do exactly this (using the computer with the ability to test your code, not handwritten).
The highlighted parts of the Python code below are what you are responsible for - everything else will be provided to you. When doing this on a test, you don't need to match the Python code given here exactly; you just need to create Python code that will implement the correct algorithm.
Swapping two list items
Assuming, we are swapping positions X and Y:
Copy the value at position X into a temporary variable
Copy the value at position Y into position X
Copy the temporary variable into position Y
my_list = ["A", "B", "C", "D", "E"]
#################################
## GOAL: Swap positions 1 and 3
################################
temp = my_list[1]
my_list[1] = my_list[3]
my_list[3] = temp
print(my_list)
Inserting an item into a list
Add an empty position at the end of the list
Starting at the end of the list and working towards the insertion position:
Replace the value at the current position with the value to its left
Copy the value to be inserted into the insertion position
nums = [10, 9, 8, 6, 5]
##################################
## GOAL: Insert 7 into position 3
##################################
INSERTION_POSITION = 3
nums.append(0) # Add a dummy value onto the end of the list
# Shift all elements over one position to the right,
# starting at the end of the list and moving to the left
# until reaching our insertion position
for i in range(len(nums)-1, INSERTION_POSITION, -1):
nums[i] = nums[i-1]
nums[3] = 7 # Now we can copy the 7 into position 3
print(nums)
Deleting an item from a list
Starting at the insertion position and working towards the end of the list:
Replace the value at the current position with the value to its right
Snip off the last value of the list
fruits = ["apples", "peaches", "tuna", "pears", "plums"]
################################################
## GOAL - remove the element in postion 2 (tuna)
################################################
DELETION_POSITION = 2
# Starting at our deletion position and continuing to the end of the list,
# shift all elements to the left
for i in range(DELETION_POSITION, len(fruits)-1):
fruits[i] = fruits[i+1]
# Remove the last item in the list
fruits = fruits[:-1]
print(fruits)
Resist the temptation to strictly memorize the code here without fully understanding how it works. You will forget what you memorize quickly, but if you can make sense of all the logic it will stay with you!