lambda forms map(), reduce(), filter(), eval(), and apply() estimating π with list comprehensions

Similar documents
List Comprehensions and Simulations

Outline. half adders adder circuits. the while loop the for loop. Euclid s algorithm approximating π

Lessons on Python Functions

Random Numbers Random Walk

Divide and Conquer. playing divide and conquer. searching in a sorted list membership in a sorted list

Outline. the try-except statement the try-finally statement. exceptions are classes raising exceptions defining exceptions

Test #2 October 8, 2015

Chapter 1. Fundamentals of Higher Order Programming

CS1 Lecture 5 Jan. 25, 2019

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

Outline. tallying the votes global and local variables call by value or call by reference. of variable length using keywords for optional arguments

Lecture 11: while loops CS1068+ Introductory Programming in Python. for loop revisited. while loop. Summary. Dr Kieran T. Herley

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

Lecture no

Welcome to MCS 275. Course Content Prerequisites & Expectations. Scripting in Python from OOP to LAMP example: Factorization in Primes

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

Using Sockets. client/server interaction telephone exchange. getting IP addresses of computers code for remote client and server

Beyond Blocks: Python Session #1

cs1114 REVIEW of details test closed laptop period

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

Variable and Data Type I

List comprehensions (and other shortcuts) UW CSE 160 Spring 2015

A Third Look At ML. Chapter Nine Modern Programming Languages, 2nd ed. 1

Introduction to Scientific Python, CME 193 Jan. 9, web.stanford.edu/~ermartin/teaching/cme193-winter15

06/11/2014. Subjects. CS Applied Robotics Lab Gerardo Carmona :: makeroboticsprojects.com June / ) Beginning with Python

Ruby: Introduction, Basics

Outline. Simple types in Python Collections Processing collections Strings Tips. 1 On Python language. 2 How to use Python. 3 Syntax of Python

egrapher Language Reference Manual

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

Defining Functions. turning expressions into functions. writing a function definition defining and using modules

Introduction to Functional Programming (Python) John R. Woodward

The Big Python Guide

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

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

CS1110 Lab 6 (Mar 17-18, 2015)

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

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

Python Evaluation Rules

CS1 Lecture 5 Jan. 26, 2018

Iterators & Generators

Outline. policies. with some potential answers... MCS 260 Lecture 19 Introduction to Computer Science Jan Verschelde, 24 February 2016

Programming. We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems.

Contents Computing with Formulas

Practical Haskell. An introduction to functional programming. July 21, Practical Haskell. Juan Pedro Villa-Isaza. Introduction.

An introduction to functional programming. July 23, 2010

CS 1301 Exam 3 Spring 2014

Outline. general information policies for the final exam

Fall Lecture 3 September 4. Stephen Brookes

a name refers to an object side effect of assigning composite objects

Ruby: Introduction, Basics

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

F21SC Industrial Programming: Functional Programming in Python

High Level Parallel Processing

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

Condition Controlled Loops. Introduction to Programming - Python

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

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

Downloaded from Chapter 2. Functions

6.001 SICP. Types. Types compound data. Types simple data. Types. Types procedures

Text Input and Conditionals

Structure and Interpretation of Computer Programs Spring 2014 Final (with corrections)

Lecture 19: Functions, Types and Data Structures in Haskell

Basic Scheme February 8, Compound expressions Rules of evaluation Creating procedures by capturing common patterns

Lecture 21: Functional Programming in Python. List Comprehensions

The float type and more on variables FEB 6 TH 2012

Basic Python 3 Programming (Theory & Practical)

MICROPROCESSOR SYSTEMS INTRODUCTION TO PYTHON

Midterm Exam 2B Answer key

Problem Solving for Intro to Computer Science

And Parallelism. Parallelism in Prolog. OR Parallelism

Ruby: Introduction, Basics

PYTHON. Values and Variables

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

CIS192: Python Programming

TUPLES AND RECURSIVE LISTS 5

ITERATORS AND STREAMS 9

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

Conditionals and Recursion. Python Part 4

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

CS111 Jeopardy: The Home Version

ENGR 101 Engineering Design Workshop

