Data Structures - Lab 03
Tuesday, September 8th
Exploring Complexity Analysis
Do not begin this lab until you receive directions from the instructor!
Today's lab will be different from our normal labs in that I will ask you to
work with a partner so that you can actually stop and think about some code and
talk it through with another student. I believe that one of the BEST ways
to learn something is to explain it to someone else.
Also, this lab will ask you to carefully consider and explain code before you
ever load it into the computer to run it. It is important that you have
made an honest effort to consider code and answer all questions before you
actually run the code. Again, I believe there is a lot to be learned from
considering the code before you do anything with it.
Activity Set A [Partner A writes code, Partner B completes
answer sheet]
- [Q1] What does this method appear to do?
- [Q2] How/where might it be used?
- To use this method enter it into a file named lab3.py
- Load it and type the following into the python shell (interactions window)
>>> data = range(0,10000,2)
>>> methodOne(data,100)
>>> methodOne(data,1000)
>>> methodOne(data,10000)
- [Q3] What answers did you get? Why?
Activity Set B [Partner B writes code, Partner A completes answer sheet]
- [Q4] What does this method appear to do?
- [Q5] How/where might it be used?
- [Q6] List at least two significant ways where methodTwo is different from methodOne.
- To use this method add it into the file named lab3.py
- Load it and type the following into the python shell (interactions window)
>>> data = range(0,10000,2)
>>> methodTwo(data,100)
>>> methodTwo(data,1000)
>>> methodTwo(data,10000)
- [Q7] What answers did you get? Why?
Activity Set C [Partner A writes code, Partner B completes answer sheet]
- [Q8] What is this code going to do?
- Add it to lab3.py and then run it. [Note, the results may take a while depending on your computer speed. Be patient]
- [Q9] Record your results in the correct cell in Table One.
Table One : computation speed for different methods under different size loads.
| search method |
testSize=5000 |
testSize=10000 |
| methodOne |
|
|
| methodTwo |
|
|
| methodThree |
|
|
| methodFour |
|
|
| methodFive |
|
|
- [Q10] Approximately how long do you predict it will take to run this
program if you double the testSize from 5000 to 10000?
- Do it. Record your answer in the table from Table One
- [Q11] Roughly how many times more is the ten thousand object test
when compared to the five thousand object test. Why???
- Modify the method call inside of the "target" loop to be methodTwo() instead
of methodOne(). Run this code for testSizes of five and ten thousand and record the results in the Table One
Activity Set D [Partner B writes code, Partner A completes answer sheet]
- [Q12] This code is very similar to the code from methodTwo() but adds in one new feature. What is this feature?
- [Q13] What do you guess is the purpose of this feature?
Add methodThree() to lab3.py and then run it. To test this code, try the following commands in the interactions pane and observe the results
>>> data1 = range(0,10)
>>> data1
>>> methodThree(data1,5)
>>> data2 = range(10,0,-1)
>>> data2
>>> methodThree(data2,5)
Notice that while 5 is contained in both lists methodThree() fails to locate it in data2
- [Q14] Now that you see this, what do you think is the purpose of this new feature? What assumptions does the code make about the data?
- Modify the method call inside of the "target" loop in timedTest() to be methodThree(). Run this code for testSize=5000 and record the results in the Table One
- [Q15] Approximately how long do you predict it will take to run this
program if you double the testSize from 5000 to 10000?
- Do it. Record your answer in the table from Table One
- [Q16] Roughly how many times more is the ten thousand object test
when compared to the five thousand object test. Why???
Activity Set E [Partner A writes code, Partner B completes answer sheet]
- [Q17] What does this code do? (Be specific)
This code is actually quite different from the previous methods. On the surface it might be difficult to understand what this actually does. In order to follow this a bit better, let's trace the code manually. Suppose that I type the following commands in the python shell:
>>> data = range(100)
>>> methodFour(data,45)
Consider the first time this code runs. What are the values for the variables first, last, and midpoint when the comment in the while loop is reached? As you should notice, the value of 45 is not found the first time that this runs, so the loop runs a second time, this time with different values of first, last, and midpoint.
- [Q18] BY HAND, consider the code for methodFour() and, in particular, the comment located early in the while loop. What values are stored in the variables for first, last, and midpoint each time this loop runs? Put your answers in the table below. Note, this table contains more rows then you will actually need. I want to see if you can detect how many times the loop ACTUALLY runs.
| Loop # | first | last | midpoint |
| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
- [Q19] The while loop says "while first<=last" under what conditions would this NOT be true?
- [Q20] What assumptions does this method make?
- Only now should you actually add it this code to lab3.py and then run it. Modify the method call inside of the "target" loop in timedTest() to be methodFour(). Run this code for testSize=5000 and record the results in the Table One.
- [Q21] Approximately how long do you predict it will take to run this
program if you double the testSize from 5000 to 10000?
- Do it. Record your answer in the table from Table One
- [Q22] Roughly how many times more is the ten thousand object test
when compared to the five thousand object test. Why???
Activity Set F [Partner B writes code, Partner A completes answer sheet]
- [Q23] What does this code do? (Be specific)
- [Q24] How is this code different from methodFour?
- Add this code to lab3.py and modify the method call inside of the "target" loop to be methodFive(). Run this code for testSize=5000 and record the results in the Table One.
- [Q25] Approximately how long do you predict it will take to run this
program if you double the testSize from 5000 to 10000?
- Do it. Record your answer in the table from Table One
- [Q26] Roughly how many times more is the ten thousand object test
when compared to the five thousand object test. Why???
Wrap Up [You pick who fills out the answer sheet]
- [Q27] Consider the methods you worked with. Which was the most efficient (the fastest)?
- [Q28] Which one was the least effected by the doubling from 5000 to 10000?
- [Q29] Which method would you prefer to use?
- [Q30] Under which circumstances might you use one of the others?
- [Q31] Which method would you prefer to write from scratch off the top of your head (no reference materials)?