About the Final. Saturday, 7-10pm in Science Center 101. Closed book, closed notes. Not on the final: graphics, file I/O, vim, unix

Similar documents
Announcements. Lab 11 is due tomorrow. Quiz 6 is on Monday. Ninja session tonight, 7-9pm. The final is in two weeks!

Exam 1 Format, Concepts, What you should be able to do, and Sample Problems

Module 08: Searching and Sorting Algorithms

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Text Input and Conditionals

GE PROBLEM SOVING AND PYTHON PROGRAMMING. Question Bank UNIT 1 - ALGORITHMIC PROBLEM SOLVING

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Module 04: Lists. Topics: Lists and their methods Mutating lists Abstract list functions Readings: ThinkP 8, 10. CS116 Fall : Lists

About this exam review

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Collections. Lists, Tuples, Sets, Dictionaries

Introduction to Programming Using Java (98-388)

PREPARING FOR PRELIM 1

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

Data Handing in Python

CPSC 217 Midterm (Python 3 version)

Linked Structures Chapter John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

CS Summer 2013

TUPLES AND RECURSIVE LISTS 5

egrapher Language Reference Manual

Introduction to Concepts in Functional Programming. CS16: Introduction to Data Structures & Algorithms Spring 2017

CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees. CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 1

CS261 Data Structures. Ordered Array Dynamic Array Implementation

(Refer Slide Time: 01.26)

COMP-520 GoLite Tutorial

1 Strings (Review) CS151: Problem Solving and Programming

Lab 6 Sorting. Sup Biotech 3 Python. Pierre Parutto

Data Structures. Lists, Tuples, Sets, Dictionaries

n 1 i = n + i = n + f(n 1)

CS 1301 Final Exam Review Guide

CS 1110 Final, December 16th, 2013

Here n is a variable name. The value of that variable is 176.

CS Lecture 18: Card Tricks. Announcements. Slides by D. Gries, L. Lee, S. Marschner, W. White

Linked Lists. Linked List vs Array vs Python List Singly Linked List Bag ADT Revisited Building Linked Lists

CS 1110 Final, December 8th, Question Points Score Total: 100

VALLIAMMAI ENGINEERING COLLEGE

Introduction to Python

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

1 Truth. 2 Conditional Statements. Expressions That Can Evaluate to Boolean Values. Williams College Lecture 4 Brent Heeringa, Bill Jannen

CMSC 341 Lecture 10 Binary Search Trees

Lab 9: More Sorting Algorithms 12:00 PM, Mar 21, 2018

! 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)

Review Sheet for Midterm #1 COMPSCI 119 Professor William T. Verts

Lecture 8: Iterators and More Mutation

CIS192: Python Programming

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes

Part I. Wei Tianwen. A Brief Introduction to Python. Part I. Wei Tianwen. Basics. Object Oriented Programming

Flow Control: Branches and loops

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

CS 331 Summer 2017 Final Exam

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

15-110: Principles of Computing, Spring 2018

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

Beyond Blocks: Python Session #1

CS 1110 Final, December 16th, 2013

Data structures and libraries

Computing with Numbers

CS 177 Week 15 Recitation Slides. Review

ENGR 102 Engineering Lab I - Computation

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

Working with recursion. From definition to template. Readings: HtDP, sections 11, 12, 13 (Intermezzo 2).

RECURSION AND LINKED LISTS 3

CS 234. Module 3. September 19, CS 234 Module 3 Data structures, ADTs with no order of any kind, Python review 1 / 33

Working with recursion

x = 3 * y + 1; // x becomes 3 * y + 1 a = b = 0; // multiple assignment: a and b both get the value 0

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

Module 8: Local and functional abstraction

RLISTS AND THE ENVIRONMENT MODEL 9

Lecture 6 Sorting and Searching

Hacettepe University Computer Engineering Department. Programming in. BBM103 Introduction to Programming Lab 1 Week 4. Fall 2018

cs1114 REVIEW of details test closed laptop period

PREPARING FOR THE FINAL EXAM

CS 211 Programming Practicum Fall 2018

Overview of List Syntax

