计算机代考程序代写 python Function Examples – cscodehelp代写

Function Examples

Announcements

Hog Contest Rules
•Up to two people submit one entry; Max of one entry per person
•Your score is the number of entries against which you win more than 50.00001% of the time
•Strategies are time-limited
•All strategies must be deterministic, pure functions of the players’ scores
•Winning entries will receive a paltry amount of extra credit
•The real prize: honor and glory •See website for detailed rules
Fall 2011 Winners
Kaylee Duan & Ziming Li
&
&
Fall 2012 Winners
Chenyang 2013 Winners
Paul Bramsen
Sam Kumar & Kangsik Lee

Fall 2014 Winners
Alan Tong &

Villaflor &
&
& Yihe Li
cs61a.org/proj/hog_contest
3

Hog Contest Winners
Spring 2015 Winners
Sinho Chewi & Li
Stella Tao and
Fall 2015 Winners
Micah Carroll &

and
Spring 2016 Winners
Michael McDonald and Tianrui Chen
Andrei Kassiantchouk

Fall 2016 Winners
Will Gan &
&
Mingwei Samuel
&
& Zekai Fan
Spring 2017 Winners
Cindy Jin and Sunjoon Lee
and
and
and
Fall 2017 Winners
Alex Yu and

Spring 2018 Winners
Eric

Fall 2018 Winners
Rahul Kohli and
4

Hog Contest Winners
Fall 2019 Winners
Spring 2020 Winners
Fall 2020 Winners
Jet Situ and
and
and

and
– Sen
Samuel Berkun
Mitchell de Li
Aaron 2021 Winners
5
Your name could be here FOREVER!

Review

What Would Python Display?
The print function returns None. It also displays its arguments
(separated by spaces) when it is called.
from operator import add, mul
def square(x):
return mul(x, x)
A function that takes any
argument and returns a
function that returns
that arg
def delay(arg):
print(‘delayed’)
def g():
return arg
return g
Names in nested def
statements can refer to
their enclosing scope
This expression
Evaluates to
Interactive
Output
555 print(5) None 5
print(print(5)) None
None
5 None
delayed delay(delay)()(6)() 6 delayed
6
delayed print(delay(print)()(4)) None 4
None
7

What Would Python Print?
The print function returns None. It also displays its arguments
(separated by spaces) when it is called.
from operator import add, mul
def square(x):
return mul(x, x)
A function that
always returns the
identity function
def pirate(arggg):
print(‘matey’)
def plunder(arggg):
return arggg
return plunder
This expression
add(pirate(3)(square)(4), 1)
func square(x)
16
Evaluates to
17
Interactive
Output
Matey 17
Matey
Matey
Error
pirate(pirate(pirate))(5)(7) Error
Identity function
5
A name evaluates to the value bound to that name in the earliest frame of the current environment
in which that name is found.
8

Global frame
def horse(mask):
horse = mask
def mask(horse):
return horse
return horse(mask)
mask = lambda horse: horse(2)
horse(mask)
func horse(mask) [parent=Global]
func λ(horse) [parent=Global]
func mask(horse) [parent=f1]
f1: horse [parent=Global] mask
horse
Return Value
2
horse mask
f2: λ [parent=Global] horse
Return Value
2
f3: mask [parent=f1]
horse 2
Return Value
2

Implementing Functions

Implementing a Function
def remove(n, digit):
“””Return all digits of non-negative N
Read the description
Verify the examples & pick a simple one
Read the template
Implement without the template, then change your implementation to match the template. OR
If the template is helpful, use it.
Annotate names with values from your chosen
example
Write code to compute the result
Did you really return the right thing?
Check your solution with the other examples
11
that are not DIGIT, for some
231 43
non-negative DIGIT less than 10.
11 +20 +30 + 200 21 231
while ________________________________:
n, last = n // 10, n % 10
>>> remove(231, 3)
21
>>> remove(243132, 2)
4313
“””
kept, digits = 0, 0
n>0
last != digit
if _______________________________:
10*kept + last*10**digits
kept = _______________________
231
digits + 1
digits = _____________________
return _______________________________
kept

Implementing a Function
def remove(n, digit):
“””Return all digits of non-negative N
Read the description
Verify the examples & pick a simple one
Read the template
Implement without the template, then change your implementation to match the template. OR
If the template is helpful, use it.
Annotate names with values from your chosen
example
Write code to compute the result
Did you really return the right thing?
Check your solution with the other examples
12
that are not DIGIT, for some
231 3
non-negative DIGIT less than 10.
>>> remove(231, 3)
21
>>> remove(243132, 2)
4313
“””
kept, digits = 0, 0
n>0
while ________________________________:
n, last = n // 10, n % 10
21
digits + 1
last != digit
if _______________________________:
kept/10 + last
kept = _______________________
digits = _____________________
round(kept * 10 ** (digits-1))
return _______________________________

Decorators

Function Decorators
Function
decorator
(Demo)
@trace1
def triple(x):
return 3 * x
is identical to
def triple(x):
return 3 * x
triple = trace1(triple)
Decorated
function
Why not just
use this?
14

Leave a Reply

Your email address will not be published. Required fields are marked *