ISF DP Computer Science

Lists #

This lab introduces Lists, a built-in Python data structure.


Syllabus Topics [SL] #

  • B2.2.2 Construct programs that apply arrays and Lists.
  • B2.4.2 Construct and trace algorithms to implement a linear search and a binary search for data retrieval.

Key Vocabulary #

WordDefinition
ArrayA data structure that stores elements that are accessible by an index.
IndexAn integer that represents the position in a data structure
Static Data structureA data structure that has a fixed size and will not grow or shrink.
Dynamic Data structureA data structure that can change its size as needed as elements are added or deleted.
Python ListA built-in Python data structure. It implements a dynamic array.
AppendTo add an item to the end of a List
Linear SearchTo find an item in a List by iterating over all of the items in the List, until the current item is equivalent to the target item

What is a list? #

๐Ÿ“– Operations for using Lists

# create an empty list
num_list = []

# create a list with items
dessert_list = ["ice cream", "brownies", "mochi", "timtams"]

# access item in list by index
dessert_list[0]     # returns "ice cream"
dessert_list[-1]     # returns "timtams"

# count number of items in list
len(dessert_list)   # returns 4

# loop through a list with indexing
for i in range(len(dessert_list)):
    print(dessert_list[i])

๐Ÿ“– Operations for altering Lists

dessert_list = ["ice cream", "brownies", "mochi", "timtams"]

# set a particular item by index
dessert_list[2] = "egg tart" # ["ice cream", "brownies", "egg tart", "timtams"]
list[-1] = "chocolate" # ["ice cream", "brownies", "egg tart", "chocolate"]

# add item to end of list
dessert_list.append("cookies")  # ["ice cream", "brownies", "egg tart", "chocolate", "cookies"]

# remove item from list
dessert_list.remove("brownies")  # ["ice cream", "egg tart", "chocolate", "cookies"]

# insert item at specific index of list
dessert_list.insert("cookies", 2) # ["ice cream", "egg tart", "cookies", "chocolate", "cookies"]

[0] Set up #

๐Ÿ’ป Go to your dpcs folder and create a new folder for this unit.

cd ~/desktop/dpcs/
mkdir unit02_data_structures
cd unit02_data_structures

๐Ÿ’ป Clone your repo. This will copy it onto your computer. Be sure to replace yourgithubusername with your actual username.

git clone https://github.com/isf-dp-cs/lab_lists_yourgithubusername
๐Ÿ’ป In the Terminal, type the following command to open the lab folder.
cd lab_lists_yourgithubusername

๐Ÿ’ป Enter the Poetry Shell to start the lab. As a reminder, we will run this command at the start of each lab, but only when we are inside a lab folder.

poetry shell
๐Ÿ‘พ ๐Ÿ’ฌ Exiting the poetry shell

When you want to exit the shell, you can type exit or ^D


[1] Using Lists: Numbers #

๐Ÿ’ป In number_analysis.py, write 6 functions that take a list of numbers as a parameter. Each function will perform an different analysis on the list.

  • total_sum(num_list)
  • contains(num_list, num)
  • reverse_list(num_list)
  • count(num_list, num)
  • minimum(num_list)
  • maximum(num_list)
  • biggest_difference(num_list)

๐Ÿ’ป For each function, write at least 2 tests using assert at the bottom of the file.

assert total_sum([2,4,6]) == 12
  • total_sum([2,4,6]) is the function call with a specific parameter as a test case
  • == 12 is what the function should return
  • an error will print to the Terminal if the assert condition does not pass

โœ… Check your functions with a peer

๐Ÿ“ Practice Coding by Hand

  1. Take out a piece of paper and write out entire the function for total_sum(num_list) without looking at your solution.

  2. Double check your handwritten code against your typed code.

  3. If you made mistakes, take note of them and try again with another function. Continue until you complete one without errors.


[2] Creating Lists: Words #

Convert text file to a List #

๐Ÿ’ป In get_words.py write a function get_word_list(file) to properly format the txt file. It should read in the given file and convert it into a List of words. Be sure to remove all newline characters \n from the words!

Get Words #

๐Ÿ’ป In get_words.py write 4 functions that take a list of words a parameter. Each function will create and return a new list that contains only some of the words.

  • get_words_of_length(word_list, length)
  • get_words_including(word_list, including_string)
  • get_words_starting(word_list, starting_string)
  • get_words_ending(word_list, ending_string)

๐Ÿ’ป Use the functions to answer the questions at the bottom of the file. Each question applies to the words_100k.txt file.

โœ… Check your answers with a peer

๐Ÿ“ Practice Coding by Hand

  1. Take out a piece of paper and write out entire the function for get_words_of_length(word_list, length).

  2. Double check your handwritten code against your typed code.

  3. If you made mistakes, take note of them and try again with another function. Continue until you complete one without errors.


[3] Deliverables #

โšกโœจ Once you finish the lab, be sure to complete these two steps:

๐Ÿ“‹ Update Syllabus Checklist: Go to your Syllabus Content Checklist in your Google Drive and update it accordingly.

๐Ÿ’ป Push your work to Github

  • git status
  • git add -A
  • git status
  • git commit -m “describe your code here”
  • git push
  • remote


[4] Extension #

This exercise comes from Project Euler.

In the card game poker, a hand consists of five cards and are ranked, from lowest to highest, in the following way:

  • High Card: Highest value card.
  • One Pair: Two cards of the same value.
  • Two Pairs: Two different pairs.
  • Three of a Kind: Three cards of the same value.
  • Straight: All cards are consecutive values.
  • Flush: All cards of the same suit.
  • Full House: Three of a kind and a pair.
  • Four of a Kind: Four cards of the same value.
  • Straight Flush: All cards are consecutive values of same suit.
  • Royal Flush: Ten, Jack, Queen, King, Ace, in same suit.

The cards are valued in the order: 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace.

If two players have the same ranked hands then the rank made up of the highest value wins; for example, a pair of eights beats a pair of fives (see example 1 below). But if two ranks tie, for example, both players have a pair of queens, then highest cards in each hand are compared (see example 4 below); if the highest cards tie then the next highest cards are compared, and so on.

Consider the following five hands dealt to two players:

HandPlayer1Player2Winner
15H 5C 6S 7S KD Pair of Fives2C 3S 8S 8D TD Pair of EightsPlayer 2
25D 8C 9S JS AC Highest card Ace2C 5C 7D 8S QH Highest card QueenPlayer 1
32D 9C AS AH AC Three Aces3D 6D 7D TD QD Flush with DiamondsPlayer 2
44D 6S 9H QH QC Pair of Queens Highest card Nine3D 6D 7H QD QS Pair of Queens Highest card SevenPlayer 1
52H 2D 4C 4D 4S Full House With Three Fours3C 3D 3S 9S 9D Full House with Three ThreesPlayer 1

๐Ÿ’ป Download the file poker.txt It contains one-thousand random hands dealt to two players.

  • Each line of the file contains ten cards (separated by a single space): the first five are Player 1’s cards and the last five are Player 2’s cards.
  • You can assume that all hands are valid (no invalid characters or repeated cards), each player’s hand is in no specific order, and in each hand there is a clear winner.

๐Ÿ’ป Create a new file poker_extension.py and answer the question: How many hands does Player 1 win?