CS171 Midterm Exam. October 29, Name:

Algorithm Design and Recursion. Search and Sort Algorithms

Algorithms in Systems Engineering ISE 172. Lecture 5. Dr. Ted Ralphs

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions. 1 Calculator. calc> (+ 2 2) 4

Fundamentals of Programming (Python) Getting Started with Programming

[CHAPTER] 1 INTRODUCTION 1

CMSC201 Computer Science I for Majors

Getting Started with Python

Outline: Search and Recursion (Ch13)

CS1 Lecture 12 Feb. 11, 2019

NATIONAL UNIVERSITY OF SINGAPORE

Part III Appendices 165

CS 211 Programming Practicum Spring 2018

Ch. 12: Operator Overloading

ENGR 101 Engineering Design Workshop

CS1 Lecture 11 Feb. 9, 2018

Lists, loops and decisions

Transcription:

CS 21 Final Review

About the Final Saturday, 7-10pm in Science Center 101 Closed book, closed notes Not on the final: graphics, file I/O, vim, unix

Expect Questions That Ask You To: Evaluate Python expressions and identify their types Trace a program, drawing stack diagram and showing output Write a complete Python program with multiple functions Write recursive functions, draw the stack for a recursive function

Expect Questions That Ask You To: Trace the execution of searching or sorting algorithms, identify their big O run times Write methods for a class, look at a class definition and understand how to use it. Write/understand methods for LinkedList class, use LinkedList class, draw structure of a linked list, identify big O run times for operations on Python lists and linked lists. Find and fix bugs in a function

Types int float bool string (also a sequence) list (also a sequence) None objects belonging to various classes

Arithmetic Operators Addition / Concatenation: + Multiplication / Replication: * Subtraction: - Division: / - Integer division Remainder: % Promotion: an int gets promoted when combined with a float

Comparison Operators Equality: == Inequality:!= Greater than: > Greater than or equal to: >= Less than: < Less than or equal to: <=

Logical Operators and: True if both operands are True, False otherwise or: True if either operand is True, False otherwise not: True if lone operand is False, False if it s True

Sequence Operators Indexing: seq[index] Slicing: seq[start:stop], seq[start:stop:step] in operator: tests for membership, different from for loop s in

Variables and Assignment a = 7 a = a + 1 a += 1 Can t refer to a variable before it s initialized - For loop variables are initialized implicitly - So are function parameters

Practice: identify value and type for each expression on the next slide.

Assume: x = 1 s = swarthmore L = [[7, 29, 0], [-4, 13], [9, 9, 5]] 1 + 2.0 1 / 2 1.0 / 2 21 % 10 s[2:4] len(l) len(l[1]) L[1][0] + L[2][2]

Conditionals / Branching if if / else if / elif / elif / / elif / else Nested conditionals

Loops for loops - for i in range(len(seq)): - for i in range(n): - for ch in str: - for item in lst: while loops - while [boolean expression]: - while True:

Loops Nested loops Accumulators - Initialize accumulator variable once before loop - Update accumulator variable inside loop - use final value after loop ends

Built-in functions print(message) - string formatting raw_input(prompt) conversion functions: int, float, str - validation with try/except or str.isdigit() type() useful for studying len(seq) range(start, stop, step)

Functions from a module/library from random import * random() randrange(start, stop, step) choice(seq)

Defining Functions Arguments in function call match parameters in function definition based on order Parameters and variables defined within a function are local to that function. - Can t be referred to outside of the function - This is what we depict with frames in a stack diagram Specifying a return value - Or None by default

Mutable vs. Immutable Strings, ints, floats, and bools are immutable. They can never be modified, only used to compute something new. Lists and objects are mutable. They can be mutated in the following ways: - index assignment: L[i] = L[i]+1 - calling a method that mutates, like L.pop(index) or L.append(item) - passing a list or object to a function that does mutation We use the stack to depict mutability vs. immutability. - When we mutate we don t cross out any arrows

Practice: draw stack at line indicated and show all output from program on next slide.

Recursive Functions A recursive function calls itself with a different, somehow smaller set of arguments. This is instead of using a loop. Recursive functions have a base case and a general case - The base case is a very small version of the problem, which can be solved right away. - The general case breaks the problem down into a smaller version of the same problem. Uses the solution to the smaller problem in solving the original problem.

Recursive Functions Sequences: Ints: - typical base case: len(l) == 0 or len(l) == 1 - typical general case: do something with L[0], combine it with result of recursive call on L[1:] - typical base case: n == 0 or n == 1 - typical general case: do something with n, combine it with result of recursive call on n-1 Take a leap of faith when writing the code for the general case. Assume the function will eventually do what you expect, and ask yourself what f(l[1:]) or f(n-1) will return.

Recursion practice Write a recursive function, length(l), that returns the length of a list L without using the len function. Write a main() function that calls length(l) on some list of length 3. Draw the stack diagram as it would look when the base case of the length(l) function is reached.

Searching Does the value x appear in the list L? - linear search - list doesn t need to be sorted - O(n) run time - binary search - list does need to be sorted - O(log n) run time - Repeatedly cuts in the half the range of indices where x might be found - Know how low, mid, and high variables update

Sorting O(n 2 ) sorts: - selection sort: select item that should go at position i and swap it directly into place. - bubble sort: swap consecutive items that are out of order so biggest items bubble up - insertion sort: insert next item so beginning of list stays sorted O(n log n) sort: - merge sort: repeatedly split in half until you have lists of size 1, which are already sorted, then merge back together

Practice algorithm traces Binary search for 3 in [-4, 1, 3, 7, 8, 10, 12, 17, 20]. Show how low, mid, and high update. Trace selection sort on [7, 2, 10, 5, 0] Trace bubble sort on [9, 10, 1, 2]

Using classes and methods We call constructors to create objects, then call methods on those objects to interact with them Strings and lists have methods even though we don t create them by calling a constructor - str.isdigit(), lst.append( x ), lst.pop(0)

Syntax for classes and methods

Defining classes A class definition is comprised of method definitions The init method is what actually gets executed when the constructor is called. It initializes the instance variables (self.whatever). The str method returns a string representation of the object, usually based on the values of its instance variables. This gets called when the object is printed or converted to a string. There are also getter methods, which return the value of an instance variable And setter methods, which update the values of one or more instance variables.

Defining classes Every method definition must have self as its first parameter. This gives the method the ability to access or change the instance variables. When we call a method, we don t list self as an argument. We don t know the order in which methods will be called (other than init being called first) so our methods must make sure that all instance variables are up-to-date before they return - i.e. the turn method for the bug class had to update self.bug and self.heading

For the class definition on the next slide: Create a LibraryBook with title Pride and Prejudice and author Jane Austen. Store it in a variable called book. Show what print(book) would display. Define a method checkout(newborrower) which updates whatever instance variables need to be updated after someone new checks the book out. Write code to register David checking the book out and returning it followed by Tia checking the book out.

Linked Lists Each item in the list corresponds to a node. A node contains the item and a pointer to the next node. We can start at the first node and follow links to all the other nodes. If a node s next point is None, we ve reached the end. Node class - self.data, self.next LinkedList class - self.head, self.tail, self.size

Linked Lists Be able to draw the structure, showing self.head, self.tail, and self.size Be able to identify various methods: insertions, removals, traversals Be able to compare run times of linked list operations and Python list operations.

Linked Lists Python list Linked list Insert at beginning O(n) O(1) Insert at end O(n)* O(1) Remove from beginning O(n) O(1) Remove from end O(1) O(n) Get item at index O(1) O(n)

Practice: identify in words what each of the following methods does.

More Linked List practice Write code that creates an empty linked list, stores it in a variable called LL, prepends A and B, appends C and D, and then prints out the linked list. Draw the structure of LL at this point. Write a method getatindex(index) which returns the item at the specified index in a linked list. Show how you would use it to get the item at index 2 in a linked list called LL

Top-down design Write the main() function first, creating a wish list of functions Implement those functions one at a time Common functions for our top-down designs: - A function that gets and validates user input - A function that helps with complicated computation - A function that prints output neatly

Practice writing a full program

Good luck!!