Presentation is loading. Please wait.

Presentation is loading. Please wait.

Winter 2019 CISC101 5/30/2019 CISC101 Reminders

Similar presentations


Presentation on theme: "Winter 2019 CISC101 5/30/2019 CISC101 Reminders"— Presentation transcript:

1 Winter 2019 CISC101 5/30/2019 CISC101 Reminders Assignment 4 due this Friday. Use lists, sets, dictionaries and design and write functions to solve some data mining exercises. Quiz 3 grading nears completion… Quiz 4 next week! Topics on next slide. Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

2 Quiz 4 Topics Functions, loops, conditionals, lists, file I/O – STILL.
New topics: Sets. Strings. Raising Exceptions. Passing by Reference. Dictionaries. Algorithm Topics (more details on Friday). Turtle, os, os.path, sys, urllib.request and exec() BIF not on quiz. Winter 2018 CISC101 - Prof. McLeod

3 Today Finish: Passing by Reference. Algorithms:
Find minimums, maximums and obtaining sums. Timing code execution. Sequential search (if we have time). Winter 2019 CISC101 - Prof. McLeod

4 CISC101 Passing by Reference Can a function change something in its parameter list and have the change stay (or “stick”), even when the function is complete? What kinds of arguments can be changed and how? See TestPassingByReference.py Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

5 Passing by Reference, Cont.
Observations: Immutable objects (the int, the string and the tuple) do not stay changed outside the function. (All you can do inside the function is re-assign them.) Re-assigning a mutable object, a list, does not change it. However, element by element changes (using the slice operator only!) or invoking a method belonging to a list does allow the changes to stay after the function is complete. Winter 2019 CISC101 - Prof. McLeod

6 Passing by Reference, Cont.
When you pass a list (or any object) into a function, you do not re-create the entire structure inside the function. That would be wasteful and time-consuming! Instead you just pass a reference (a memory address, or “pointer”) into the function. If the object is mutable, and its elements are changed or deleted inside the function, then that change is made to the structure created outside the function. Winter 2019 CISC101 - Prof. McLeod

7 Passing by Reference, Cont.
We will take advantage of being able to pass mutable objects (especially lists) by reference to simplify code! This also gives you a way to get more than one list out of a function without having to return a tuple of lists. But, if you are doing this maybe your function is not just doing one thing! And, returning multiple things through the parameter list can make for confusing code. Winter 2019 CISC101 - Prof. McLeod

8 Algorithms Let’s take a break from Python syntax for a while!
Instead – use Python code to explore various useful algorithms. Make comparisons based on ease of coding, flexibility and efficiency (speed!). Winter 2019 CISC101 - Prof. McLeod

9 CISC101 What is an Algorithm? An algorithm is a set of instructions to solve a particular problem. A computing algorithm can be expressed in code. In any language, including Python! We are going to learn some simple algorithms that every programmer should know. Focus on Searching and Sorting algorithms. (Avoid recursive algorithms.) Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

10 Finding Min’s, Max’s and Sums
Naturally Python has BIFs for this! min(), max() and sum() Used as min(iter, key=None), or min(arg0, arg1, arg2, …, key=None), and sum(iter[, start]) iter is a list or tuple (or string), or an iterable. (Optional key can point to a function that can be used to determine the order of the elements. We won’t use this.) Winter 2019 CISC101 - Prof. McLeod

11 Finding Min’s and Max’s, Cont.
Sometimes you have to do this yourself. (And sometimes your version will work better!) A function that returns the minimum of a simple list: def findMin(aList): min = aList[0] i = 1 while i < len(aList) : if aList[i] < min : min = aList[i] i = i + 1 return min Winter 2019 CISC101 - Prof. McLeod

12 Finding Min’s and Max’s, Cont.
Maybe you want to know the position of the minimum, not the value. Could you use a for loop instead? Would it be safe to say min = 0 ? See FindMinMaxSumDemo.py Winter 2019 CISC101 - Prof. McLeod

13 Finding Min’s and Max’s, Cont.
Note how the functions work with lists of other types. And, note that the built-in sum() works only with lists of numbers – we can modify our sum and our Min/Max functions to work with the other list types or even a mix of types in the same list. Winter 2019 CISC101 - Prof. McLeod

14 Two Questions… Would it be faster to sort the list first and then just get the first or last values as the Min and Max? If you had to write a function that returns the min, the max and the sum at the same time – what is the best way to do it? How many loops? Winter 2019 CISC101 - Prof. McLeod

15 Min, Max & Sum: Summary Finding minimums, maximums and making sums are all very similar algorithms. Python has BIFs for these since they are so often used. Writing our own versions allows us to add flexibility to how the code works. Is our code as fast as the BIF version? Winter 2019 CISC101 - Prof. McLeod

16 Comparing Code Efficiency
We are always concerned about making code easily read, repaired and modified. That’s just good style. But we are also concerned about making code efficient. Two measures of interest: memory use and time of execution. Winter 2019 CISC101 - Prof. McLeod

