程序代写代做代考 1

1

2

3

4

pair
pair(a, b)
first(pair)
second(pair)
couple = pair(“Neil”, “David”)
neil = first(couple) # ‘Neil’
david = second(couple) # ‘David’
5

pair
def pair(a, b): def first(pair): def second(pair):
6

pair
def pair(a, b): return [a, b]
def first(pair): def second(pair):
6

pair
def pair(a, b): return [a, b]
def first(pair): return pair[0]
def second(pair):
6

pair
def pair(a, b): return [a, b]
def first(pair): return pair[0]
def second(pair): return pair[1]
6

7

rational(n,d)
numer(rat)
denom(rat)
quarter = rational(1, 4)
top = numer(quarter) # 1
bot = denom(quarter) # 4
8

9

def mul_rational(x, y): return rational(
numer(x) * numer(y),
denom(x) * denom(y))
def add_rational(x, y):
(nx, dx) = numer(x), denom(x)
(ny, dy) = numer(y), denom(y)
return rational(nx * dy + ny * dx, dx * dy)
mul_rational( rational(3, 2), rational(3, 5))
add_rational( rational(3, 2), rational(3, 5))
10

def print_rational(x): print(numer(x), ‘/’, denom(x))
def rationals_are_equal(x, y):
return numer(x) * denom(y) == numer(y) * denom(x)
print_rational( rational(3, 2) ) # 3/2
rationals_are_equal( rational(3, 2), rational(3, 2) ) # True
11

def rational(n, d):
“””Construct a rational number that represents N/D.””” return [n, d]
def numer(x):
“””Return the numerator of rational number X.””” return x[0]
def denom(x):
“””Return the denominator of rational number X.””” return x[1]
12

add_rational( rational(3, 4), rational(2, 16) ) # 56/64
add_rational( rational(3, 4), rational(4, 16) ) # 64/64
13

add_rational( rational(3, 4), rational(2, 16) ) # 56/64
add_rational( rational(3, 4), rational(4, 16) ) # 64/64
13

