Winter 2018 CISC101 12/1/2018 CISC101 Reminders

Slides:



Advertisements
Similar presentations
Fundamentals of Python: From First Programs Through Data Structures
Advertisements

Data Structures Introduction. What is data? (Latin) Plural of datum = something given.
Winter 2006CISC121 - Prof. McLeod1 Stuff No stuff today!
Searching Topics Sequential Search Binary Search.
Today… The for loop. Introducing the Turtle! Loops and Drawing. Winter 2016CISC101 - Prof. McLeod1.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Today… Strings: –String Methods Demo. Raising Exceptions. os Module Winter 2016CISC101 - Prof. McLeod1.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Assignment 5 is posted. Exercise 8 is very similar to what you will be doing with assignment 5. Exam.
Quiz 4 Topics Aid sheet is supplied with quiz. Functions, loops, conditionals, lists – STILL. New topics: –Default and Keyword Arguments. –Sets. –Strings.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
Next Week… Quiz 2 next week: –All Python –Up to this Friday’s lecture: Expressions Console I/O Conditionals while Loops Assignment 2 (due Feb. 12) topics:
Data Structures I (CPCS-204) Week # 2: Algorithm Analysis tools Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
Algorithm Analysis 1.
Outline lecture Revise arrays Entering into an array
CMPT 120 Topic: Searching – Part 2
Algorithmic complexity: Speed of algorithms
CMPT 120 Topic: Searching – Part 1
EGR 2261 Unit 4 Control Structures I: Selection
Course Description Algorithms are: Recipes for solving problems.
CISC/CMPE320 - Prof. McLeod
Algorithms Furqan Majeed.
CISC101 Reminders Quiz 2 this week.
CISC101 Reminders Quiz 2 this week.
CISC101 Reminders Assn 3 due Friday, this week.
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
Winter 2018 CISC101 11/16/2018 CISC101 Reminders
Last Class We Covered Data representation Binary numbers ASCII values
Winter 2018 CISC101 11/19/2018 CISC101 Reminders
List Algorithms Taken from notes by Dr. Neil Moore
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
Objective of This Course
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
CISC101 Reminders Slides have changed from those posted last night…
Winter 2018 CISC101 11/29/2018 CISC101 Reminders
CISC101 Reminders Assn 3 due Friday, this week. Quiz 3 next week.
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
Winter 2018 CISC101 12/2/2018 CISC101 Reminders
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Winter 2018 CISC101 12/5/2018 CISC101 Reminders
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
CISC101 Reminders Quiz 2 this week.
Algorithmic complexity: Speed of algorithms
Fall 2018 CISC124 2/15/2019 CISC124 TA names and s will be added to the course web site by the end of the week. Labs start next week in JEFF 155:
Winter 2019 CISC101 2/17/2019 CISC101 Reminders
CISC/CMPE320 - Prof. McLeod
CMPE212 – Reminders The other four assignments are now posted.
CISC101 Reminders All assignments are now posted.
CISC101 Reminders Assn 3 sample solution is posted.
CISC101 Reminders Labs start this week. Meet your TA! Get help with:
CISC101 Reminders Assignment 2 due today.
Winter 2019 CISC101 4/16/2019 CISC101 Reminders
Algorithmic complexity: Speed of algorithms
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
CISC101 Reminders Quiz 1 marking underway.
Winter 2019 CISC101 4/29/2019 CISC101 Reminders
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
Course Description Algorithms are: Recipes for solving problems.
Winter 2019 CISC101 5/26/2019 CISC101 Reminders
CISC101 Reminders Assignment 3 due today.
Winter 2019 CISC101 5/30/2019 CISC101 Reminders
CMPE212 – Reminders Assignment 2 due next Friday.
Introduction to Python
Introduction to Computer Science
Presentation transcript:

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 coursefeedback@compsa.queensu.ca, if you are interested. Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

Today Start Algorithms. Begin with finding Minimums, Maximums and Sums. Searching – Sequential Search. Winter 2018 CISC101 - Prof. McLeod

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

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

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

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

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

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

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

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

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

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

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

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

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 0.24 milliseconds Version 1 of findMax 1.3 msec Version 2 0.89 msec Version 3 0.95 msec Version 4 0.37 msec Winter 2018 CISC101 - Prof. McLeod

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

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

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

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

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

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

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

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

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

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

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

Aside – Comparing Real Numbers Does 1.00000000001 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

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