Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMPSA Exam Prep Session On: April 8th from 1:30-3:00 Location TBA Winter 2016CISC101 - Prof. McLeod1.

Similar presentations


Presentation on theme: "COMPSA Exam Prep Session On: April 8th from 1:30-3:00 Location TBA Winter 2016CISC101 - Prof. McLeod1."— Presentation transcript:

1 COMPSA Exam Prep Session On: April 8th from 1:30-3:00 Location TBA Winter 2016CISC101 - Prof. McLeod1

2 The Arts and Science Undergraduate Society's Lost Paws's bi-annual animal de-stressing event "Critters on Campus"- will be happening in the JDUC March 29th, 30th and 31st. This event provides students from all faculties with the opportunity to interact with a dog or a cat prior to exam time as a means of stress relief. Dog Walking Sign Up: http://tinyurl.com/lostpawsdogwalk Dog/Cat Cuddling Sign Ups: Tuesday http://tinyurl.com/tuesdaysignups Wednesday http://tinyurl.com/wednesdaysignups Thursday http://tinyurl.com/thursdaysignups Winter 2016CISC101 - Prof. McLeod2 Critters on Campus!

3 Today… Algorithms, Cont.: –Bubble Sort. –Compare search timings. –Sorting, Cont. Visualize: Insertion Selection Bubble Start GUI with Tkinter. Winter 2016CISC101 - Prof. McLeod3

4 4 Bubble Sort Is best envisioned as a vertical column of numbers as bubbles. The larger bubbles gradually work their way to the top of the column, with the smaller ones pushed down to the bottom. Pseudocode: Loop through list from i=0 to the end: –Loop down from the last element in the list to i. -Swap adjacent elements if they are in the wrong order. Winter 2016

5 CISC101 - Prof. McLeod5 Bubble Sort, Cont. def bubbleSort(numsList): size = len(numsList) for i in range(0, size): j = size - 1 while j > i : if numsList[j] < numsList[j - 1]: swap(numsList, j, j - 1) j = j - 1 Winter 2016

6 CISC101 - Prof. McLeod6 2712318117 3271271811 3727121118 3711271218 3711122718 3711121827

7 CISC101 - Prof. McLeod7 Bubble Sort, Cont. Note that both the comparison and the swap are inside the inner loop (yuk!). Winter 2016

8 CISC101 - Prof. McLeod8 Bubble Sort – A Slight Improvement def bitBetterBubbleSort(numsList): size = len(numsList) i = 0 isSorted = False while i < size and not isSorted: j = size - 1 isSorted = True while j > i : if numsList[j] < numsList[j - 1]: swap(numsList, j, j - 1) isSorted = False j = j - 1 i = i + 1 Winter 2016

9 CISC101 - Prof. McLeod9 Bubble Sort, Cont. Possibly the simplest sorting algorithm to code. Also the slowest sorting algorithm! On average, bubble sort makes n (the size of the dataset) times more moves than selection or insertion sort. Winter 2016

10 CISC101 - Prof. McLeod10 Sorting Animations For a collection of animation links see: http://www.hig.no/~algmet/animate.html Here are a few that are OK: http://sorting.at/ http://math.hws.edu/eck/jsdemo/sortlab.html https://www.cs.usfca.edu/~galles/visualization/ComparisonS ort.html And youtube has videos of many other animations. Winter 2016

11 CISC101 - Prof. McLeod11 Demo Program See ModifiedSortingTestBed.py How do our sorts compare to list.sort()? (Not very well. How is.sort() so fast?) Winter 2016

12 CISC101 - Prof. McLeod12 Timings, Summary For 1000 data points between 1 and 1000, random order case. SortMillisec Python sort()0.36 Insertion100 Selection125 Bubble287 Better?Bubble279 Winter 2016

13 Observations Insertion sort works remarkably well on data that is almost in order. Selection sort doesn’t care much about the state of the data. Bubble sort is always the worst sort, but it behaves slightly better when the data is more ordered. Winter 2016CISC101 - Prof. McLeod13

14 You are Not Responsible for These Experiments! If I double the size of the list to be sorted do the timings double? Is there a practical limit to the size of the list that can be sorted with our simple sorts? In other words, when do I switch to Quicksort? Binsort is remarkably fast! Why? What is the big restriction on using this sort? Winter 2016CISC101 - Prof. McLeod14