17 Execution Time Code runs very quickly on modern computers.
But, sometimes it needs to be faster! (In CISC121). You can make a theoretical analysis of code to predict what algorithm may be faster. Or, You can just measure execution times. Or, You do both! Winter 2019 CISC101 - Prof. McLeod

18 How to Measure Execution Time?
One way is to use the perf_counter() function from the time module. It returns the number of seconds elapsed from the first time the function is invoked as a float. We won’t care about the absolute value from the function, we just want the relative change to get the time elapsed by a particular function call. (If you need to measure really short times use perf_counter_ns().) Winter 2019 CISC101 - Prof. McLeod

19 Timing Code Execution, Cont.
CISC101 Timing Code Execution, Cont. So, just for fun, let’s compare our findMax to the max() BIF. First we will have to enlarge the list so that it will take enough time to measure. We can fill it with random numbers as “fodder”. See TimingFindMaxDemo.py. Which one is slower? Can we speed up our findMax() function? Note how timings change from one run to the next. Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

20 Results These times will depend on other factors such as your microprocessor speed and type as well as your operating system. But, the relative order of the results should not change. BIF max() function milliseconds Version 1 of findMax 2.3 msec Version msec Version msec Version msec Winter 2019 CISC101 - Prof. McLeod

21 findMax() Versions Version 1 is the one shown in the previous demo.
Version 2 – the len() BIF call is moved outside the loop. Version 3 – a for loop with the range() BIF Version 4 – a simple for loop – the best version we could code! The max() BIF is still a bit faster. Our code is interpreted. The max() BIF is probably already in machine language is some library. Tough to compete with this! Winter 2019 CISC101 - Prof. McLeod

22 Better Code Execution, Cont.
For faster execution: Use the BIF if you can. Use a for loop instead of a while loop. Minimize the operations that will be in the loop. Use the best algorithm you can! Experiment, if you need to. At times, you may find that you will need to sacrifice code brevity and readability in order to get better speed. Winter 2019 CISC101 - Prof. McLeod

23 Execution Time: Summary
Execution time is an important measure of the efficiency of our code. We might not always notice, but sometimes execution speed is the bottleneck of a program and it is worth knowing what can be done to speed things up! Many BIFs exist as binary executable code in dynamic link libraries (*.dll files). This makes them execute as quickly as possible. Winter 2019 CISC101 - Prof. McLeod

24 Aside – Memory Usage Using larger amounts of data means that we are going to need larger amounts of RAM to store all that data in memory. It will take longer to process larger amounts of data, but that is a result of code execution time, not a result of using more memory. As long as we don’t run out of memory!! If this happens, then swapping RAM contents to your hard disk will take place and then this will really slow you down! Winter 2019 CISC101 - Prof. McLeod

25 Algorithms, Cont. - Searching
CISC101 Algorithms, Cont. - Searching We will look at two algorithms: Sequential Search Brute force! Works on a dataset in any order. Binary Search Fast! Only works on sorted datasets. Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

26 Searching in Python We already have searching methods as well as the keywords in and not in. Lists: count(), index() Strings: various find(), count() and index() methods. A search could result in a count of occurrences, a True or False or just the location of the first match. So, why do we need to write our own searching functions? Winter 2019 CISC101 - Prof. McLeod

27 Searching in Python, Cont.
You might need to search datasets in a programming language that does not have this code built-in. Or your dataset structure might not be amenable for use with the built-in methods. So, you need to know these algorithms! Winter 2019 CISC101 - Prof. McLeod

28 Searching, Cont. Sequential search pseudocode:
Loop through dataset starting at the first element until the value of target matches one of the elements. Return the location of the match. If a match is not found, raise a ValueError exception. (Note that the list.index() method also raises a ValueError exception if the value is not located…) Winter 2019 CISC101 - Prof. McLeod

29 Sequential Search As code:
def sequentialSearch(numsList, target) : i = 0 size = len(numsList) while i < size : if numsList[i] == target : return i i = i + 1 raise ValueError("Target not found.") Note how len(numsList) is done outside the loop. Winter 2019 CISC101 - Prof. McLeod

30 Sequential Search, Version 2
def sequentialSearch2(numsList, target) : for i in range(len(numsList)) : if numsList[i] == target : return i raise ValueError("Target not found.") Using our trusty for loop. Is this one faster? Winter 2019 CISC101 - Prof. McLeod

31 Timing our Search See: TimingSeqSearchDemo.py
Look at normal case and worst-case. Note how exception is raised and caught. The farther the target is from the beginning of the dataset, the longer the search takes. Makes sense! Our fastest sequential search is still about 4 times slower than index(). Why? Winter 2019 CISC101 - Prof. McLeod


Download ppt "Winter 2019 CISC101 5/30/2019 CISC101 Reminders"

Similar presentations


Ads by Google