zoukankan      html  css  js  c++  java
  • 写了这么多行就给我30,呜呜呜

    class ShortInputError(Exception):
    def __init__(self):
    pass
    def __str__(self):
    return f'向量的长度不对'
    class Vector:
    # 构造方法,初始化,定义向量,需要把参数的各元素转化为浮点数
    def __init__(self, data):
    self.__data=data
    if isinstance(self.__data,tuple) or isinstance(self.__data,list):
    pass
    else:
    print('输出格式有误')
    def get(self):
    return self.__data,len(self.__data)
    # 与两一个向量相加,对应分量相加,返回新向量

    def __add__(self, anotherPoint):
    try:
    if len(self.__data)!=anotherPoint.get()[1]:
    raise ShortInputError()
    except Exception as result:
    print(result)
    else:
    print('是两个合法的向量')
    finally:
    ret = [self.get()[0][i]+anotherPoint.get()[0][i] for i in range(anotherPoint.get()[1])]
    return ret

    # 减去另一个向量,对应分量相减,返回新向量

    def __sub__(self, anotherPoint):
    try:
    if len(self.__data)!=anotherPoint.get()[1]:
    raise ShortInputError()
    except Exception as result:
    print(result)
    else:
    print('是两个合法的向量')
    finally:
    ret = [self.get()[0][i]-anotherPoint.get()[0][i] for i in range(anotherPoint.get()[1])]
    return ret
    # 向量与一个数字相乘,各分量乘以同一个数字,返回新向量

    def __mul__(self, n):
    ret=[i*n for i in self.__data]
    return ret
    # 向量除以一个数字,各分量除以同一个数字,返回新向量
    def __truediv__(self, n):
    if n==0:
    print('错误')
    return
    else:
    ret = [i / n for i in self.__data]
    return ret

    def __getitem__(self, index):
    if index>len(self.__data)-1 or index<-len(self.__data):
    print('错误')
    return
    else:
    return self.__data[index]

    def __setitem__(self, index, v):
    if index>len(self.__data)-1 or index<-len(self.__data):
    print('错误')
    return
    else:
    self.__data[index]=v
    return self.__data
    # 查看向量长度,所有分量平方和的平方根

    @property
    def length(self):
    ret = [i **2 for i in self.__data]
    return sum(ret)**0.5

    def __str__(self):
    return ('这是一个实例化对象')
    li=[1,2,3]
    li1=[4,5,6]
    li2=[3,4,5]
    a=Vector(li)
    b=Vector(li1)
    c=Vector(li2)
    ret=a.__getitem__(1)
    print(ret)
    ret=a.__add__(b)
    print(ret)
    ret=a.__sub__(b)
    print(ret)
    ret=a.__truediv__(0)
    print(ret)
    ret=a.__truediv__(0.5)
    print(ret)
    ret=a.__setitem__(1,4)
    print(ret)
    ret=c.length
    print(ret)
    print(c)
  • 相关阅读:
    Flume-NG源码分析-整体结构及配置载入分析
    Flume之核心架构深入解析
    使用maven构建scala项目
    大数据的一些面试题
    HBase原理和设计
    Hive UDAF开发详解
    Hive UDTF开发指南
    Hive UDF开发指南
    局域网访问电脑中VMware虚拟机
    百度面试-前端
  • 原文地址:https://www.cnblogs.com/diracy/p/14051619.html
Copyright © 2011-2022 走看看