数据结构与算法 Python语言实现 第二章课后习题

2.5-2.8
在这里插入图片描述

class CreditCard():    #2.5-2.9
    def __init__(self,customer,bank,acnt,limit,balance = 0):
        self._customer = customer
        self._bank = bank
        self._account = acnt
        self._limit = limit
        self._balance = balance
    
    def get_customer(self):
        return self._customer
    def get_bank(self):
        self._bank
    def get_account(self):
        return self._account
    def get_limit(self):
        return self._limit
    def get_balance(self):
        return self._balance
    
    def charge(self,price):
        try:
            if price + self._balance  > self._limit:
                return False
            else:
                self._balance += price
                return True
        except TypeError:
            print('请输入数字')
    
    def make_payment(self,amount):
        if amount >= 0:
            self._balance = amount
        else:
            raise ValueError 
if __name__ == '__main__':
    wallet = []
    wallet.append(CreditCard('John Bowman', 'California Savings',
                             '5391 0375 9387 5309', 2500))
    wallet.append(CreditCard('John Bowman', 'California Federal',
                             '3485 0399 3395 1954', 3500))
    wallet.append(CreditCard('John Bowman', 'California Finance',
                             '5391 0375 9387 5309', 5000))

    for val in range(1, 17):
        wallet[0].charge(val)
        wallet[1].charge(2 * val)
        wallet[2].charge(3 * val)

    for c in range(3):
        print('Customer =', wallet[c].get_customer())
        print('Bank =', wallet[c].get_bank())
        print('Account =', wallet[c].get_account())
        print('Limit =', wallet[c].get_limit())
        print('Balance =', wallet[c].get_balance())
        while wallet[c].get_balance() > 100:
            wallet[c].make_payment(100)
            print('New balance =', wallet[c].get_balance())

    
    def __init__(self,name = 0,num = 0,price = 0):
        self.name = name
        self.num = num
        self.price = prio

2.9-2.15
在这里插入图片描述

在这里插入图片描述

import copy
class Vector():
    def __init__(self,d):          #2.15
        if isinstance(d,list):
            self._coords = copy.deepcopy(d)
        else:
            self._coords = [0] * d
        
    def __len__(self):
        return len(self._coords)
    
    def __getitem__(self,j):
        return self._coords[j]
    
    def __setitem__(self,j,val):
        self._coords[j] = val
    
    def __add__(self,other):
        if len(self) != len(other):
            raise ValueError ('长度必须一致')
        result = Vector(len(self))
        for i in range(len(self)):
            result[i] = self[i] +other[i]
        return result
    
    def __sub__(self,other):              #2.9
        if len(self) != len(other):
            raise ValueError ('长度必须一致')
        result = Vector(len(self))
        for i in range(len(self)):
            result[i] = self[i] - other[i]
        return result 
    def __neg__(self):                   #2.10
        result = Vector(len(self)) 
        for i in range(len(self)):
            if self[i] >= 0:
                result[i] = -self[i]
            else:
                result[i] = self[i]
        return result
    # def __mul__(self,other):             #2.12
    #     result = Vector(len(self))
    #     for i in range(len(self)):
    #         result[i] = self[i] * other
    #     return result
    
    def __rmul__(self,other):               #2.13
        result = Vector(len(self))
        for i in range(len(self)):
            result[i] = self[i] * other
        return result
    
    def __radd__(self,other):              #2.11
        if len(self) != len(other):
            raise ValueError ('长度必须一致')
        result = Vector(len(self))
        for i in range(len(self)):
            result[i] = self[i] +other[i]
        return result
     
    def __mul__(self,other):            #2.14
        result = 0
        for i in range(len(self)):
            result += self[i] * other[i]
        return result            
    def __eq__(self,other):
        return self._coords == other._coords
    
    def __ne__(self,other):
        return not self == other
    
    def __str__(self):
        return '<' + str(self._coords)[1:-1] + '>'
    __repr__ = __str__       #直接输入实例名即可显示该实例的坐标,无需print方法

2.16
在这里插入图片描述

在这里插入图片描述

2.17(复习ing,随后更)

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值