农民代表
- 积分
- 7858
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2011-11-5
- 最后登录
- 1970-1-1
|
居然忘记做这个了
这节课还是挺简单的,不过数学方面有时感觉理解起来有歧义
# This is for cs61a discussion 2
def near_thirty(city, diff):
"""
1.1.1
This function is to check whether an input city is within diff
degrees of 30 dgrees N or 30 degrees S
"""
latitude = get_lat(city)
return abs(latitude - 30) <= diff or abs(latitude + 30) <= diff
from math import sqrt
def closer_city(lat, lon, city1, city2):
"""
1.1.2
Return the closer city
"""
lat_1, lon_1 = get_lat(city1), get_lon(city1)
lat_2, lon_2 = get_lat(city2), get_lon(city2)
if sqrt((lat_1 - lat) ** 2 + (lon_1 - lon) ** 2) > sqrt((lat_2 - lat) ** 2 + (lon_2 - lon) ** 2):
return city2
else:
return city1
from math import pow
def rational_pow(x, e):
"""
2.1.1
Return the given rational # raised to positive power e
"""
return rational(pow(numer(x), e), pow(denom(x), e))
def factorial(n):
return 1 if n == 0 else n * factorial(n - 1)
def approx_e(iter=100):
"""
2.1.2
Return a rational number approximation of e to iter amount of itertion
"""
e, i = 0, 0
while i <= iter:
e, i = add_rational(e, rational(1, factorial(i))), i + 1
return e
def inverse_rational(x):
"""
2.1.3
Returns the inverse of the given non_zero rational number
"""
a, b = numer(x), denom(x)
return rational(b, a)
def div_rationals(x, y):
"""
2.1.3
Returns x/y for given rational x and non_zero rational y
"""
return mul_rational(x, inverse_rational(y))
def make_unit(catchpahrse, damage):
"""
3.1.1
"""
return [catchpahrse, damage]
def get_catchpharase(unit):
return unit[0]
def get_damage(unit):
return unit[1]
def battle(first, second):
"""
3.1.1
Simulates a battle between the first and second unit
>>> zealot = make_unit(’My life for Aiur!’, 16)
>>> zergling = make_unit(’GRAAHHH!’, 5)
>>> winner = battle(zergling, zealot)
GRAAHHH!
My life for Aiur!
>>> winner is zealot
True
"""
print(get_catchpharase(first))
print(get_catchpharase(second))
if get_damage(first) >= get_damage(second):
return first
return second
def make_resource_bundle(minerals, gas):
"""
3.3.1
"""
return pair(minerals, gas)
def get_minerals(bundle):
return select(bundle, 0)
def get_gas(bundle):
return select(bundle, 1)
def make_building(unit, bundle):
"""
3.4.1
"""
return pair(unit, bundle)
def get_unit(building):
return building[0]
def get_bundle(building):
return building[1]
def build_unit(building, bundle):
"""
3.4.2
Constructs a unit if given the minimum amount of
resources. Otherwise, prints an error message.
>>> barracks = make_building(make_unit(’Go go go!’, 6),
.. make_resource_bundle(50, 0))
>>> marine = build_unit(barracks, make_resource_bundle(20,
20))
We require more minerals!
>>> marine = build_unit(barracks, make_resource_bundle(50,
0))
>>> print(get_catchphrase(marine))
Go go go!
"""
minerals, gas = get_minerals(bundle), get_gas(bundle)
b_minerals = get_minerals(get_bundle(building))
b_gas = get_gas(get_bundle(building))
if minerals >= b_minerals and gas >= b_gas:
unit = get_unit(building)
return make_unit(get_catchphrase(unit), get_damage(unit))
if minerals < b_minerals:
print("We require more minerals!")
if gas < b_gas:
print("We require more vespene gas!")
def make_resource_bundle(minerals, gas):
"""
3.4.3
"""
return [minerals, gas]
def get_minerals(bundle):
return bundle[0]
def get_gas(bundle):
return bundle[1]
|
|