10. References & More Lists

  • Feel free to use your laptop

  • You are strongly encourage to work with others

    • When you get stuck, ask those sitting around you for help

    • Get used to working together in the labs

    • Peer teaching and peer learning has been empirically shown to be very effective

10.1. Pre Lab Exercises

  • 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

10.2. Before Kattis

  1. Run the following code and figure out 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]
    3
    4for row in b:
    5    to_print = ''
    6    for d in row:
    7        to_print += str(d) + ' '
    8
    9    print(to_print)
    
  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)

10.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