def couple(s, t):
"""Return a list of two-element lists in which the i-th element is [s[i], t[i]].
>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> couple(a, b)
[[1, 4], [2, 5], [3, 6]]
>>> c = ['c', 6]
>>> d = ['s', '1']
>>> couple(c, d)
[['c', 's'], [6, '1']]
"""
assert len(s) == len(t)
"*** YOUR CODE HERE ***"
couple_st = []
for i in range(len(s)):
tmp = []
tmp.append(s[i])
tmp.append(t[i])
couple_st.append(tmp)
return couple_st
from math import sqrt
def distance(city_a, city_b):
"""
>>> city_a = make_city('city_a', 0, 1)
>>> city_b = make_city('city_b', 0, 2)
>>> distance(city_a, city_b)
1.0
>>> city_c = make_city('city_c', 6.5, 12)
>>> city_d = make_city('city_d', 2.5, 15)
>>> distance(city_c, city_d)
5.0
"""
"*** YOUR CODE HERE ***"
a_x ,b_x= get_lat(city_a),get_lat(city_b)
a_y , b_y = get_lon(city_a) , get_lon(city_b)
return sqrt((a_x-b_x)**2+(a_y - b_y)**2)
def closer_city(lat, lon, city_a, city_b):
"""
Returns the name of either city_a or city_b, whichever is closest to
coordinate (lat, lon). If the two cities are the same distance away
from the coordinate, consider city_b to be the closer city.
>>> berkeley = make_city('Berkeley', 37.87, 112.26)
>>> stanford = make_city('Stanford', 34.05, 118.25)
>>> closer_city(38.33, 121.44, berkeley, stanford)
'Stanford'
>>> bucharest = make_city('Bucharest', 44.43, 26.10)
>>> vienna = make_city('Vienna', 48.20, 16.37)
>>> closer_city(41.29, 174.78, bucharest, vienna)
'Bucharest'
"""
"*** YOUR CODE HERE ***"
my_city = make_city('my_city',lat,lon)
distance1 = distance(my_city,city_a)
distance2 = distance(my_city,city_b)
return get_name(city_a) if distance1 < distance2 else get_name(city_b)
def check_city_abstraction():
"""
There's nothing for you to do for this function, it's just here for the extra doctest
>>> change_abstraction(True)
>>> city_a = make_city('city_a', 0, 1)
>>> city_b = make_city('city_b', 0, 2)
>>> distance(city_a, city_b)
1.0
>>> city_c = make_city('city_c', 6.5, 12)
>>> city_d = make_city('city_d', 2.5, 15)
>>> distance(city_c, city_d)
5.0
>>> berkeley = make_city('Berkeley', 37.87, 112.26)
>>> stanford = make_city('Stanford', 34.05, 118.25)
>>> closer_city(38.33, 121.44, berkeley, stanford)
'Stanford'
>>> bucharest = make_city('Bucharest', 44.43, 26.10)
>>> vienna = make_city('Vienna', 48.20, 16.37)
>>> closer_city(41.29, 174.78, bucharest, vienna)
'Bucharest'
>>> change_abstraction(False)
"""
# Treat all the following code as being behind an abstraction layer, you shouldn't need to look at it!
def make_city(name, lat, lon):
"""
>>> city = make_city('Berkeley', 0, 1)
>>> get_name(city)
'Berkeley'
>>> get_lat(city)
0
>>> get_lon(city)
1
"""
if change_abstraction.changed:
return {"name" : name, "lat" : lat, "lon" : lon}
else:
return [name, lat, lon]
def get_name(city):
"""
>>> city = make_city('Berkeley', 0, 1)
>>> get_name(city)
'Berkeley'
"""
if change_abstraction.changed:
return city["name"]
else:
return city[0]
def get_lat(city):
"""
>>> city = make_city('Berkeley', 0, 1)
>>> get_lat(city)
0
"""
if change_abstraction.changed:
return city["lat"]
else:
return city[1]
def get_lon(city):
"""
>>> city = make_city('Berkeley', 0, 1)
>>> get_lon(city)
1
"""
if change_abstraction.changed:
return city["lon"]
else:
return city[2]
def change_abstraction(change):
change_abstraction.changed = change
change_abstraction.changed = False
def berry_finder(t):
"""Returns True if t contains a node with the value 'berry' and
False otherwise.
>>> scrat = tree('berry')
>>> berry_finder(scrat)
True
>>> sproul = tree('roots', [tree('branch1', [tree('leaf'), tree('berry')]), tree('branch2')])
>>> berry_finder(sproul)
True
>>> numbers = tree(1, [tree(2), tree(3, [tree(4), tree(5)]), tree(6, [tree(7)])])
>>> berry_finder(numbers)
False
>>> t = tree(1, [tree('berry',[tree('not berry')])])
>>> berry_finder(t)
True
"""
"*** YOUR CODE HERE ***"
if label(t) == 'berry':
return True
else :
if any(berry_finder(b) for b in branches(t)) :
return True
else :
return False
def sprout_leaves(t, leaves):
"""Sprout new leaves containing the data in leaves at each leaf in
the original tree t and return the resulting tree.
>>> t1 = tree(1, [tree(2), tree(3)])
>>> print_tree(t1)
1
2
3
>>> new1 = sprout_leaves(t1, [4, 5])
>>> print_tree(new1)
1
2
4
5
3
4
5
>>> t2 = tree(1, [tree(2, [tree(3)])])
>>> print_tree(t2)
1
2
3
>>> new2 = sprout_leaves(t2, [6, 1, 2])
>>> print_tree(new2)
1
2
3
6
1
2
"""
"*** YOUR CODE HERE ***"
if is_leaf(t):
return tree(label(t), [tree(leaf) for leaf in leaves])
else :
return tree(label(t),[sprout_leaves(b,leaves) for b in branches(t)])
# t1 = tree(1, [tree(2), tree(3)])
# new1 = sprout_leaves(t1, [4, 5])
# print_tree(new1)
# Abstraction tests for sprout_leaves and berry_finder
def check_abstraction():
"""
There's nothing for you to do for this function, it's just here for the extra doctest
>>> change_abstraction(True)
>>> scrat = tree('berry')
>>> berry_finder(scrat)
True
>>> sproul = tree('roots', [tree('branch1', [tree('leaf'), tree('berry')]), tree('branch2')])
>>> berry_finder(sproul)
True
>>> numbers = tree(1, [tree(2), tree(3, [tree(4), tree(5)]), tree(6, [tree(7)])])
>>> berry_finder(numbers)
False
>>> t = tree(1, [tree('berry',[tree('not berry')])])
>>> berry_finder(t)
True
>>> t1 = tree(1, [tree(2), tree(3)])
>>> print_tree(t1)
1
2
3
>>> new1 = sprout_leaves(t1, [4, 5])
>>> print_tree(new1)
1
2
4
5
3
4
5
>>> t2 = tree(1, [tree(2, [tree(3)])])
>>> print_tree(t2)
1
2
3
>>> new2 = sprout_leaves(t2, [6, 1, 2])
>>> print_tree(new2)
1
2
3
6
1
2
>>> change_abstraction(False)
"""
def coords(fn, seq, lower, upper):
"""
>>> seq = [-4, -2, 0, 1, 3]
>>> fn = lambda x: x**2
>>> coords(fn, seq, 1, 9)
[[-2, 4], [1, 1], [3, 9]]
"""
"*** YOUR CODE HERE ***"
# result = []
# for s in seq :
# tmp = []
# if fn(s) >= lower and fn(s) <= upper :
# tmp.append(s)
# tmp.append(fn(s))
# result.append(tmp)
# return result
return [[x,fn(x)]for x in seq if fn(x)>= lower and fn(x) <= upper]
def riffle(deck):
"""Produces a single, perfect riffle shuffle of DECK, consisting of
DECK[0], DECK[M], DECK[1], DECK[M+1], ... where M is position of the
second half of the deck. Assume that len(DECK) is even.
>>> riffle([3, 4, 5, 6])
[3, 5, 4, 6]
>>> riffle(range(20))
[0, 10, 1, 11, 2, 12, 3, 13, 4, 14, 5, 15, 6, 16, 7, 17, 8, 18, 9, 19]
"""
"*** YOUR CODE HERE ***"
return [deck[i+((i%2)*(len(deck)//2 -1))- i //2 ]for i in range(len(deck))]
def add_trees(t1, t2):
"""
>>> numbers = tree(1,
... [tree(2,
... [tree(3),
... tree(4)]),
... tree(5,
... [tree(6,
... [tree(7)]),
... tree(8)])])
>>> print_tree(add_trees(numbers, numbers))
2
4
6
8
10
12
14
16
>>> print_tree(add_trees(tree(2), tree(3, [tree(4), tree(5)])))
5
4
5
>>> print_tree(add_trees(tree(2, [tree(3)]), tree(2, [tree(3), tree(4)])))
4
6
4
>>> print_tree(add_trees(tree(2, [tree(3, [tree(4), tree(5)])]), \
tree(2, [tree(3, [tree(4)]), tree(5)])))
4
6
8
5
5
"""
"*** YOUR CODE HERE ***"
if is_leaf(t1) and is_leaf(t2):
return tree(label(t1)+label(t2),[])
elif is_leaf(t1):
return tree(label(t1)+label(t2),[b for b in branches(t2)])
elif is_leaf(t2):
return tree(label(t1)+label(t2),[b for b in branches(t1)])
else :
b1 , b2 = branches(t1) , branches(t2)
len1, len2 = len(b1),len(b2)
if len1>len2 :
zip_t = zip(b1,b2)
return tree(label(t1)+label(t2),[add_trees(x,y)for x ,y in zip_t]+ b1[len2:])
elif len2 > len1 :
zip_t = zip(b1 , b2 )
return tree(label(t1)+label(t2),[add_trees(x,y)for x ,y in zip_t]+ b2[len1:])
else :
zip_t = zip(b1,b2)
return tree(label(t1)+label(t2),[add_trees(x,y)for x ,y in zip_t])
def build_successors_table(tokens):
"""Return a dictionary: keys are words; values are lists of successors.
>>> text = ['We', 'came', 'to', 'investigate', ',', 'catch', 'bad', 'guys', 'and', 'to', 'eat', 'pie', '.']
>>> table = build_successors_table(text)
>>> sorted(table)
[',', '.', 'We', 'and', 'bad', 'came', 'catch', 'eat', 'guys', 'investigate', 'pie', 'to']
>>> table['to']
['investigate', 'eat']
>>> table['pie']
['.']
>>> table['.']
['We']
"""
table = {}
prev = '.'
for word in tokens:
if prev not in table:
table[prev] = [word]
else:
table[prev].append(word)
prev = word
return table
def construct_sent(word, table):
"""Prints a random sentence starting with word, sampling from
table.
>>> table = {'Wow': ['!'], 'Sentences': ['are'], 'are': ['cool'], 'cool': ['.']}
>>> construct_sent('Wow', table)
'Wow!'
>>> construct_sent('Sentences', table)
'Sentences are cool.'
"""
import random
result = ''
while word not in ['.', '!', '?']:
"*** YOUR CODE HERE ***"
result+= ' '+word
word = random.choice(table[word])
return result.strip() + word
def shakespeare_tokens(path='shakespeare.txt', url='http://composingprograms.com/shakespeare.txt'):
"""Return the words of Shakespeare's plays as a list."""
import os
from urllib.request import urlopen
if os.path.exists(path):
return open(path, encoding='ascii').read().split()
else:
shakespeare = urlopen(url)
return shakespeare.read().decode(encoding='ascii').split()
# Uncomment the following two lines
tokens = shakespeare_tokens()
table = build_successors_table(tokens)
def random_sent():
import random
return construct_sent(random.choice(table['.']), table)
print(random_sent())
# Tree ADT
def tree(label, branches=[]):
"""Construct a tree with the given label value and a list of branches."""
if change_abstraction.changed:
for branch in branches:
assert is_tree(branch), 'branches must be trees'
return {'label': label, 'branches': list(branches)}
else:
for branch in branches:
assert is_tree(branch), 'branches must be trees'
return [label] + list(branches)
def label(tree):
"""Return the label value of a tree."""
if change_abstraction.changed:
return tree['label']
else:
return tree[0]
def branches(tree):
"""Return the list of branches of the given tree."""
if change_abstraction.changed:
return tree['branches']
else:
return tree[1:]
def is_tree(tree):
"""Returns True if the given tree is a tree, and False otherwise."""
if change_abstraction.changed:
if type(tree) != dict or len(tree) != 2:
return False
for branch in branches(tree):
if not is_tree(branch):
return False
return True
else:
if type(tree) != list or len(tree) < 1:
return False
for branch in branches(tree):
if not is_tree(branch):
return False
return True
def is_leaf(tree):
"""Returns True if the given tree's list of branches is empty, and False
otherwise.
"""
return not branches(tree)
def change_abstraction(change):
change_abstraction.changed = change
change_abstraction.changed = False
def print_tree(t, indent=0):
"""Print a representation of this tree in which each node is
indented by two spaces times its depth from the root.
>>> print_tree(tree(1))
1
>>> print_tree(tree(1, [tree(2)]))
1
2
>>> numbers = tree(1, [tree(2), tree(3, [tree(4), tree(5)]), tree(6, [tree(7)])])
>>> print_tree(numbers)
1
2
3
4
5
6
7
"""
print(' ' * indent + str(label(t)))
for b in branches(t):
print_tree(b, indent + 1)
def copy_tree(t):
"""Returns a copy of t. Only for testing purposes.
>>> t = tree(5)
>>> copy = copy_tree(t)
>>> t = tree(6)
>>> print_tree(copy)
5
"""
return tree(label(t), [copy_tree(b) for b in branches(t)])
CS 61A Fall 2020 lab05
最新推荐文章于 2024-05-19 11:09:09 发布