10. List and Reference

10.1. Before Kattis

1

Run the below code and figure out what exactly is going on. 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 bits. Do whatever you need to do to figure it out.

1a = [0, 0, 0]
2b = [a,a,a]
3
4for row in b:
5    toPrint = ''
6    for d in row:
7        toPrint += str(d) + ' '
8
9    print(toPrint)

2

Add b[1][1] = 1 to line 3 and run it again. Is this what you wanted? (no, it’s not). Fix this code such that the output will be what’s below. Hint: Change the list setup part, not the loops.

0 0 0
0 1 0
0 0 0

2.5

Change line 3 b[0][1] = 1 and hit run. Did it do what you expected?

3

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

You may assume that each input would have exactly one solution, and you may not use the same element twice.

EXAMPLE

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,

return [0, 1].

10.2. Kattis Problems

These are the same problems from last week. They’re good problems. Keep going. Start where you left off.

Grab a scrap piece of paper to start scratching your ideas down on paper. The problems are getting tricky enough where this really is becoming a requirement.

  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

10.3. LeetCode Problems

If you have somehow finished everything so far, go check out LeetCode. Sort the problems by Acceptance (click the table header) and start seeing if you can solve some of these problems.