15 GUI Development in Python “Graphical User Interfaces”! We’ll stick with a distributed module called tkinter. It is easy to learn and use and we know it will work with out current version of Python. But there are many other packages and tookits! CISC101 - Prof. McLeod15Winter 2016

16 CISC101 - Prof. McLeod16 GUI with Tkinter Stands for “Tk Interface”. It is a Python interface to the Tk GUI toolkit, which is maintained by ActiveState (www.activestate.com, they also distribute a free Python development tool called ActivePython, which is an alternative to IDLE). Tk consists of a bunch of components (called “widgets”) developed in C++, which can be used with other languages like Perl or Ruby. See section 25.1 in the Python Library Reference. Winter 2016

17 CISC101 - Prof. McLeod17 Some Tkinter Widgets Button – something to click on! Canvas – to draw on or display graphics. Checkbutton – a checkbox – on or off. Entry – single line text entry. Frame – container for other widgets. Label – displays one line of text that the user cannot change. Listbox – drop down list for user selection. Menu – a list of choices displayed by a: Menubutton Winter 2016

18 CISC101 - Prof. McLeod18 Some Tkinter Widgets, Cont. Message – shows multiple lines of text. Radiobutton – appear in groups where the user can only select one. Scale – a slider. Scrollbar – allows scrolling for other widgets. Text – user can enter multiple lines of text. Winter 2016

19 CISC101 - Prof. McLeod19 Using the tkinter Module One note: IDLE is programmed itself using tkinter and this might cause some strange results. This does not seem to be a problem with Python 3.#, so far… First you must import the tkinter module: import tkinter Or: from tkinter import * If this does not work, then you may have to install the module – but, for Windows Python installs, it should already be there. Winter 2016

20 CISC101 - Prof. McLeod20 Using tkinter, Cont. Create your main window (or “root” window) using: top_window = tkinter.Tk() Then start the main loop using: tkinter.mainloop() (This assumes you used the first import statement.) Winter 2016

21 CISC101 - Prof. McLeod21 Using tkinter, Cont. The first line: top_window = tkinter.Tk() invokes the constructor method for the Tk object and now top_window is an object of this type. The main loop is the main “listener” loop for the window – it will wait very patiently for the user to do something! See Window1.py. Note functionality built into even this simple window! Winter 2016

22 CISC101 - Prof. McLeod22 Add a Label See Window2.py: helloWorldLabel = tkinter.Label(top, text="HelloWorld!") helloWorldLabel.pack() The pack() is invoked to place the label into the first available position in the window. Pretty small font – see Window3.py How can we get away from this top-down stacking of widgets in the window? Winter 2016

23 CISC101 - Prof. McLeod23 Using Frames See Window4.py Create a Frame, add labels to the Frame, pack the labels and specify a relative position in the Frame, then pack the Frame. Winter 2016

24 So Far… Can change the window title. Add labels. Use frames. But, using the packer makes for some pretty ugly designs!! CISC101 - Prof. McLeod24Winter 2016

25 CISC101 - Prof. McLeod25 The Grid Manager The “packer” is very simple but just jams stuff in from top to bottom. You must use frames to get more than one widget in a row. The Grid manager is an alternative that is just a bit harder to use. You specify a row and column position instead. Do not mix the packer and grid together – use one or the other! Winter 2016

26 CISC101 - Prof. McLeod26 The Grid Manager, Cont. Grid method syntax: grid(row=?, column=?, rowspan=?, columnspan=?, sticky="news", ipadx=?, ipady=?, padx=?, pady=?) "news" is North, East, West, and/or South. row, column positions and rowspan, columnspan are ints. Other “pad” dimensions are in pixels. What is a pixel, anyways? Winter 2016

27 CISC101 - Prof. McLeod27 The Grid Manager, Cont. See Window5.py Note how the columns and rows are sized to the width and height of the largest widget in that row or column. Widgets are centre-aligned by default – change this using the sticky option. They can span multiple rows and columns – use rowspan or columnspan (or both!). Add extra “padding” using padx and pady. Winter 2016


Download ppt "COMPSA Exam Prep Session On: April 8th from 1:30-3:00 Location TBA Winter 2016CISC101 - Prof. McLeod1."

Similar presentations


Ads by Google