add_rational( rational(3, 4), rational(2, 16) ) # 56/64
add_rational( rational(3, 4), rational(4, 16) ) # 64/64
from math import gcd
def rational(n, d):
“””Construct a rational that represents n/d in lowest terms.””” g = gcd(n, d)
return [n//g, d//g]
13

def exact_harmonic_number(n):
“””Return 1 + 1/2 + 1/3 + … + 1/N as a rational n s = rational(0, 1)
for k in range(1, n + 1):
s = add_rat(s, rational(1, k)) return s
14
u
m

15

[..,..] [0] [1]
make_rat() numer() denom()
add_rat() mul_rat() print_rat() equal_rat()
exact_harmonic_number()
16

add_rational( [1, 2], [1, 4] )
def divide_rational(x, y):
return [ x[0] * y[1], x[1] * y[0] ]
17

add_rational( [1, 2], [1, 4] )
# Doesn’t use constructors!
def divide_rational(x, y):
return [ x[0] * y[1], x[1] * y[0] ]
17

add_rational( [1, 2], [1, 4] )
# Doesn’t use constructors!
def divide_rational(x, y):
return [ x[0] * y[1], x[1] * y[0] ] # Doesn’t use selectors!
17

rational()
def rational(n, d): def select(name):
if name == ‘n’: return n
elif name == ‘d’: return d
return select def numer(x):
return x(‘n’)
def denom(x): return x(‘d’)
18

19

0 -1 0xFF 0b1101
True False
def f(x)… lambda x: … “pear””I say, \”hello!\”” range(11) range(1, 6)
[] [“apples”, “bananas”]
[x**3 for x in range(2)]
20

21

dict
states = {
“CA”: “California”,
“DE”: “Delaware”,
“NY”: ” “,
“TX”: “Texas”,
“WY”: “Wyoming”
}
>>> len(states)
>>> “CA” in states
>>> “ZZ” in states
22

dict
states = {
“CA”: “California”,
“DE”: “Delaware”,
“NY”: ” “,
“TX”: “Texas”,
“WY”: “Wyoming”
}
>>> len(states)
5
>>> “CA” in states
>>> “ZZ” in states
22

dict
states = {
“CA”: “California”,
“DE”: “Delaware”,
“NY”: ” “,
“TX”: “Texas”,
“WY”: “Wyoming”
}
>>> len(states)
5
>>> “CA” in states True
>>> “ZZ” in states
22

dict
states = {
“CA”: “California”,
“DE”: “Delaware”,
“NY”: ” “,
“TX”: “Texas”,
“WY”: “Wyoming”
}
>>> len(states)
5
>>> “CA” in states True
>>> “ZZ” in states False
22

words = {
“m¨s”: “more”,
“otro”: “other”,
“agua”: “water”
}
>>> words[“otro”]
>>> first_word = “agua”
>>> words[first_word]
>>> words[“pavo”]
>>> words.get(“pavo”, “”)
23

words = {
“m¨s”: “more”,
“otro”: “other”,
“agua”: “water”
}
>>> words[“otro”]
‘other’
>>> first_word = “agua”
>>> words[first_word]
>>> words[“pavo”]
>>> words.get(“pavo”, “”)
23

words = {
“m¨s”: “more”,
“otro”: “other”,
“agua”: “water”
}
>>> words[“otro”]
‘other’
>>> first_word = “agua”
>>> words[first_word]
‘water’
>>> words[“pavo”]
>>> words.get(“pavo”, “”)
23

words = {
“m¨s”: “more”,
“otro”: “other”,
“agua”: “water”
}
>>> words[“otro”]
‘other’
>>> first_word = “agua”
>>> words[first_word]
‘water’
>>> words[“pavo”]
KeyError: pavo
>>> words.get(“pavo”, “”)
23

words = {
“m¨s”: “more”,
“otro”: “other”,
“agua”: “water”
}
>>> words[“otro”]
‘other’
>>> first_word = “agua”
>>> words[first_word]
‘water’
>>> words[“pavo”]
KeyError: pavo
>>> words.get(“pavo”, “”)

23

spiders = {
“smeringopus”: {
“name”: “Pale Daddy Long-leg”,
“length”: 7
},
“holocnemus pluchei”: {
} }
“name”: “Marbled cellar spider”,
“length”: (5, 7)
24

insects = {“spiders”: 8, “centipedes”: 100, “bees”: 6} for name in insects:
print(insects[name])
25

insects = {“spiders”: 8, “centipedes”: 100, “bees”: 6} for name in insects:
print(insects[name]) 8 100 6
25

{key: value for in } {x: x*x for x in range(3,6)}
26

def prune(d, keys):
“””Return a copy of D which only contains key/value pairs whose keys are also in KEYS.
>>> prune({“a”: 1, “b”: 2, “c”: 3, “d”: 4}, [“a”, “b”, “c”]) {‘a’: 1, ‘b’: 2, ‘c’: 3}
“””
27

def prune(d, keys):
“””Return a copy of D which only contains key/value pairs whose keys are also in KEYS.
>>> prune({“a”: 1, “b”: 2, “c”: 3, “d”: 4}, [“a”, “b”, “c”]) {‘a’: 1, ‘b’: 2, ‘c’: 3}
“””
return {k: d[k] for k in keys}
28

def index(keys, values, match):
“””Return a dictionary from keys k to a list of values v for which match(k, v) is a true value.
>>> index([7, 9, 11], range(30, 50), lambda k, v: v % k == 0)
{7: [35, 42, 49], 9: [36, 45], 11: [33, 44]}
“””
29

def index(keys, values, match):
“””Return a dictionary from keys k to a list of values v for which match(k, v) is a true value.
>>> index([7, 9, 11], range(30, 50), lambda k, v: v % k == 0) {7: [35, 42, 49], 9: [36, 45], 11: [33, 44]}
“””
return {k: [v for v in values if match(k, v)] for k in keys}
30

[ [1, 2], [3, 4] ]
{“name”: “Brazilian Breads”, “location”: {“lat”:
37.8, “lng”: -122}}
{“heights”: [89, 97], “ages”: [6, 8]}
[{“title”: “Ponyo”, “year”: 2009}, {“title”:
“Totoro”, “year”: 1993}]
31

32

33