报错
class SqList1:
def __init__(self):
self.initcapacity = 10
self.capacity = self.initcapacity
self.data = [None] * self.capacity
self.size = 0
def Add(self, e):
if self.size == self.capacity:
self.resize(2 * self.size)
self.data[self.size] = e
self.size += 1
def CreateList(self, a):
for i in range(len(a)):
if self.size == self.capacity:
self.resize(2 * self.size)
self.data[self.size] = a[i]
self.size += 1
print(f"您输入的数组为{self.data[0:self.size]}")
def getsize(self):
return self.size
def traverse(self): # 遍历顺序表
for item in self.data:
print(item, end=' ')
print('\n')
def resize(self, newcapacity):
assert newcapacity >= 0
olddata = self.data
self.data = [None] * newcapacity
self.capacity = newcapacity
for i in range(self.size):
self.data[i] = olddata[i]
from SqList1 import SqList1
def count_x(A, x):
if A.getsize() == 0:
return 0
else:
result = count_x(A[:-1], x)
if A[-1] == x:
result += 1
return result
if __name__ == '__main__':
A = SqList1()
a=[1,2,34,5,5,2,3,5,7,5]
A.CreateList(a)
A.traverse()
print(count_x(A, 5))
该回答通过自己思路及引用到GPTᴼᴾᴱᴺᴬᴵ搜索,得到内容具体如下:
在这段代码中,有一个问题是在 count_x
函数中,递归调用时使用了 SqList1 类型的对象 A,而不是 A.data。因为 A 是 SqList1 类型的对象,而 A[:-1] 是将 SqList1 类型的对象切片后得到的新的 SqList1 类型的对象,不能直接进行递归调用。
改正的方法是将递归调用的参数改为 A.data[:-1],即将 SqList1 对象中的 data 列表切片后进行递归调用。同时,在计算最后一个元素是否等于 x 时,应该使用 A.data[-1] 而不是 A[-1]。
修改后的代码如下:
class SqList1:
def __init__(self):
self.initcapacity = 10
self.capacity = self.initcapacity
self.data = [None] * self.capacity
self.size = 0
def Add(self, e):
if self.size == self.capacity:
self.resize(2 * self.size)
self.data[self.size] = e
self.size += 1
def CreateList(self, a):
for i in range(len(a)):
if self.size == self.capacity:
self.resize(2 * self.size)
self.data[self.size] = a[i]
self.size += 1
print(f"您输入的数组为{self.data[0:self.size]}")
def getsize(self):
return self.size
def traverse(self): # 遍历顺序表
for item in self.data:
print(item, end=' ')
print('\n')
def resize(self, newcapacity):
assert newcapacity >= 0
olddata = self.data
self.data = [None] * newcapacity
self.capacity = newcapacity
for i in range(self.size):
self.data[i] = olddata[i]
def count_x(A, x):
if len(A) == 0:
return 0
else:
result = count_x(A.data[:-1], x)
if A.data[-1] == x:
result += 1
return result
if __name__ == '__main__':
A = SqList1()
a = [1, 2, 34, 5, 5, 2, 3, 5, 7, 5]
A.CreateList(a)
A.traverse()
print(count_x(A, 5))
运行结果为:
您输入的数组为[1, 2, 34, 5, 5, 2, 3, 5, 7, 5]
1 2 34 5 5 2 3 5 7 5
4
如果以上回答对您有所帮助,点击一下采纳该答案~谢谢
一个小孩买了价值少于1美元的糖,并将1美元的钱交给售货员。售货员希望用数目最少的硬币找给小孩。假设提供了数目不限的面值为2 5美分、1 0美分、5美分、及1美分的硬币,写一个算法让售货员用最少的硬币数找给小孩。
# pay
lollipop = 0.37
money = 1
# coin
change1 = Decimal('0.01')
change5 = Decimal('0.05')
change10 = Decimal('0.1')
change25 = Decimal('0.25')
change = Decimal(str(money)) - Decimal(str(lollipop))
# initialize
n1, n5, n10, n25 = 0, 0, 0, 0
# if change < 0.05
if Decimal('0') < change < change5:
n1 = change / change1
# if change < 0.1
elif change < change10:
n5 = int(change / change5)
n1 = int((change - change5 * n5) / change1)
# if change < 0.25
elif change < change25:
n10 = int(change / change10)
n5 = int((change - change10 * n10) / change5)
n1 = int((change - change10 * n10 - change5 * n5) / change1)
# if change < 1
elif change < Decimal('1'):
n25 = int(change / change25)
n10 = int((change - change25 * n25) / change10)
n5 = int((change - change25 * n25 - change10 * n10) / change5)
n1 = int((change - change25 * n25 - change10 * n10 - change5 * n5) / change1)
print(f"$0.25: {n25}\n$0.1: {n10}\n$0.05: {n5}\n$0.01: {n1}")
根据该段代码提供的信息,无法确定具体的报错信息和原因。需要提供更多的上下文和错误信息才能作出具体的解决方案。建议提供完整的代码和报错信息,或者说明如何重现该报错以及出现该报错的具体场景,以便帮助解决该问题。