Lecture #12: Immutable and Mutable Data. Last modified: Mon Feb 22 16:33: CS61A: Lecture #12 1

TAIL RECURSION, SCOPE, AND PROJECT 4 11

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

Algebra 1 Review. Properties of Real Numbers. Algebraic Expressions

An introduction introduction to functional functional programming programming using usin Haskell

A general introduction to Functional Programming using Haskell

G Programming Languages - Fall 2012

DaMPL. Language Reference Manual. Henrique Grando

Taking the Derivative

Outline. measuring complexity: big-oh complexity classes counting flops: floating-point operations

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Extending Jython. with SIM, SPARQL and SQL

CIS192 Python Programming

LOOPS. Repetition using the while statement

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

Python Programming: Lecture 2 Data Types

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

Lecture 27. Lecture 27: Regular Expressions and Python Identifiers

SCHEME AND CALCULATOR 5b

Transcription:

Outline 1 Guessing Secrets functions returning functions oracles and trapdoor functions 2 anonymous functions lambda forms map(), reduce(), filter(), eval(), and apply() estimating π with list comprehensions 3 List Comprehensions algorithms and data structures sequences, dictionaries, lists 4 Summary + Assignments MCS 260 Lecture 15 Introduction to Computer Science Jan Verschelde, 15 February 2016 Intro to Computer Science (MCS 260) lambda and list comprehensions L-15 15 February 2016 1 / 34

guessing secrets A little game: try to guess a number. Typical repeat until: generate secret repeat ask for a guess until guess equals secret This game is typical for password verification. Intro to Computer Science (MCS 260) lambda and list comprehensions L-15 15 February 2016 2 / 34

lambda forms list comprehensions 1 Guessing Secrets functions returning functions oracles and trapdoor functions 2 anonymous functions lambda forms map(), reduce(), filter(), eval(), and apply() estimating π with list comprehensions 3 List Comprehensions algorithms and data structures sequences, dictionaries, lists 4 Summary + Assignments Intro to Computer Science (MCS 260) lambda and list comprehensions L-15 15 February 2016 3 / 34

functions returning functions Instead of storing the secret explicitly, we use an oracle. For a given input, the oracle will return True if the input matches the secret and return False otherwise. Our number guessing game with an oracle: oracle = generate_secret() repeat guess = input( give number : ) until oracle(guess) The function oracle() is a function computed by the function generate_secret(). Intro to Computer Science (MCS 260) lambda and list comprehensions L-15 15 February 2016 4 / 34

lambda forms list comprehensions 1 Guessing Secrets functions returning functions oracles and trapdoor functions 2 anonymous functions lambda forms map(), reduce(), filter(), eval(), and apply() estimating π with list comprehensions 3 List Comprehensions algorithms and data structures sequences, dictionaries, lists 4 Summary + Assignments Intro to Computer Science (MCS 260) lambda and list comprehensions L-15 15 February 2016 5 / 34

oracles and trapdoor functions password security Guarding of passwords on Unix: the password is encrypted, only the encrypted password is saved on file. Password verification consists in 1 calling the encryption algorithm on user input, 2 checking if the result of the encryption equals the encrypted password stored on file. The encryption algorithm acts as an oracle. The oracle is typically a trapdoor function: 1 efficient to compute output for any input, 2 very hard to compute the inverse of an output. Intro to Computer Science (MCS 260) lambda and list comprehensions L-15 15 February 2016 6 / 34

lambda forms list comprehensions 1 Guessing Secrets functions returning functions oracles and trapdoor functions 2 anonymous functions lambda forms map(), reduce(), filter(), eval(), and apply() estimating π with list comprehensions 3 List Comprehensions algorithms and data structures sequences, dictionaries, lists 4 Summary + Assignments Intro to Computer Science (MCS 260) lambda and list comprehensions L-15 15 February 2016 7 / 34

lambda forms (or anonymous functions) Lambda forms are functions without name, syntax: lambda < arguments > : < expression > Often we want to create functions rapidly, or to give shorter, more meaningful names. For example, to simulate the rolling of a die: >>> import random >>> die = lambda : random.randint(1,6) >>> die() 2 The function die() has no arguments, but just as with any other function, lambda forms can have default values, keyword and optional arguments. Intro to Computer Science (MCS 260) lambda and list comprehensions L-15 15 February 2016 8 / 34

