一亩三分地论坛

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

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

[CS61A]Disc06

[复制链接] |试试Instant~ |关注本帖
sky420 发表于 2015-6-1 14:37:25 | 显示全部楼层 |阅读模式

[其他]CS61A #3 - 04@UCBerkely

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

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

x
Disc Link: http://gaotx.com/cs61a//disc/disc06.pdf
My solution: http://gaotx.com/blogs/2015/05/27/cs61a-disc06/
wuxiaomin98 发表于 2015-6-6 04:52:06 | 显示全部楼层
# This is for cs61a discussion 6

def take_color(self, color):
        """2.1.2"""
        for i in range(len(self.skittles)):
                if color == self.skittles[i].color:
                        return self.skittles.pop(i)
                return None

def take_all(self):
        """2.1.3"""
        for _ in range(len(self.skittles)):
                print(self.taken_skittle().color)

class Cat(Pet):
        """3.2.1"""
    def __init__(self, name, owner, lives=9):
        Pet.__init__(self, name, owner)
        self.lives = lives  
    def talk(self):
        """A cat says meow! when asked to talk."""
        print('meow!')
    def lose_life(self):
        """A cat can only lose a life if they have at least
        one life. When lives reach zero, the ’is_alive’
        variable becomes False.
        """
        if self.is_alive:
            self.lives -= 1
        if self.lives < 1:
            self.is_alive = False
            print(self.name + ' is dead!')

class NoisyCat(Cat):
    """3.3.1 A Cat that repeats things twice."""
    """Not necessary to have init"""
    def talk(self):
        """Repeat what a Cat says twice."""
        Cat.talk(self)
        Cat.talk(self)
回复 支持 反对

使用道具 举报

reasonapp 发表于 2015-6-26 01:27:19 | 显示全部楼层
disc06并不难嘛,上代码:
#Disc06
#Q2.1_2
def take_color(self, color):
    for i in range(len(skittles)):
        color_skittle = self.skittles[i].color
        if color_skittle == color:
            return self.skittles.pop(i)
    return None
   
#Q2.1_3
def take_all(self):
    for _ in range(len(self.skittles)):
        print(self.take_skittle().color)
        
#3.2_1
class Cat(Pet):
    def __init__(self, name, owner, lives=9):
        Pet.__init__(self, name, owner)
        self.lives = lives
    def talk(self):
        print('meow!')
    def lose_life(self):
        if self.lives > 0:
            self.lives -= 1
            if self.lives == 0:
                self.is_alive = False
        else:
            print(str(self.name) + " is dead.")
            
#Q4.2_1
def __neg__(self):
    return Vector([-i for i in self.vector])
   
#Q4.2_2
def __add_(self, other):
    assert type(other) == Vector, "Invalid operation!"
    assert len(self) == len(other), "Invalid dimensions!"
    return Vector([self.vector[i] + other.vector[i] for i in range(len(self))])

#Q4.2_3
def __mul__(self, other):
    if type(other) == int or type(other) == float:
        return Vector([self.vector[i] * other for i in range(len(self))])
    elif type(other) == Vector:
     assert len(self) == len(other), "Invalid dimensions!"   
     return sum([self[n] * other[n] for n in range(len(self))])   
回复 支持 反对

使用道具 举报

本版积分规则

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

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

关闭

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

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

custom counter

GMT+8, 2016-12-9 15:53

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

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