9. References & More Lists

  • Feel free to use your laptop if you have it

  • Ensure I have recorded your completion — failure to do so will result in a grade of 0

  • I strongly encourage you to work with others in the lab

    • When you get stuck, do me a favour and ask those sitting around you for help

    • I want people to get used to working together in the labs

    • Peer teaching and peer learning is super effective


To obtain full marks for the lab, you must:

  1. Have completed the pre-lab exercises

  2. Have been working on the lab content

  3. Demonstrate competency in the topics

9.1. Pre Lab Exercises


You must have completed the specified exercises prior to the start of the lab. If you have not come to lab prepared, you will be asked to leave and you will obtain a grade of 0 for the lab.

  • For all exercises

    • Use assert to test instead of their test function

  1. Chapter 14 exercise(s)

    • 2 — note the portions of solutions within the chapter text

  2. Write assertion tests for each of your functions above

9.2. Before Kattis

  1. Run the following code and figure out what what it does and how it does it

    • I want to see everyone with paper here working through the logic

    • If you can’t figure it out, hack around with the code

      • Comment out bits

      • Print out values

      • Do whatever you need to do to figure it out

    1a = [0, 0, 0]
    2b = [a,a,a]
    4for row in b:
    5    toPrint = ''
    6    for d in row:
    7        toPrint += str(d) + ' '
    9    print(toPrint)
  2. Add b[1][1] = 1 to line 3 and run it again and see what happens

    • Is this what you expected?

  3. Alter the code such that the output will be what’s below.

    • Change the list setup/creation part, not the loops

    0 0 0
    0 1 0
    0 0 0
  4. Add b[0][1] = 1 to line 3 and run it again and see what happens

    • Is this what you expected?

  5. Given a list of integers, return, in a list, indices of two numbers such that they add up to a specific target

    • You may assume that each input would have exactly one solution

    • You may not use the same element twice

    • For example, given nums = [2, 7, 11, 15], target = 9

      • Since nums[0] + nums[1] = 2 + 7 = 9

      • Return [0, 1]

  6. Think about how much work your algorithm for the previous question had to do in terms of \(n\), where \(n\) is the length of the list

    • Can you somehow think of a better algorithm that would do less work?

    • Don’t spend too much time on this if you can’t come up with a solution

    • Hint: Dictionaries (but leave the nums list alone)

9.3. Kattis Problems

  • You should be using a scrap piece of paper to work out the ideas for the following problems

    • The problems you are to solve are getting too complex to try to solve by just coding

    • Trying to solve problems by just typing away will not yield success

  1. https://open.kattis.com/problems/bijele

  2. https://open.kattis.com/problems/cold

  3. https://open.kattis.com/problems/nastyhacks

  4. https://open.kattis.com/problems/grassseed

  5. https://open.kattis.com/problems/pet

  6. https://open.kattis.com/problems/batterup

  7. https://open.kattis.com/problems/aboveaverage

  8. https://open.kattis.com/problems/icpcawards

  9. https://open.kattis.com/problems/quickbrownfox

  10. https://open.kattis.com/problems/nodup

  11. https://open.kattis.com/problems/conundrum

  12. https://open.kattis.com/problems/bela

  13. https://open.kattis.com/problems/kornislav


Ensure that your your completion has been recorded. Failure to do so may result in a grade of 0.