functions as objects Use default arguments to extend die(): >>> import random >>> die = lambda a=1,b=6: random.randint(a,b) >>> die() 2 >>> die(0,100) 34 >>> die(a=-100) -29 Functions are also objects: >>> type(die) <type function > >>> die <function <lambda> at 0x40247294> Intro to Computer Science (MCS 260) lambda and list comprehensions L-15 15 February 2016 9 / 34

functions returning functions Recall the guessing of a secret. The secret itself is less important than its function: we want an oracle to separate those who know the secret from those who don t. Our application is to return a function def make_oracle(): "returns an oracle as a lambda form" numb = int(input( Give secret number : )) return lambda x: x == numb In the main program: ORACLE = make_oracle() Intro to Computer Science (MCS 260) lambda and list comprehensions L-15 15 February 2016 10 / 34

the guessing game with a lambda form def make_oracle(): "returns an oracle as a lambda form" numb = int(input( Give secret number : )) return lambda x: x == numb ORACLE = make_oracle() while True: GUESS = int(input( Guess the secret : )) if ORACLE(GUESS): break print wrong, try again print found the secret Intro to Computer Science (MCS 260) lambda and list comprehensions L-15 15 February 2016 11 / 34

lambda forms list comprehensions 1 Guessing Secrets functions returning functions oracles and trapdoor functions 2 anonymous functions lambda forms map(), reduce(), filter(), eval(), and apply() estimating π with list comprehensions 3 List Comprehensions algorithms and data structures sequences, dictionaries, lists 4 Summary + Assignments Intro to Computer Science (MCS 260) lambda and list comprehensions L-15 15 February 2016 12 / 34

the map() function to map functions to lists map() performs the same function on a sequence, syntax: map ( < function >, < sequence > ) where the function is often anonymous. Enumerate all letters of the alphabet: >>> ord( a ) 97 >>> chr(97) a >>> map(chr,range(97,97+26)) [ a, b, c,.., y, z ] observe the use of range Intro to Computer Science (MCS 260) lambda and list comprehensions L-15 15 February 2016 13 / 34

combining lists with map() Adding corresponding elements of lists: >>> a = range(0,3) >>> b = range(3,6) >>> map(lambda x,y: x+y, a,b) [3, 5, 7] To create tuples, use list() on zip(): >>> list(zip(a,b)) [(0, 3), (1, 4), (2, 5)] Let us add the elements in the tuples of m: >>> map(lambda x: x[0]+x[1], m) [3, 5, 7] Intro to Computer Science (MCS 260) lambda and list comprehensions L-15 15 February 2016 14 / 34

the reduce() function The function reduce() returns one single value from a list, for example to compute the sum: >>> r = range(0,10) >>> reduce(lambda x,y: x+y, r) 45 The function given as argument to reduce() must take two elements on input, return one single element. reduce() repeatedly replaces the first two elements of the list by the result of the function, applied to those first two elements, until only one element in the list is left Intro to Computer Science (MCS 260) lambda and list comprehensions L-15 15 February 2016 15 / 34

the filter() function Filters a sequence, subject to a criterion, syntax: filter ( < criterion >, < sequence > ) where criterion is a function returning a boolean, and the sequence is typically a list. The list on return contains all elements of the input list for which the criterion is True. Sieve methods to compute primes: >>> s = range(2,100) >>> s = filter(lambda x: x%2!= 0,s) >>> s = filter(lambda x: x%3!= 0,s) >>> s = filter(lambda x: x%5!= 0,s) >>> s = filter(lambda x: x%7!= 0,s) first element of the list s is always a prime Intro to Computer Science (MCS 260) lambda and list comprehensions L-15 15 February 2016 16 / 34

evaluation of expressions with eval() The eval() executes an expression string. def make_fun(): "user given expression becomes a function" expr = input( give an expression in x : ) return lambda x : eval(expr) FUN = make_fun() ARG = float(input( give a value : )) VAL = FUN(ARG) print( the expression evaluated at %f % ARG) print( gives %f % VAL) Intro to Computer Science (MCS 260) lambda and list comprehensions L-15 15 February 2016 17 / 34

