9. 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
9.1. Pre Lab Exercises
For all exercises
Use
assert
to test instead of theirtest
function
-
2 — note the portions of solutions within the chapter text
Write assertion tests for each of your functions above
9.2. Before Kattis
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)
Add
b[1][1] = 1
to line 3 and run it again and see what happensIs this what you expected?
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
Add
b[0][1] = 1
to line 3 and run it again and see what happensIs this what you expected?
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]
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