📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
查看: 2130| 回复: 2
跳转到指定楼层
上一主题 下一主题
收起左侧

[CS61A]Disc06

全局:
公开课
学校名称: UCBerkely
Unit号: 3
开课时间: 04
课程全名: CS61A
平台: 其他

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

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
Disc Link: http://gaotx.com/cs61a//disc/disc06.pdf
My solution: http://gaotx.com/blogs/2015/05/27/cs61a-disc06/

上一篇:[CS61A]Quiz02
下一篇:[CS61A]HW06
🔗
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))])   
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

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