illustration of evalshow.py (delayed evaluation) Running evalshow.py at the command prompt $: $ python evalshow.py give an expression in x : 2*x**6 - x + 9.9 give a value : -0.4523 the expression evaluated at -0.452300 gives 10.369423 $ With eval() we delay the evaluation of the expression entered by the user, till a value for the variable is provided. Intro to Computer Science (MCS 260) lambda and list comprehensions L-15 15 February 2016 18 / 34

the apply() function Syntax: apply ( < function name >, < arguments > ) Although y = apply(f,x) equals y = f(x), the apply is useful when not only the arguments, but also the function is only known at run time. Typical example: a simple calculator. $ python calculator.py give first operand : 3 give second operand : 4 operator? (+, -, *) * 3 * 4 = 12 Intro to Computer Science (MCS 260) lambda and list comprehensions L-15 15 February 2016 19 / 34

the program calculator.py (apply() avoids if else elif) from operator import add, sub, mul OPS = { + :add, - :sub, * : mul } A = int(input( give first operand : )) B = int(input( give second operand : )) ACT = input( operator? (+, -, *) ) C = apply(ops[act], (A, B)) print %d %s %d = %d % (A, ACT, B, C) Intro to Computer Science (MCS 260) lambda and list comprehensions L-15 15 February 2016 20 / 34

Monte Carlo without Loops a functional implementation Recall the Monte Carlo method to estimate π: 1 generate n points P in [0, 1] [0, 1] 2 m := { (x, y) P : x 2 + y 2 1 } 3 the estimate is then 4 m/n Main ingredients in a functional implementation: 1 U = lambda i: random.uniform(0, 1) 2 map U on range(0, n) twice, for x and y: X, Y 3 zip(x, Y) can with list() be a list of tuples 4 T = lambda (x, y): x**2 + y**2 <= 1 5 F = filter(t, Z) without explicit loops! Intro to Computer Science (MCS 260) lambda and list comprehensions L-15 15 February 2016 21 / 34

estimating π, Monte Carlo without loops from random import uniform N = int(input( Give number of samples : )) R = range(0, N) U = lambda i: uniform(0, 1) X = map(u, R) Y = map(u, R) Z = zip(x, Y) T = lambda (x, y): x**2 + y**2 <= 1 F = filter(t, Z) P = 4.0*len(F)/N print estimate for Pi : %f % P Intro to Computer Science (MCS 260) lambda and list comprehensions L-15 15 February 2016 22 / 34

lambda forms list comprehensions 1 Guessing Secrets functions returning functions oracles and trapdoor functions 2 anonymous functions lambda forms map(), reduce(), filter(), eval(), and apply() estimating π with list comprehensions 3 List Comprehensions algorithms and data structures sequences, dictionaries, lists 4 Summary + Assignments Intro to Computer Science (MCS 260) lambda and list comprehensions L-15 15 February 2016 23 / 34

