设计三维向量类、实现向量的加法、减法以及向量与标量的乘法和除法运算
实验代码:
- class Vector_3D:
- def __init__(self, x, y, z):
- self.__x = x
- self.__y = y
- self.__z = z
-
- def add(self, other):
- x = self.__x + other.__x
- y = self.__y + other.__y
- z = self.__z + other.__z
- return Vector_3D(x, y, z)
-
- def Minus(self, other):
- x = self.__x - other.__x
- y = self.__y - other.__y
- z = self.__z - other.__z
- return Vector_3D(x, y, z)
-
- def Multiply(self, a):
- x = self.__x * a
- y = self.__y * a
- z = self.__z * a
- return Vector_3D(x, y, z)
-
- def Except(self, a):
- x = self.__x / a
- y = self.__y / a
- z = self.__z / a
- return Vector_3D(x, y, z)
-
- def show(self):
- print('X:{0}, Y:{1}, Z:{2}'.format(self.__x,self.__y,self.__z))
-
- def lenght(self):
- return (self.__x ** 2 + self.__y ** 2 + self.__z ** 2) ** 0.5
-
-
- b = Vector_3D(1, 2, 3)
- b1 = b.Multiply(4)
- b1.show()
-
- b2 = b1.add(b)
- b2.show()
- print(b2.lenght())
编写自定义类,模拟内置集
实验代码:
- class Set(object):
- def __init__(self, data=None):
- if data is None:
- self.__data = []
- else:
- raise Exception('必须是可迭代的数据')
- temp = []
- for itme in self.__data:
- hash(itme)
- if itme not in temp:
- temp.append(itme)
- self.__data = temp
-
- def __del__(self):
- del self.__data
-
- def add(self, value):
- hash(value)
- if value not in self.__data:
- self.__data.append(value)
- else:
- print('已存在,忽略操作')
-
- def remove(self, value):
- if value in self.__data:
- self.__data.remove(value)
- print('删除成功')
- else:
- print('元素不存在,操作忽略')
-
- def pop(self):
- if not self.__data:
- print('集合已空,弹出操作忽略')
- return
- import random
- item = random.choice(self.__data)
- self.__data.remove(item)
- return item
-
- def __sub__(self, anotherSet):
- if not isinstance(anotherSet, Set):
- raise Exception('类型错误')
- result = Set()
- for item in self.__data:
- if item not in anotherSet.__data:
- result.__data.append(item)
- return result
-
- def difference(self, anotherSet):
- return self - anotherSet
-
- def __or__(self, anotherSet):
- if not isinstance(anotherSet, Set):
- raise Exception('类型错误')
- result = Set(self.__data)
- for item in anotherSet.__data:
- if item not in result.__data:
- result.__data.append(item)
- return result
-
- def union(self, anotherSet):
- return self | anotherSet
-
- def __and__(self, anotherSet):
- if not isinstance(anotherSet, Set):
- raise Exception('类型错误')
- result = Set()
- for item in self.__data:
- result.__data.append(item)
- return result
-
- def __xor__(self, other):
- return (self - other) | (other - self)
-
- def symetric_difference(self, anotherSet):
- return self ^ anotherSet
-
- def __eq__(self, other):
- if not isinstance(other, Set):
- raise Exception('类型错误')
- return sorted(self.__data) == sorted(other.__data)
-
- def __gt__(self, other):
- if not isinstance(other, Set):
- raise Exception('类型错误')
- if self != other:
- flag1 = True
- for item in self.__data:
- if item not in other.__data:
- flag1 = False
- break
- flag2 = True
- for item in other.__data:
- if item not in self.__data:
- if item not in self.__data:
- flag2 = False
- break
- if not flag1 and flag2:
- return True
- return False
-
-
- def __ge__(self, anotherSet):
- if not isinstance(anotherSet, Set):
- raise Exception('类型错误')
- return self == anotherSet or self > anotherSet
-
-
- def issubset(self, anotherSet):
- return self < anotherSet
-
-
- def clear(self):
- while self.__data:
- del self.__data[-1]
- print('集合已清空')
-
-
- def __iter__(self):
- return iter(self.__data)
-
-
- def __contains__(self, item):
- return item in self.__data
-
-
- def __len__(self):
- return len(self.__data)
-
-
- def __repr__(self):
- return '{' + str(self.__data)[1:-1] + '}'
-
-
- __str__ = __repr__
编写自定义类,模拟双端队列。
实验代码:
- class li_Deque():
- def __init__(self, iterable=None,maxlen=0):
- if iterable == None:
- self.__content = []
- self.__current = 0
- else:
- self.__content = list(iterable)
- self.__current = len(iterable)
- self.__size = maxlen
- if self.__size < self.__current:
- self.__size = self.__current
-
- def __del__(self):
- del self.__current
-
- def setSize(self, size):
- if size < self.__current:
- for i in range(size, self.__current)[::-1]:
- del self.__current[i]
- self.__current = size
- self.__size = size
-
- def appendRight(self, v):
- if self.__current < self.__size:
- self.__content.append(v)
- self.__current = self.__current + 1
- else:
- print('full')
-
- def appendLeft(self, v):
- if self.__content < self.__size:
- self.__content.insert(0, v)
- self.__current = self.__current + 1
- else:
- print('full')
-
- def popLeft(self):
- if self.__content:
- self.__current = self.__current + 1
- return self.__content.pop(0)
- else:
- print('empty')
-
- def popRight(self):
- if self.__content:
- self.__current = self.__current - 1
- return self.__content.pop()
- else:
- print('empty')
-
- def rotate(self, k):
- if abs(k) > self.__current:
- print('k must <=' + str(self.__current))
- return
- self.__content = self.__content[-k:] + self.__content[:-k]
-
- def reverse(self):
- self.__content = self.__content[::-1]
-
- def __len__(self):
- return self.__current
-
- def __str__(self):
- return 'Lihubing('+str(self.__content)+',maxlen='+str(self.__size)+')'
-
- __repr__ = __str__
-
- def clear(self):
- self.__content = []
- self.__current = 0
-
- def isEmpty(self):
- return not self.__content
-
- def isFull(self):
- return self.__current == self.__size
-
- if __name__ == '__main__':
- print('Please use as a module')
设计并实现一个数组类,要求能够把包含数字的列表、元组或range 对象转换为数组,并能够修改数组中指定位置上的元素值。
实验代码:
实验结果:
设计并实现一个数组类,要求能够把包含数字的列表、元组或range 对象转换为数组,能够使用包含整数的列表作为下标同时返回多个位置上的元素值。
实验代码:
实验结果:
编写函数,接收一个字符串,返回其中最长的数字子串。
实验代码:
实验结果:
编写函数,接收一句英文,把其中的单词倒置,标点符号不倒置,例如 I like Beijing.经过函数后变为:Beijing. like I
实验代码:
实验结果:
编写函数,接收一个字符串,返回其中每个字符的最后一次出现,并按每个字符最后一次出现的先后顺序依次存入列表。例如,对于字符串'abcda'的处理结果为['b', 'c', 'd', 'a'],而字符串'abcbda'的处理结果为['c', 'b', 'd', 'a']。
实验代码:
实验结果: