Algorithmic Thinking: Computing with Lists

Similar documents
Algorithmic Thinking: Computing with Lists

Algorithmic Thinking: Loops and Conditionals

15110 Principles of Compu5ng, Carnegie Mellon University - CORTINA. Finding the maximum. Required: a non-empty list of integers.

15-110: Principles of Computing, Spring Problem Set 3 (PS3) Due: Friday, February 9 by 2:30PM on Gradescope

CIS192: Python Programming Data Types & Comprehensions Harry Smith University of Pennsylvania September 6, 2017 Harry Smith (University of Pennsylvani

CMPT 120 Lists and Strings. Summer 2012 Instructor: Hassan Khosravi

Lists How lists are like strings

CIS192 Python Programming

MUTABLE LISTS AND DICTIONARIES 4

Python I. Some material adapted from Upenn cmpe391 slides and other sources

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

Dynamic Memory Allocation

Programming in Python

Why efficiency of algorithm matters? CSC148 Intro. to Computer Science. Comparison of growth of functions. Why efficiency of algorithm matters?

Decisions, Decisions. Testing, testing C H A P T E R 7

Python for loops. Girls Programming Network School of Information Technologies University of Sydney. Mini-lecture 7

Python Class-Lesson1 Instructor: Yao

CS S-02 Python 1. Most python references use examples involving spam, parrots (deceased), silly walks, and the like

COMP1730/COMP6730 Programming for Scientists. Sequence types, part 2

Problem Solving for Intro to Computer Science

CSCE 110 Programming I Basics of Python: Lists, Tuples, and Functions

COMPSCI 230 Discrete Math Prime Numbers January 24, / 15

Extended Introduction to Computer Science CS1001.py

Computer Sciences 368 Scripting for CHTC Day 3: Collections Suggested reading: Learning Python

Announcements. Project 2 due next Monday. Next Tuesday is review session; Midterm 1 on Wed., EE 129, 8:00 9:30pm

Lists in Python CS 8: Introduction to Computer Science, Winter 2018 Lecture #10

The Practice of Computing Using PYTHON

PIC 16, Fall Variables, Dynamically-typed, Mutable, Immutable, Functions. Michael Andrews. October 1, Department of Mathematics UCLA

AI Programming CS S-02 Python

CS Summer 2013

CONTROL AND ENVIRONMENTS 1

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Working with Lists 4

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

Abstract Data Types. CS 234, Fall Types, Data Types Abstraction Abstract Data Types Preconditions, Postconditions ADT Examples

UCT Algorithm Circle: Number Theory

Worksheet 6: Basic Methods Methods The Format Method Formatting Floats Formatting Different Types Formatting Keywords

ENGR/CS 101 CS Session Lecture 12

Extended Introduction to Computer Science CS1001.py Lecture 7: Basic Algorithms: Sorting, Merge; Time Complexity and the O( ) notation

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Important note: In all cases code should be clearly-written and should include a brief explanation in English explaining the design of your code.

Sharing, mutability, and immutability. Ruth Anderson UW CSE 160 Spring 2018

Homework notes. Homework 2 grades posted on canvas. Homework 3 due tomorrow. Homework 4 posted on canvas. Due Tuesday, Oct. 3

Types, lists & functions

University of Texas at Arlington, TX, USA

Programming Languages. Streams Wrapup, Memoization, Type Systems, and Some Monty Python

Intro. Speed V Growth

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

CS 115 Lecture 8. Selection: the if statement. Neil Moore

Control and Environments Fall 2017 Discussion 1: August 30, Control. If statements. Boolean Operators

MITOCW watch?v=rvrkt-jxvko

CS1 Lecture 12 Feb. 11, 2019

Data Structures. Lists, Tuples, Sets, Dictionaries

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

Basic Python Revision Notes With help from Nitish Mittal

Iteration, Lists, and Algorithm Design 9/14/2017

CSC 148 Lecture 3. Dynamic Typing, Scoping, and Namespaces. Recursion

Introduction to Python

CSc 120 Introduction to Computer Programing II Adapted from slides by Dr. Saumya Debray

Lecture Writing Programs. Richard E Sarkis CSC 161: The Art of Programming

F21SC Industrial Programming: Functional Programming in Python

CS 102 Lab 3 Fall 2012

CSc 120. Introduc/on to Computer Programing II. Adapted from slides by Dr. Saumya Debray. 01- a: Python review

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

CS1 Lecture 11 Feb. 9, 2018

Streams. CS21b: Structure and Interpretation of Computer Programs Spring Term, 2004

COMP-520 GoLite Tutorial

Collections. Lists, Tuples, Sets, Dictionaries

Announcements. Project 5 is on the street. Second part is essay questions for CoS teaming requirements.

Programming Languages

PREPARING FOR PRELIM 1

SCHEME AND CALCULATOR 5b

Prime Factorization. Jane Alam Jan. 1 P a g e Document prepared by Jane Alam Jan

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

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

Advanced Python. Executive Summary, Session 1

Lists, loops and decisions

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Control and Environments Fall 2017 Discussion 1: August 30, 2017 Solutions. 1 Control. If statements. Boolean Operators

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

Implementing an Algorithm for Boomerang Fraction Sequences in Python

9. The Disorganized Handyman

CMSC201 Computer Science I for Majors

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

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

Lecture 11: Iteration and For-Loops

EE 355 Unit 17. Python. Mark Redekopp

Programming for Engineers in Python

CMPT 125: Lecture 3 Data and Expressions

Data structure and algorithm in Python

Programming for Engineers in Python. Recitation 1

List Processing Patterns and List Comprehension

Add Subtract Multiply Divide

CS 2316 Exam 1 Spring 2014

Module 08: Searching and Sorting Algorithms

CMSC 201 Computer Science I for Majors

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

Accelerating Information Technology Innovation

Introduction to Python (All the Basic Stuff)

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

Transcription:

Algorithmic Thinking: Computing with Lists

So Far in Python Data types: int, float, Boolean, string Assignments, function definitions Control structures: For loops, while loops, conditionals

Last Lecture More algorithmic thinking Example: Finding the maximum in a list Composite (structured) data type: lists Storing and accessing data in lists Modifying lists Operations on lists Iterating over lists

Any confusion? Print vs Return ------------- def?????? (a, b): result = a + b print (result) ------------- def??????(a, b): result = a + b return (result) Between Data Types ------ "3 + 5" vs 3 + 5 ------------- 6 * 5 vs 6 * 5.0

Representing Lists in Python We will use a list to represent a collection of data values. scores = [78, 93, 80, 68, 100, 94, 85] colors = [ red, green, blue ] mixed = [ purple, 100, 90.5] A list is an ordered sequence of values and may contain values of any data type. In Python lists may be heterogeneous (may contain items of different data types). 5

Some List Operations Indexing (think of subscripts in a sequence) Length (number of items contained in the list) Slicing Membership check Concatenation 6

Some List Operations >>> names = [ "Al", "Jane", "Jill", "Mark" ] >>> len(names) 4 >>> Al in names Error... name 'Al' is not defined >>> "Al" in names True >>> names + names ['Al','Jane','Jill','Mark','Al','Jane','Jill','Mark'] 7

Accessing List Elements names "Al" "Jane" "Jill" "Mark" 0 1 2 3 list elements indices >>> names[0] 'Al' >>> names[3] 'Mark' >>> names[len(names)-1] 'Mark' >>> names[4] Traceback (most recent call last): File "<pyshell#8>", line 1, in <module> names[4] IndexError: list index out of range

Slicing Lists names "Al" "Jane" "Jill" "Mark" list elements 0 1 2 3 indices >>> names[1:3] ['Jane', 'Jill'] slice >>> names[0:4:2] [ Al', 'Jill'] Start Step End incremental slice

Slicing Lists names "Al" "Jane" "Jill" "Mark" list elements 0 1 2 3 indices names, names[0:4], names[0,4,1] ['Al', 'Jane, 'Jill', 'Mark'] >>> names[1:3] ['Jane', 'Jill'] >>> names[1:4] ['Jane', 'Jill', 'Mark'] >>> names[0:4:2] [ Al', 'Jill'] >>> names[:3] ['Al', 'Jane', 'Jill'] >>> names[:2] ['Al', 'Jane'] >>> names[2:] ['Jill', 'Mark']

source: docs.python.org 11

Modifying Lists >>> names = ['Al', 'Jane', 'Jill', 'Mark'] >>> names[1] = "Kate" >>> names ['Al', 'Kate', 'Jill', 'Mark'] >>> a = [1, 2, 3] >>> a[0:0] = [-2, -1, 0] >>> a [-2, -1, 0, 1, 2, 3] >>> names[1:3] = [ "Me", "You" ] >>> names ['Al', 'Me', 'You', 'Mark'] >>> a = [1, 2, 3] >>> a[0:1] = [-2, -1, 0] >>> a [-2, -1, 0, 2, 3] >>> names[1:3] = [ "AA", "BB", "CC", "DD" ] ['Al', 'AA', 'BB', 'CC', 'DD', 'Mark'] The list grew in length, we could make it shrink as well. 12

source: docs.python.org 13

Aliasing west east >>> west = ["CA", "OR"] >>> east = ["NY", "MA"] >>> all = [west, east] >>> all [["CA", "OR"],["NY", "MA"]] "CA" all "OR" "NY" "MA" 2 paths to the list containing state names in the West Coast. One through the variable west, >>> west The other through the variable all. >>> all[0] This is called aliasing.

Mutability Requires Caution west "CA" "OR" "WA" "NY" "MA" east all >>> west = ["CA", "OR"] >>> east = ["NY", "MA"] All variables that are bound to the >>> all = [west, east] modified object change in value. >>> west.append("wa") >>> all [['CA', 'OR', 'WA'], ['NY', 'MA']]

Creating Copies west "CA" "OR" east "NY" "MA" all west east "CA" >>> west = ["CA", "OR"] >>> east = ["NY", "MA"] >>> all2 = [west[:], east[:]] >>> all2 [["CA", "OR"], ["NY", "MA"]] No matter how I modify west, all1 will not see it. all2 "OR" [, ] [, ] "NY" "MA" Creates a shallow copy. If list items were mutable objects, as opposed to strings as we have here, we would have needed something more. Don t worry about it now.

What Happens in the Memory? >>> west = ["CA", "OR"] >>> east = ["NY", "MA"] >>> all = [west, east] >>> all2 = [west[:], east[:]] this is more like >>> all2 = [["CA", "OR"], ["NY", "MA"]] >>> print(id(all), all) 48231728 [["CA", "OR"], ["NY", "MA"]] >>> print(id(all2), all2) 48221880 [["CA", "OR"], ["NY", "MA"]]

Iterating over Lists def print_colors(colors): for index in range(0, len(colors)): print(colors[index]) >>> print_colors(["red", "blue", "green"]) red blue green 18

Alternative Version def print_colors(colors): for c in colors: print(c) Compare with previous version def print_colors(colors): for index in range( 0, len(colors) ): print( colors[index] ) Python binds c to the first item in colors, then execute the statement in the loop body, binds c to the next item in the list colors etc. 19

Finding the max using Python def findmax(lst): max_so_far = lst[0] # set 1st item as the maximum found for i in range(1,len(lst)): if lst[i] > max_so_far: max_so_far = lst[i] return max_so_far # Check all following items # if you find a bigger value # update the maximum # After checking all values # return the maximum found 20

Alternative Version def findmax(lst): max_so_far = lst[0] for item in lst: if item > max_so_far: max_so_far = item return max_so_far # initialize the maximum For each item in the list... # if it is bigger then maximum # keep it as the new maximum # return the maximum after checkin all 21

Summary The list data type (ordered and dynamic collections of data) Creating lists Accessing elements Modifying lists Iterating over lists

A 2000 year old algorithm (procedure) for generating a table of prime numbers. 2, 3, 5, 7, 11, 13, 17, 23, 29, 31, 23

What Is a Sieve or Sifter? Separates stuff you want from stuff you don t: We want to separate prime numbers. 24

Prime Numbers An integer is prime if it is not divisible by any smaller integers except 1. 10 is not prime because 10 = 2 5 11 is prime 12 is not prime because 12 = 2 6 = 2 2 3 13 is prime 15 is not prime because 15 = 3 5 25

Testing Divisibility in Python x is divisible by y if the remainder is 0 when we divide x by y 15 is divisible by 3 and 5, but not by 2: >>> 15 % 3 0 >>> 15 % 5 0 >>> 15 % 2 1 26

The Sieve of Eratosthenes Start with a table of integers from 2 to N. Cross out all the entries that are divisible by the primes known so far. The first value remaining is the next prime. 27

Finding Primes Between 2 and 50 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 2 is the first prime 28

Finding Primes Between 2 and 50 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 Filter out everything divisible by 2. Now we see that 3 is the next prime. 29

Finding Primes Between 2 and 50 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 Filter out everything divisible by 3. Now we see that 5 is the next prime. 30

Finding Primes Between 2 and 50 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 Filter out everything divisible by 5. Now we see that 7 is the next prime. 31

Finding Primes Between 2 and 50 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 Filter out everything divisible by 7. Now we see that 11 is the next prime. 32

Finding Primes Between 2 and 50 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 Since 11 x 11 > 50, all remaining numbers must be primes. Why? 33

An Algorithm for Sieve of Eratosthenes Input: A number n: 1. Create a list numlist with every integer from 2 to n, in order. (Assume n > 1.) 2. Create an empty list primes. 3. For each element in numlist a. If element is not marked, copy it to the end of primes. b. Mark every number that is a multiple of the most recently discovered prime number. Output: The list of all prime numbers less than or equal to n 34

Automating the Sieve numlist primes 2 3 4 5 6 7 8 9 10 11 12 13 Use two lists: candidates, and confirmed primes. 35

Steps 1 and 2 numlist primes 2 3 4 5 6 7 8 9 10 11 12 13 36

Step 3a numlist primes 2 3 4 5 6 7 8 9 10 11 12 13 2 Append the current number in numlist to the end of primes. 37

Step 3b numlist primes 2 3 4 5 6 7 8 9 10 11 12 13 2 Cross out all the multiples of the last number in primes. 38

Iterations numlist 2 3 4 5 6 7 8 9 10 11 12 13 2 3 primes Append the current number in numlist to the end of primes. 39

Iterations numlist 2 3 4 5 6 7 8 9 10 11 12 13 2 3 primes Cross out all the multiples of the last number in primes. 40

Iterations numlist 2 3 4 5 6 7 8 9 10 11 12 13 primes 2 3 5 Append the current number in numlist to the end of primes. 41

Iterations numlist 2 3 4 5 6 7 8 9 10 11 12 13 primes 2 3 5 Cross out all the multiples of the last number in primes. 42

An Algorithm for Sieve of Eratosthenes Input: A number n: 1. Create a list numlist with every integer from 2 to n, in order. (Assume n > 1.) 2. Create an empty list primes. 3. For each element in numlist a. If element is not marked, copy it to the end of primes. b. Mark every number that is a multiple of the most recently discovered prime number. Output: The list of all prime numbers less than or equal to n 43

Implementation Decisions How to implement numlist and primes? For numlist we will use a list in which crossed out elements are marked with the special value None. For example, [None, 3, None, 5, None, 7, None] Use a helper function for step 3.b. We will call it sift.

Relational Operators If we want to compare two integers to determine their relationship, we can use these relational operators: < less than <= less than or equal to > greater than >= greater than or equal to == equal to!= not equal to We can also write compound expressions using the Boolean operators and and or. x >= 1 and x <= 1 45

Sifting: Removing Multiples of a Number def sift(lst, k): # marks multiples of k with None i = 0 while i < len(lst): if (lst[i]!=none) and lst[i]%k == 0: lst[i] = None i = i + 1 return lst Filters out the multiples of the number k from list by marking them with the special value None (greyed out ones).

Sifting: Removing Multiples of a Number (Alternative version) def sift2(lst,k): i = 0 while i < len(lst): if lst[i] % k == 0: lst.remove(lst[i]) else: i = i + 1 return lst Filters out the multiples of the number k from list by modifying the list. Be careful in handling indices.

A Working Sieve def sieve(n): numlist = list(range(2,n+1)) primes = [] for i in range(0, len(numlist)): if numlist[i]!= None: primes.append(numlist[i]) sift(numlist, numlist[i]) return primes We could have used primes[len(primes)-1] instead. Helper function that we defined before

Observation for a Better Sieve We stopped at 11 because all the remaining entries must be prime since 11 11 > 50. 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 49

A Better Sieve def sieve(n): numlist = list(range(2, n + 1)) primes = [] i = 1 while i <= math.sqrt(n): if numlist[i]!= None: primes.append(numlist[i]) sift(numlist, numlist[i]) i = i + 1 return primes + numlist

Algorithm-Inspired Sculpture The Sieve of Eratosthenes, 1999 sculpture by Mark di Suvero. Displayed at 51 Stanford University.