Winter 2019 CISC101 5/30/2019 CISC101 Reminders

Slides:



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

Lists Introduction to Computing Science and Programming I.
Lists in Python.
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.
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:
Algorithmic complexity: Speed of algorithms
Containers and Lists CIS 40 – Introduction to Programming in Python
CMPT 120 Topic: Searching – Part 1
EGR 2261 Unit 4 Control Structures I: Selection
CISC/CMPE320 - Prof. McLeod
Fall 2017 CISC124 9/21/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
CISC/CMPE320 - Prof. McLeod
Winter 2018 CISC101 11/9/2018 CISC101 Reminders
CISC101 Reminders Quiz 2 this week.
CISC101 Reminders Quiz 2 this week.
CMSC 341 Prof. Michael Neary
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
More Loop Examples Functions and Parameters
Winter 2018 CISC101 11/19/2018 CISC101 Reminders
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
CISC101 Reminders Slides have changed from those posted last night…
Winter 2018 CISC101 12/1/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
CISC/CMPE320 - Prof. McLeod
Winter 2019 CISC101 2/17/2019 CISC101 Reminders
CISC124 Labs start this week in JEFF 155. Fall 2018
Fall 2018 CISC124 2/22/2019 CISC124 Quiz 1 This Week. Topics and format of quiz in last Tuesday’s notes. The prof. (me!) will start grading the quiz.
CISC/CMPE320 - Prof. McLeod
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
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 Assignment 2 due this Friday.
Winter 2019 CISC101 4/14/2019 CISC101 Reminders
CISC101 Reminders Quiz 1 marking underway.
Winter 2019 CISC101 4/29/2019 CISC101 Reminders
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
Winter 2019 CMPE212 5/10/2019 CMPE212 – Reminders
Winter 2019 CISC101 5/26/2019 CISC101 Reminders
CISC101 Reminders Assignment 3 due today.
CMPE212 – Reminders Assignment 2 due next Friday.
Introduction to Computer Science
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.29 milliseconds Version 1 of findMax 2.3 msec Version 2 1.5 msec Version 3 0.78 msec Version 4 0.39 msec Winter 2019 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 2019 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 code brevity and readability in order to get better speed. Winter 2019 CISC101 - Prof. McLeod

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

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

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

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

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

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

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

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

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