list comprehensions >>> L = range(10) >>> L [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> [x**2 for x in L] [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] >>> f = lambda x: x**2 Then the typical list comprehension works as >>> [f(x) for x in L] [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] Intro to Computer Science (MCS 260) lambda and list comprehensions L-15 15 February 2016 24 / 34

making the product of two lists In the product of two lists A and B we take every x in A and put x in a tuple with every other y of B. >>> A = range(3); A [0, 1, 2] >>> B = range(4,6); B [4, 5] >>> Z = [(x,y) for x in A for y in B] >>> Z [(0, 4), (0, 5), (1, 4), (1, 5), (2, 4), (2, 5)] This is different from the zip of two lists: >>> C = range(4,7) >>> C [4, 5, 6] >>> Z = [(A[k], C[k]) for k in range(len(a))] >>> Z [(0, 4), (1, 5), (2, 6)] Intro to Computer Science (MCS 260) lambda and list comprehensions L-15 15 February 2016 25 / 34

estimating π with list comprehensions import random N = int(input( Give number of samples : )) X = [random.uniform(0, 1) for i in range(n)] Y = [random.uniform(0, 1) for i in range(n)] Z = [(X[k], Y[k]) for k in range(n)] T = lambda (x, y): x**2 + y**2 <= 1 R = [T(z) for z in Z] P = 4.0*sum(R)/len(R) print estimate for Pi : %f % P Intro to Computer Science (MCS 260) lambda and list comprehensions L-15 15 February 2016 26 / 34

lambda forms list comprehensions 1 Guessing Secrets functions returning functions oracles and trapdoor functions 2 anonymous functions lambda forms map(), reduce(), filter(), eval(), and apply() estimating π with list comprehensions 3 List Comprehensions algorithms and data structures sequences, dictionaries, lists 4 Summary + Assignments Intro to Computer Science (MCS 260) lambda and list comprehensions L-15 15 February 2016 27 / 34

Algorithms and Data Structures a summary of Python in the small Niklaus Wirth: programs = algorithms + data structures Three basic control structures in any algorithm: 1 sequence of statements 2 conditional statement: if else 3 iteration: while and for loop For every control structure, we have a matching data structure: control structures data structures 1 sequence tuple 2 if else dictionary 3 while / for list Intro to Computer Science (MCS 260) lambda and list comprehensions L-15 15 February 2016 28 / 34

lambda forms list comprehensions 1 Guessing Secrets functions returning functions oracles and trapdoor functions 2 anonymous functions lambda forms map(), reduce(), filter(), eval(), and apply() estimating π with list comprehensions 3 List Comprehensions algorithms and data structures sequences, dictionaries, lists 4 Summary + Assignments Intro to Computer Science (MCS 260) lambda and list comprehensions L-15 15 February 2016 29 / 34

programs are data transformations All data are sequences of bits, or bit tuples. Swapping values: >>> a = 1 >>> b = 2 >>> (b,a) = (a,b) >>> b 1 >>> a 2 Functions take sequences of arguments on input and return sequences on output. Intro to Computer Science (MCS 260) lambda and list comprehensions L-15 15 February 2016 30 / 34

storing conditions: dictionaries and if else statements We can represent an if else statement >>> import time >>> hour = time.localtime()[3] >>> if hour < 12:... print good morning... else:... print good afternoon... good afternoon via a dictionary: >>> d = { True: good morning,... False : good afternoon } >>> d[hour<12] good afternoon Intro to Computer Science (MCS 260) lambda and list comprehensions L-15 15 February 2016 31 / 34

loops and lists: storing the results of a for loop Printing all lower case characters: >>> for i in range(ord( a ),ord( z )):... print chr(i) A list of all lower case characters: >>> L = range(ord( a ),ord( z )) >>> map(chr,l) map() returns a list of the results of applying a function to a sequence of arguments. The while statement combines for with if else: conditional iteration. Intro to Computer Science (MCS 260) lambda and list comprehensions L-15 15 February 2016 32 / 34

list comprehensions: defining lists in a short way Instead of map(), filter(), etc... (eventually with lambda functions), list comprehensions provide a shorter way to create lists: To sample integer points on the parabola y = x 2 : >>> [(x,x**2) for x in range(0,3)] [(0, 0), (1, 1), (2, 4)] Generating three random numbers: >>> from random import uniform >>> L = [uniform(0,1) for i in range(0,3)] >>> [ %.3f % x for x in L] [ 0.843, 0.308, 0.272 ] Intro to Computer Science (MCS 260) lambda and list comprehensions L-15 15 February 2016 33 / 34

Summary + Assignments Background reading for this lecture: pages 181-182 in Python Programming in Context, pages 256-258 in Computer Science, an overview. Assignments: 1 Generate the list [(1,1),(1,2),(1,3),(1,4),..,(1,n)], for any given n. Use this list then to create all fractions 1.0/k, fork from 1 to n. Finally, use round() to round all fractions to two decimal places. n x k 2 Approximate the exponential function as k!. k=0 Write a Python program using map() and reduce(), to evaluate this approximation for given x and n. 3 Use list comprehensions to generate points (x, y) uniformly distributed on the circle: x 2 + y 2 = 1. (For some angle t: x = cos(t), y = sin(t).) Intro to Computer Science (MCS 260) lambda and list comprehensions L-15 15 February 2016 34 / 34