python数据结构第五章课后习题答案这⾥写⽬录标题
1
import sys
data =list()
n =100
for k in range(n):
a =len(data)
b = sizeof(data)
print("length :{0} Size in bytes: {1}".format(a,b))
data.append(None)
2
import sys
data =list()
n =100
size =63
for k in range(n):
a =len(data)
b = sizeof(data)
if size != b:
print((b -64)//8)
data.append(None)
size = b
else:
print("length :{0} Size in bytes: {1}".format(a, b))
data.append(None)
3
import sys
data =[None for i in range(100)]
for k in range(100):
a =len(data)
b = sizeof(data)
print("length :{0} Size in bytes: {1}".format(a,b))
data.pop(-1)
4
import ctypes # provides low-level arrays
class DynamicArray:
"""A dynamic array class akin to a simplified Python list."""
def__init__(self):
"""Create an empty array."""
self._n =0# count actual elements
self._capacity =1# default array capacity
self._A = self._make_array(self._capacity)# low-level array
def__len__(self):
"""Return number of elements stored in the array."""
return self._n
def__getitem__(self, item):
"""Return element at index k."""
if0<= item < self._n:
return self._A[item]
elif-1* self._n < item <0:
return self._A[self._n + item]
else:
raise IndexError("invalid index")
def append(self, obj):
"""Add object to end of the array."""
if self._n == self._capacity:# not enough room
self._resize(2* self._capacity)# so double capacity
self._A[self._n]= obj
self._n +=1
def_resize(self, c):# nonpublic utitity
"""Resize internal array to capacity c."""
B = self._make_array(c)# new (bigger) array
for k in range(self._n):
B[k]= self._A[k]
self._A = B
self._capacity = c
@staticmethod
def_make_array(c):
"""Return new array with capacity c."""
return(c * ctypes.py_object())()# see ctypes documentation
6
import ctypes # provides low-level arrays
def_make_array(c):
"""Return new array with capacity c."""
return(c * ctypes.py_object)()# see ctypes documentation
class DynamicArray:
"""A dynamic array class akin to a simplified Python list."""
def__init__(self):
"""Create an empty array."""
self._n =0# count actual elements
self._capacity =1# default array capacity
self._A = _make_array(self._capacity)# low-level array
def__len__(self):
"""Return number of elements stored in the array."""
return self._n
def__getitem__(self, item):
"""Return element at index k."""
if0<= item < self._n:
return self._A[item]# retrieve from array
elif-1* self._n < item <0:
return self._A[self._n + item]
else:
raise IndexError("invalid index")
def append(self, obj):
"""Add object to end of the array."""
if self._n == self._capacity:# not enough room
self._resize(2* self._capacity)# so double capacity
self._A[self._n]= obj
self._n +=1
def_resize(self, c):# nonpublic utility
"""Resize internal array to capacity c."""
B = _make_array(c)# new (bigger) array
for k in range(self._n):
B[k]= self._A[k]
self._A = B
self._capacity = c
def insert(self, k, value):
"""Insert value index l,shifting subsequent values rightward."""
# (for simplicity,we assume 0 <= k <= n in this version)
if self._n == self._capacity:# not enough room
B = self._A[:k]
B.append(value)
for j in range(k, self._n):
B.append(self._A[j])
self._A = B
self._n +=1
else:
for j in range(self._n, k,-1):# shift rightmost first
self._A[j]= self._A[j -1]
self._A[k]= value # store newest element
self._n +=1
def remove(self, value):
"""Remove first occurrence of value (or raise ValueError)."""
# note we do not consider shrinking the dynamic array in this version for k in range(self._n):
if self._A[k]== value:# found a match!
for j in range(k, self._n -1):# shift others to fill gap
self._A[j]= self._A[j +1]
self._A[self._n -1]=None# help garbage collection
self._n -=1# we have one less item
return# exit immediately
raise ValueError("value not found")# only reached if no match
if __name__ =="__main__":
java软件小说阅读器
A = DynamicArray()
for i in range(10):
A.append(i)
turtle的中文A.insert(2,6)
for j in range(11):
print(A[j])
7
def find(arrayList,length):
return sum(arrayList)-sum(range(length))
if __name__ =="__main__":
array =[1,2,3,4,4,5,6]
print(find(array,7))
8
import time
import ctypes # provides low-level arrays
import copy
def_make_array(c):
"""Return new array with capacity c."""
return(c * ctypes.py_object)()# see ctypes documentation
class DynamicArray:
"""A dynamic array class akin to a simplified Python list."""
def__init__(self):
"""Create an empty array."""
self._n =0# count actual elements
self._capacity =1# default array capacity
self._A = _make_array(self._capacity)# low-level array
def__len__(self):
免费游戏源码网站推荐"""Return number of elements stored in the array."""
return self._n
各国language介绍def__getitem__(self, item):
"""Return element at index k."""
if0<= item < self._n:
return self._A[item]# retrieve from array
elif-1* self._n <= item <0:
return self._A[self._n + item]
else:
raise IndexError("invalid index")
def append(self, obj):
"""Add object to end of the array."""
if self._n == self._capacity:# not enough room
self._resize(2* self._capacity)# so double capacity
self._A[self._n]= obj
self._n +=1
def_resize(self, c):# nonpublic utility
"""Resize internal array to capacity c."""
B = _make_array(c)# new (bigger) array
for k in range(self._n):
B[k]= self._A[k]
self._A = B
self._capacity = c
def insert(self, k, value):
"""Insert value index l,shifting subsequent values rightward."""
# (for simplicity,we assume 0 <= k <= n in this version)
if self._n == self._capacity:# not enough room
python编程基础教程课后答案B = self._A[:k]
B = self._A[:k]
B.append(value)
for j in range(k, self._n):
B.append(self._A[j])
self._A = B
self._n +=1
xmlschemaelse:
for j in range(self._n, k,-1):# shift rightmost first
self._A[j]= self._A[j -1]
self._A[k]= value # store newest element
self._n +=1
def remove(self, value):
"""Remove first occurrence of value (or raise ValueError)."""
# note we do not consider shrinking the dynamic array in this version for k in range(self._n):
if self._A[k]== value:# found a match!
for j in range(k, self._n -1):# shift others to fill gap
self._A[j]= self._A[j +1]
self._A[self._n -1]=None# help garbage collection
self._n -=1# we have one less item
return# exit immediately
raise ValueError("value not found")# only reached if no match
def pop(self, index=-1):
if0<= index < self._n:
for i in range(index, self._n -1):
self._A[i]= self._A[i +1]
self._A[self._n -1]=None
self._n -=1
return
elif-1* self._n <= index <0:
for i in range(self._n + index, self._n-1):
self._A[i]= self._A[i +1]
self._A[self._n -1]=None
self._n -=1
return
else:
raise IndexError("invalid index")
if __name__ =="__main__":
N =10000
list1 = DynamicArray()
for i in range(N):
list1.append(i)
start = time.time()
while list1:
list1.pop(0)
end = time.time()
print("从头删花费的平均时间为{0}".format((end-start)/N))
list2 = DynamicArray()
for i in range(N):
list2.append(i)
start = time.time()
while list2:
list2.pop(len(list2)//2)
end = time.time()
print("从中间删花费的平均时间为{0}".format((end-start)/N))
list3 = DynamicArray()
for i in range(N):
list3.append(i)
start = time.time()
while list3:
list3.pop()
end = time.time()
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论