一亩三分地论坛

 找回密码
 获取更多干货,去instant注册!

扫码关注一亩三分地公众号
查看: 478|回复: 2
收起左侧

[CS61A]Disc03

[复制链接] |试试Instant~ |关注本帖
sky420 发表于 2015-5-20 11:17:10 | 显示全部楼层 |阅读模式

[其他]CS61A #2 - 04@UCBerkely

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看,没有帐号?获取更多干货,去instant注册!

x
Disc03 加分


评分

1

查看全部评分

wuxiaomin98 发表于 2015-5-30 04:37:58 | 显示全部楼层
居然忘记做这个了
这节课还是挺简单的,不过数学方面有时感觉理解起来有歧义
# 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]

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

reasonapp 发表于 2015-6-17 22:17:50 | 显示全部楼层
同一天做一个homework加一个discussion好累啊。。

dis03不难,不过量挺大的。。上代码:
# -*- coding: utf-8 -*-
#Dis 03
#1.1_1
def near_thirty(city, diff):
    latitude = get_lat(city)
    if abs(latitude-30) <= diff or abs(latitude + 30) <= diff:
        return True
    else:
        return False
   
#1.1_2
from math import sqrt
def closer_city(lat, lon, city1, city2):
    lat1, lon1 = get_lat(city1), get_lon(city1)
    lat2, lon2 = get_lat(city2), get_lon(city2)
    distance1 = sqrt((lat1 - lat) ** 2 + (lon1 - lon) ** 2)
    distance2 = sqrt((lat2 - lat) ** 2 + (lon2 - lon) ** 2)
    if distance1 == distance2:
        print 'same distance'
    if distance1 < distance2:
        return get_name(city1)
    else:
        return get_name(city2)
   
#2.1_1
from math import pow
def rational_pow(x, e):
    return rational(pow(numor(x),e),pow(denom(x),e) )
   

#2.1_2
def factorial(n):
    return 1 if n == 0 else n * factorial(n - 1)
def approx_e(iter=100):
    i = 0
    total = rational(0, 1)
    while i < iter:
        total = add_rationals(e, rational(1, factorial(k)))
        i += 1
    return total
   
#2.1_3
def inverse_rational(x):
    """Returns the inverse of the given non-zero rational
    number"""
    return rational(denom(x), numor(x))

def div_rationals(x, y):
    """Returns x / y for given rational x and non-zero
    rational y"""
    return mul_rationals(x, inverse_rational(y))
   
#3.1_1
def make_unit(catchphrase, damage):
    return [catchphrase, damage]
def get_catchphrase(unit):
    return unit[0]
def get_damage(unit):
    return unit[1]
   
#3.2
def battle(first, second):
    """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_catchphrase(first))
    print(get_catchphrase(second))
    if get_damage(first) >= get_damage(second):
        return first
    return second
   
#3.3
def pair(x, y):
    def get(index):
        if index == 0:
            return x
        elif index == 1:
            return y
    return get
   
def select(p, i):
    return p(i)

def make_resource_bundle(minerals, gas):
    return pair(minerals, gas)
def get_minerals(bundle):
    return select(bundle, 0)
def get_gas(bundle):
    return select(bundle, 1)
   
#3.4_1
def make_building(unit, bundle):
    return [unit, bundle]

def get_unit(building):
    return building[0]

def get_bundle(building):
    return building[1]
   
#3.4_2
def build_unit(building, bundle):
    """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)
    build_minerals = get_minerals(get_bundle(building))
    build_gas = get_gas(get_bundle(building))
    if minerals >= build_minerals and gas >= build_gas:
        unit = get_unit(building)
        return make_unit(get_catchphrase(unit),get_damage(unit))
    if minerals < build_minerals:
        print("We require more minerals!")
    if gas < build_gas:
        print("We require more gas!")
        
#3.4_3
def make_resource_bundle(minerals, gas):
    return [minerals, gas]
   
def get_minerals(bundle):
    return bundle[0]
   
def get_gas(bundle):
    return bundle[1]
回复 支持 反对

使用道具 举报

本版积分规则

请点这里访问我们的新网站:一亩三分地Instant.

Instant搜索更强大,不扣积分,内容组织的更好更整洁!目前仍在beta版本,努力完善中!反馈请点这里

关闭

一亩三分地推荐上一条 /5 下一条

手机版|小黑屋|一亩三分地论坛声明 ( 沪ICP备11015994号 )

custom counter

GMT+8, 2016-12-9 00:19

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

快速回复 返回顶部 返回列表