Munster Programming Training - Cycle 2

Similar documents
CS 206 Introduction to Computer Science II

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

CSC148 Week 2. Larry Zhang

Postfix (and prefix) notation

Stacks. Revised based on textbook author s notes.

CS W3134: Data Structures in Java

Stacks and queues (chapters 6.6, 15.1, 15.5)

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

IT 4043 Data Structures and Algorithms

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

STACKS AND QUEUES. Problem Solving with Computers-II

Ceng 111 Fall 2015 Week 12b

CSC148H Week 3. Sadia Sharmin. May 24, /20

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Data Structures I: Linked Lists

ABSTRACT DATA TYPES (ADTS) COMP1927 Computing 2 16x1 Sedgewick Chapter 4

infix expressions (review)

(Refer Slide Time: 01.26)

introduction to Programming in C Department of Computer Science and Engineering Lecture No. #40 Recursion Linear Recursion

INSTITUTE OF AERONAUTICAL ENGINEERING

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

ECE250: Algorithms and Data Structures Midterm Review

CSE 373 Autumn 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

CS 222: Linked Lists, Queues, Stacks

CS 206 Introduction to Computer Science II

Priority Queue ADT. Revised based on textbook author s notes.

Data structure and algorithm in Python

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

CS-141 Exam 2 Review November 10, 2017 Presented by the RIT Computer Science Community

ECE Spring 2018 Problem Set #0 Due: 1/30/18

8/19/2014. Most algorithms transform input objects into output objects The running time of an algorithm typically grows with input size

! Mon, May 5, 2:00PM to 4:30PM. ! Closed book, closed notes, clean desk. ! Comprehensive (covers entire course) ! 30% of your final grade

Search Lesson Outline

12 Abstract Data Types

Stacks and Queues. Gregory D. Weber. CSCI C243 Data Structures

How to Win Coding Competitions: Secrets of Champions. Week 2: Computational complexity. Linear data structures Lecture 5: Stack. Queue.

Name CPTR246 Spring '17 (100 total points) Exam 3

Fun facts about recursion

UNIT 6A Organizing Data: Lists. Last Two Weeks

CS171 Midterm Exam. October 29, Name:

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

CS 216 Exam 1 Fall SOLUTION

CSE 2123 Recursion. Jeremy Morris

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

1 Writing recursive functions

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

Course Review. Cpt S 223 Fall 2009

CS1 Lecture 5 Jan. 26, 2018

Algorithm Design and Analysis

INTRODUCTION. Objective: - Algorithms - Techniques - Analysis. Algorithms:

CSCI-1200 Data Structures Spring 2018 Lecture 14 Associative Containers (Maps), Part 1 (and Problem Solving Too)

CS1 Lecture 22 Mar. 6, 2019

What is an algorithm?

CMSC 201 Spring 2016 Lab 04 For Loops

Cpt S 223 Fall Cpt S 223. School of EECS, WSU

Admin. How's the project coming? After these slides, read chapter 13 in your book. Quizzes will return

UNIT 5B Binary Search

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Basic Data Structures (Version 7) Name:

Molecular Statistics Exercise 1. As was shown to you this morning, the interactive python shell can add, subtract, multiply and divide numbers.

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

ASYMPTOTIC COMPLEXITY

Queues and Unit Testing

Stacks and Queues

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 10: Asymptotic Complexity and

CSCI 102L - Data Structures Midterm Exam #2 Spring 2011

Shell / Python Tutorial. CS279 Autumn 2017 Rishi Bedi

Week 6. Data structures

Module 1: Asymptotic Time Complexity and Intro to Abstract Data Types

Department of Computer Science and Engineering. CSE 2011: Fundamentals of Data Structures Winter 2009, Section Z

MEMOIZATION, RECURSIVE DATA, AND SETS

Algorithms in Systems Engineering IE172. Midterm Review. Dr. Ted Ralphs

PROBLEM SOLVING 11. July 24, 2012

Summer Final Exam Review Session August 5, 2009

CSI33 Data Structures

Part III Appendices 165

Intro. Scheme Basics. scm> 5 5. scm>

PA3 Design Specification

CSE 373 Winter 2009: Midterm #1 (closed book, closed notes, NO calculators allowed)

Python Programming Exercises 1

Problem One: Loops and ASCII Graphics

CS 211 Programming Practicum Spring 2017

COMS 6100 Class Notes 3

Algorithms and Programming

DATA STRUCTURES AND ALGORITHMS

Queues. Queue ADT. Queue Specification. An ordered sequence A queue supports operations for inserting (enqueuing) and removing (dequeuing) elements.

CS1 Lecture 5 Jan. 25, 2019

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

! Determine if a number is odd or even. ! Determine if a number/character is in a range. ! Assign a category based on ranges (wind speed)

1/30/2018. Conventions Provide Implicit Information. ECE 220: Computer Systems & Programming. Arithmetic with Trees is Unambiguous

