Download presentation
Presentation is loading. Please wait.
Published byΕυλάλιος Ρέντης Modified over 6 years ago
1
Winter 2018 CISC101 12/1/2018 CISC101 Reminders Assignment 4 due this Friday. (Consider using a dictionary to hold the word frequencies…) Last quiz next week (to avoid week 12). Topics in slides from last lecture. Class rep needed for USATs – please contact Cindy Wu at if you are interested. Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod
2
Today Start Algorithms. Begin with finding Minimums, Maximums and Sums. Searching – Sequential Search. Winter 2018 CISC101 - Prof. McLeod
3
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 2018 CISC101 - Prof. McLeod
4
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 2018 CISC101 - Prof. McLeod Prof. Alan McLeod
5
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 2018 CISC101 - Prof. McLeod
6
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 2018 CISC101 - Prof. McLeod
7
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 2018 CISC101 - Prof. McLeod
8
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 2018 CISC101 - Prof. McLeod
9
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 2018 CISC101 - Prof. McLeod
10
Summary, So Far… 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 2018 CISC101 - Prof. McLeod
11
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 2018 CISC101 - Prof. McLeod
12
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. Winter 2018 CISC101 - Prof. McLeod
13
How to Measure Execution Time?
One way is to use the clock() function from the time module. It returns the number of seconds elapsed from the first time the function is invoked. 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. Winter 2018 CISC101 - Prof. McLeod
14
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 2018 CISC101 - Prof. McLeod Prof. Alan McLeod
15
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 1.3 msec Version msec Version msec Version msec Winter 2018 CISC101 - Prof. McLeod
16
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 2018 CISC101 - Prof. McLeod
17
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 some code brevity and readability in order to get better speed. Winter 2018 CISC101 - Prof. McLeod
18
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 2018 CISC101 - Prof. McLeod
19
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 2018 CISC101 - Prof. McLeod Prof. Alan McLeod
20
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 2018 CISC101 - Prof. McLeod
21
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 2018 CISC101 - Prof. McLeod
22
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 2018 CISC101 - Prof. McLeod
23
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 loop. Winter 2018 CISC101 - Prof. McLeod
24
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? Can this be coded without using the index, i? Winter 2018 CISC101 - Prof. McLeod
25
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 2018 CISC101 - Prof. McLeod
26
Other Searches Return True if a match exists.
Return a count of how many values match. Return a list of locations that match (not built-in to Python). Return the location of the match searching from the end of the list. … Winter 2018 CISC101 - Prof. McLeod
27
Aside – Comparing Real Numbers
Does equal 1? To compare floating point numbers you need to know something about the accuracy of the numbers. Suppose that your numbers are accurate to 1 part in 10,000. Instead of comparing using ==, use something like: if abs(numsList[i] – target) <= 1e-4: return i Winter 2018 CISC101 - Prof. McLeod
28
Summary The sequential search algorithm works for any kind of dataset in any order. It is a “brute force” technique. You now know how to build your own sequential search function if Python’s built-in searches won’t do the trick. Next: binary search. Winter 2018 CISC101 - Prof. McLeod
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.