• 【Python养成】:案例(设计三维向量类、实现向量的加法、减法以及向量与标量的乘法和除法运算、编写自定义类,模拟内置集、编写自定义类,模拟双端队列。)


     

    学习内容:设计三维向量类、实现向量的加法、减法以及向量与标量的乘法和除法运算

            设计三维向量类、实现向量的加法、减法以及向量与标量的乘法和除法运算

    实验代码:

    1. class Vector_3D:
    2. def __init__(self, x, y, z):
    3. self.__x = x
    4. self.__y = y
    5. self.__z = z
    6. def add(self, other):
    7. x = self.__x + other.__x
    8. y = self.__y + other.__y
    9. z = self.__z + other.__z
    10. return Vector_3D(x, y, z)
    11. def Minus(self, other):
    12. x = self.__x - other.__x
    13. y = self.__y - other.__y
    14. z = self.__z - other.__z
    15. return Vector_3D(x, y, z)
    16. def Multiply(self, a):
    17. x = self.__x * a
    18. y = self.__y * a
    19. z = self.__z * a
    20. return Vector_3D(x, y, z)
    21. def Except(self, a):
    22. x = self.__x / a
    23. y = self.__y / a
    24. z = self.__z / a
    25. return Vector_3D(x, y, z)
    26. def show(self):
    27. print('X:{0}, Y:{1}, Z:{2}'.format(self.__x,self.__y,self.__z))
    28. def lenght(self):
    29. return (self.__x ** 2 + self.__y ** 2 + self.__z ** 2) ** 0.5
    30. b = Vector_3D(1, 2, 3)
    31. b1 = b.Multiply(4)
    32. b1.show()
    33. b2 = b1.add(b)
    34. b2.show()
    35. print(b2.lenght())

    学习内容:编写自定义类,模拟内置集

            编写自定义类,模拟内置集

    实验代码:

    1. class Set(object):
    2. def __init__(self, data=None):
    3. if data is None:
    4. self.__data = []
    5. else:
    6. raise Exception('必须是可迭代的数据')
    7. temp = []
    8. for itme in self.__data:
    9. hash(itme)
    10. if itme not in temp:
    11. temp.append(itme)
    12. self.__data = temp
    13. def __del__(self):
    14. del self.__data
    15. def add(self, value):
    16. hash(value)
    17. if value not in self.__data:
    18. self.__data.append(value)
    19. else:
    20. print('已存在,忽略操作')
    21. def remove(self, value):
    22. if value in self.__data:
    23. self.__data.remove(value)
    24. print('删除成功')
    25. else:
    26. print('元素不存在,操作忽略')
    27. def pop(self):
    28. if not self.__data:
    29. print('集合已空,弹出操作忽略')
    30. return
    31. import random
    32. item = random.choice(self.__data)
    33. self.__data.remove(item)
    34. return item
    35. def __sub__(self, anotherSet):
    36. if not isinstance(anotherSet, Set):
    37. raise Exception('类型错误')
    38. result = Set()
    39. for item in self.__data:
    40. if item not in anotherSet.__data:
    41. result.__data.append(item)
    42. return result
    43. def difference(self, anotherSet):
    44. return self - anotherSet
    45. def __or__(self, anotherSet):
    46. if not isinstance(anotherSet, Set):
    47. raise Exception('类型错误')
    48. result = Set(self.__data)
    49. for item in anotherSet.__data:
    50. if item not in result.__data:
    51. result.__data.append(item)
    52. return result
    53. def union(self, anotherSet):
    54. return self | anotherSet
    55. def __and__(self, anotherSet):
    56. if not isinstance(anotherSet, Set):
    57. raise Exception('类型错误')
    58. result = Set()
    59. for item in self.__data:
    60. result.__data.append(item)
    61. return result
    62. def __xor__(self, other):
    63. return (self - other) | (other - self)
    64. def symetric_difference(self, anotherSet):
    65. return self ^ anotherSet
    66. def __eq__(self, other):
    67. if not isinstance(other, Set):
    68. raise Exception('类型错误')
    69. return sorted(self.__data) == sorted(other.__data)
    70. def __gt__(self, other):
    71. if not isinstance(other, Set):
    72. raise Exception('类型错误')
    73. if self != other:
    74. flag1 = True
    75. for item in self.__data:
    76. if item not in other.__data:
    77. flag1 = False
    78. break
    79. flag2 = True
    80. for item in other.__data:
    81. if item not in self.__data:
    82. if item not in self.__data:
    83. flag2 = False
    84. break
    85. if not flag1 and flag2:
    86. return True
    87. return False
    88. def __ge__(self, anotherSet):
    89. if not isinstance(anotherSet, Set):
    90. raise Exception('类型错误')
    91. return self == anotherSet or self > anotherSet
    92. def issubset(self, anotherSet):
    93. return self < anotherSet
    94. def clear(self):
    95. while self.__data:
    96. del self.__data[-1]
    97. print('集合已清空')
    98. def __iter__(self):
    99. return iter(self.__data)
    100. def __contains__(self, item):
    101. return item in self.__data
    102. def __len__(self):
    103. return len(self.__data)
    104. def __repr__(self):
    105. return '{' + str(self.__data)[1:-1] + '}'
    106. __str__ = __repr__

    学习内容:编写自定义类,模拟双端队列

            编写自定义类,模拟双端队列。

    实验代码:

    1. class li_Deque():
    2. def __init__(self, iterable=None,maxlen=0):
    3. if iterable == None:
    4. self.__content = []
    5. self.__current = 0
    6. else:
    7. self.__content = list(iterable)
    8. self.__current = len(iterable)
    9. self.__size = maxlen
    10. if self.__size < self.__current:
    11. self.__size = self.__current
    12. def __del__(self):
    13. del self.__current
    14. def setSize(self, size):
    15. if size < self.__current:
    16. for i in range(size, self.__current)[::-1]:
    17. del self.__current[i]
    18. self.__current = size
    19. self.__size = size
    20. def appendRight(self, v):
    21. if self.__current < self.__size:
    22. self.__content.append(v)
    23. self.__current = self.__current + 1
    24. else:
    25. print('full')
    26. def appendLeft(self, v):
    27. if self.__content < self.__size:
    28. self.__content.insert(0, v)
    29. self.__current = self.__current + 1
    30. else:
    31. print('full')
    32. def popLeft(self):
    33. if self.__content:
    34. self.__current = self.__current + 1
    35. return self.__content.pop(0)
    36. else:
    37. print('empty')
    38. def popRight(self):
    39. if self.__content:
    40. self.__current = self.__current - 1
    41. return self.__content.pop()
    42. else:
    43. print('empty')
    44. def rotate(self, k):
    45. if abs(k) > self.__current:
    46. print('k must <=' + str(self.__current))
    47. return
    48. self.__content = self.__content[-k:] + self.__content[:-k]
    49. def reverse(self):
    50. self.__content = self.__content[::-1]
    51. def __len__(self):
    52. return self.__current
    53. def __str__(self):
    54. return 'Lihubing('+str(self.__content)+',maxlen='+str(self.__size)+')'
    55. __repr__ = __str__
    56. def clear(self):
    57. self.__content = []
    58. self.__current = 0
    59. def isEmpty(self):
    60. return not self.__content
    61. def isFull(self):
    62. return self.__current == self.__size
    63. if __name__ == '__main__':
    64. print('Please use as a module')

    学习内容:设计并实现一个数组类,要求能够把包含数字的列表、元组或range 对象转换为数组,并能够修改数组中指定位置上的元素值。

            设计并实现一个数组类,要求能够把包含数字的列表、元组或range 对象转换为数组,并能够修改数组中指定位置上的元素值。

    实验代码:

    实验结果:


    学习内容:设计并实现一个数组类,要求能够把包含数字的列表、元组或range 对象转换为数组,能够使用包含整数的列表作为下标同时返回多个位置上的元素值。

            设计并实现一个数组类,要求能够把包含数字的列表、元组或range 对象转换为数组,能够使用包含整数的列表作为下标同时返回多个位置上的元素值。

     实验代码:

     实验结果:


    学习内容:编写函数,接收一个字符串,返回其中最长的数字子串。

            编写函数,接收一个字符串,返回其中最长的数字子串。

     实验代码:

     实验结果:


    学习内容:编写函数,接收一句英文,把其中的单词倒置,标点符号不倒置,例如 I like Beijing.经过函数后变为:Beijing. like I

            编写函数,接收一句英文,把其中的单词倒置,标点符号不倒置,例如 I like Beijing.经过函数后变为:Beijing. like I

     实验代码:

      实验结果:


    学习内容:编写函数,接收一个字符串,返回其中每个字符的最后一次出现,并按每个字符最后一次出现的先后顺序依次存入列表。例如,对于字符串'abcda'的处理结果为['b', 'c', 'd', 'a'],而字符串'abcbda'的处理结果为['c', 'b', 'd', 'a']。

            编写函数,接收一个字符串,返回其中每个字符的最后一次出现,并按每个字符最后一次出现的先后顺序依次存入列表。例如,对于字符串'abcda'的处理结果为['b', 'c', 'd', 'a'],而字符串'abcbda'的处理结果为['c', 'b', 'd', 'a']。

     实验代码:

       实验结果:

     

  • 相关阅读:
    docker容器
    Vue3中如何使用ref获取元素节点全面解析
    canal实现mysql数据同步
    程序员该选择Macbook Pro吗?
    自动化RPA开发 --获取所有窗口信息和进程信息
    Java读取寄存器数据的方法
    移动测试之语音识别功能如何测试?
    AutoJs学习-实现区域截图+文字识别+摇一摇截图+截图查看器
    mediakit 源码 轻微微 学习总结
    【网页设计】基于HTML+CSS+JavaScript制作美食网站舌尖上的美食
  • 原文地址:https://blog.csdn.net/oxygen23333/article/details/128089555