15110 Principles of Computing, Carnegie Mellon University - CORTINA. Binary Search. Required: List L of n unique elements.

3137 Data Structures and Algorithms in C++

CMPSCI 187: Programming With Data Structures. Lecture 5: Analysis of Algorithms Overview 16 September 2011

Chapter 5 Data Structures Algorithm Theory WS 2016/17 Fabian Kuhn

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

Chapter 8 : Computer Science. Datastructures: Class XII ( As per CBSE Board) lists, stacks, queues. Visit : python.mykvs.in for regular updates

Fundamentals of Programming (Python) Getting Started with Programming

Getting Started with Python

Hash Tables. CS 311 Data Structures and Algorithms Lecture Slides. Wednesday, April 22, Glenn G. Chappell

Transcription:

Munster Programming Training - Cycle 2 Lecture 3 - Binary Search & Queues & Stacks Bastien Pietropaoli bastien.pietropaoli@insight-centre.org

Time complexity Previously On MPT Cycle 2 Recursions Exercise - Count words Exercise - Base conversion Questions? 2

Time complexity Big-O notation Growth rate Asymptotic (for big numbers) Upper bound (worst case) Best: O(1) Good: O(n) Better: O(log n) Horrible: O(n!) Credit: Wikipedia 3

Recursion - General structure In a recursion, you dig until you reach a trivial case. def recursion(data): Doc. if trivial_case1: return value1 elif trivial_case2: return value2... else: return recursion(reduced_data) 4

Count words Write a function that counts all the words in a string. def count_words(s): Hint 1: Use the doc! https://docs.python.org/3/libr ary/stdtypes.html#string-metho ds import string as str from collections import Counter def count_words(s): """Count all the words present in the string.""" s = s.lower() for c in str.punctuation + str.whitespace: s = s.replace(c, " ") return Counter(s.split()) Hint 2: Get rid of the noise. 5

Base conversion (1/2) Write a function that can convert a number from base 10 to any other base. def base_conversion(n, base): Hint 1: Divide. def base_conversion_recursion(n, base, result=""): """Converts number n to the requested base.""" if n == 0: # Trivial case in all bases return "0" result = str(n % base) + result if n // base!= 0: return base_conversion_recursion(n // base, base, result) else: return result Hint 2: Keep the reminder. 6

Base conversion (2/2) Write a function that can convert a number from base 10 to any other base. def base_conversion(n, base): Hint 1: Divide. Hint 2: Keep the reminder. def base_conversion(n, base): """Converts number n to the requested base.""" if n == 0: # Trivial case in all bases return "0" # Same principle as the log2 function: digits = [] while n: # True while n > 0 digits.append(n % base) n //= base # Same principle as the list reversion: result = "" for d in digits: result = str(d) + result return result 7

Previously - Questions? Any question on last week s material? 8

Searching Binary Search Search efficiently! Binary search Interpolation search Complexity 9

Searching - Refresher Linear searching def find_something(iter): """Doc""" current_result = default_value for value in iter: if condition: current_result = new_r return current_result def find_something_spec(iter, e): Doc for value in iter: if condition: return result return default_value 10

Binary search What if my entries are sorted already? Searching for 7 Binary search: 1) Start in the middle 2) Go one way or the other? 3) Eliminate half of the values. 4) Repeat Credit: Wikipedia 11

Interpolation search What if my entries are more or less evenly distributed? Searching for 7 Interpolation search: 1) Interpolate (previously the middle) 2) Eliminate one side. 3) Repeat K = (data - min) / (max - min) interpolation = K * nb_elements 12

Complexity Binary search: O(log n) Interpolation search: O(log log n) BUT Interpolation search might actually go to O(n) if the entries are not evenly sorted! Credit: liquidfolders.net 13

Practice Exercises Time to get your hands dirty! Binary search The Price is Right 14

Binary search Implement a binary search for a large list of integers. 1) Test with range(1000) 2) Test with range(1000000) 3) Test with range(0, 1000000, 2) 4) What if the searched value isn t in the list? 5) Measure the number of operations needed. 6) Measure time of execution. 7) Implement interpolation search. Too big for one slide: go to solution1.py in Exercises/ 15

The price is right Make the computer guess your number! 1) Give it a range of values (min, max) 2) Give it the following answers via input(): a) < if your value is lower. b) > if your value is greater. c) = if it found it. Too big for one slide: go to solution2.py in Exercises/ 16

Stacks & Queues Simple data structures. Queues Stacks deque 17

Queues (1/3) First in, first out (FIFO) You can: Enqueue data Dequeue data Get the size of the queue Credit: Wikipedia 18

Queues (2/3) from queue import Queue Using the queue module API: put() to enqueue things get() to dequeue things qsize() to get its size # Create an empty queue: q = Queue() # Enqueue stuff: for i in range(100): q.put(i) q.qsize() # Should be 100 # Dequeue stuff in the same order: for i in range(100): print(q.get()) q.qsize() # Should be 0 Credit: Wikipedia 19

Queues (3/3) # Create an empty queue: q = [] Using simple lists API: append() to enqueue things pop(0) to dequeue things len() to get its size # Enqueue stuff: for i in range(100): q.append(i) len(q) # Should be 100 # Dequeue stuff in the same order: for i in range(100): print(q.pop(0)) len(q) # Should be 0 Credit: Wikipedia 20

Stacks (1/2) Last in, first out (LIFO) You can: Stack data (push) Unstack data (pop) Get the size of the stack 21

Stacks (2/2) # Create an empty stack: s = [] No direct implementation in the STL You can use lists again API: append() to stack things pop() to unstack things len() to get its size # Stack stuff: for i in range(100): s.append(i) len(s) # Should be 100 # Unstack in reverse order: for i in range(100): print(s.pop()) len(s) # Should be 0 22

deque to create queues from collections import deque You can use a double-ended queue or deque (pronounced deck ) API: append() to enqueue popleft() to dequeue len() to get its size # Create an empty queue: q = deque() # Enqueue stuff: for i in range(100): q.append(i) len(q) # Should be 100 # Dequeue stuff in the same order: for i in range(100): print(q.popleft()) len(q) # Should be 0 23

deque to create stacks from collections import deque You can use a double-ended queue or deque (pronounced deck ) API: append() to enqueue popleft() to dequeue len() to get its size # Create an empty queue: s = deque() # Enqueue stuff: for i in range(100): s.append(i) len(s) # Should be 100 # Dequeue stuff in the same order: for i in range(100): print(s.pop()) len(s) # Should be 0 24

Practice Exercises Time to get your hands dirty! Postfix Palindromes 25

Postfix Thanks, Milan Postfix expressions are arithmetical expressions where the operators come after anything they operate on. Write a function that evaluates postfix expressions containing only +, -, *, and / def postfix(str): Examples: 3 5 * ---> 15 6 4 * 2 + ---> 26 ops = ['-', '+', '*', '/'] def postfix(s): l = [int(e) if e not in ops else e for e in s.split()] # Parse the expression my_stack = [] for e in l: if e not in ops: my_stack.append(e) else: e1, e2 = my_stack.pop(), my_stack.pop() my_stack.append( eval("e1" + e + "e2") ) return my_stack.pop() if name == " main ": s = input("enter your postfix expression: ") print(postfix(s)) 26

Palindromes Palindromes are sequences of characters which are read the same backward as forward. Write a function return True if the provided word is a palindrome, False otherwise. def palindrome(str): def palindrome_one_liner(s): return s == s[::-1] def palindrome_stack(s): stack = list(s) for letter in s: if letter!= stack.pop(): return False return True Examples: racecar madam Tacocat whatever ---> True ---> True ---> True ---> False if name == " main ": s = input("enter something to check: ") print(palindrome_one_liner(s)) print(palindrome_stack(s)) 27

Discussion & conclusion What we ve seen today: Quick refreshers on complexity and recursions An efficient search algorithm Simple data structures: stacks & queues Where you are in algorithmic: You re getting there slowly. Where you are in Python: It s not really improving, is it? Discussion: Do you think you re getting better? 28

Next week: Consolidation Only practice next week! No, I m not too lazy to prepare another lecture, shut up! It actually helps memorisation to work again on the concepts we ve seen before. 29

Write functions Bonus material Code and test efficiently. Write tests Use a Unix terminal Fast input 30

Write functions Writing functions enable dividing the task into simpler tasks Can I divide my tasks? How much should I divide? Am I solving multiple problems at once? Easier to test: each function can be tested separately. A tiny bit slower. 31

Write tests Use test cases (unit tests): Use assert to test your code. Are there cases for which I know the answer? Test non-trivial cases: change boundaries, edge cases, try to break your code to find those cases! Use a lot of cases. Try to go through every line of code 32

Use a bash terminal Open a terminal on Linux: Ctrl + Alt + T Kill something: Ctrl + C Get out of something: Ctrl + D Basic bash commands: mkdir my_dir: creates directory my_dir cd my_dir: moves to my_dir pwd: tells you where you are gedit my_file &: edit my_file using gedit. python: gives you the Python shell (like in Idle) python my_script.py: executes your Python script mv my_file new_name: rename my_file to new_name rm my_file: removes my_file 33

Fast input My input file: 4 5 * 2 + 5 / 18 12 + * My Python script: def whatever(l): l.sort() return l My terminal: user:~$ python my_script.py < my_input_file if name == main : l = [e for e in input().split()] # List comprehension # input() reads one line # split() splits by spaces print(whatever(l)) 34

Programming Trivia Rubber Duck Debugging https://en.wikipedia.org/wiki/rubber_duck_deb ugging 35