【问题标题】:how to get the sum of all the elements in here?如何在这里获得所有元素的总和?
【发布时间】:2020-08-17 14:03:09
【问题描述】:
def isempty(qu):
    if qu==[]:
        return True
    else:
        return False

def push(qu,item):
    qu.append(item)
    if len(qu)==1:
        front=rear=0
    else:
        rear=len(qu)-1

def pop(qu):
    if isempty(qu):
        return "underflow"
    else:
        item=qu.pop()[0]
        if len(qu)==0:
            front=rear=0
        return item

def peek(qu):
    if isempty(qu):
        return "underflow"
    else:
        front=0
    return qu[front]

def display(qu):
    if isempty(qu):
        print("Queue Empty")
    else:
        front=0
        rear=len(qu)-1
        print(qu[front],"<--- front")
        for a in range(1,rear):
            print(qu[a])
        print(qu[rear],"<--- rear")
def summation():        
    def sum_arr(arr,size):
       if (size == 0):
         return 0
       else:
         return arr[size-1] + sum_arr(arr,size-1)
    n=int(input("Enter number of elements in the queue for addition="))
    queue=[]
    for i in range(0,n):
        queue.append(item)
    print("The list is:")
    print(queue)
    print("sum of items :")
    b=sum_arr(queue,n)
    print(b)

queue=[]
for i in range(0,4):
    print("----Queue Operations----")
    print("1.Push/Enqueue")
    print("2.Pop/Dequeue")
    print("3.Peek")
    print("4.Display Queue")
    print("5.Exit")
    print("6.Summation")
    ch=int(input("Enter your choice="))
    if ch==1:
        item=int(input("Enter element="))
        push(queue,item)
        
    elif ch==2:
        item=pop(queue)
        if item=="underflow":
            print("Queue Empty")
            
        else :
            print("Popped item=",item)
           
    elif ch==3:
        item=peek(queue)
        if item=="underflow":
            print("Queue Empty")
            
        else:
            print("Front most=",item)
           
    elif ch==4:
        display(queue)
        
    elif ch==5:
        break
    elif ch==6:
        summation()
        
    else:
        print("Invalid Choice")

这是我们在这里使用的全部代码......

这实际上是为了使用列表实现队列

这里只能使用queues方法

Enter element=4
Enter element=5
Enter element=6
Enter the number of elements in the queue for addition=3
The list is:
[6, 6, 6]
the sum of items :
18

这里的错误是给定的元素是 4,5,6 但只有最后一个元素 '6' 用于求和三次...

我希望我回答了 cmets 中提出的所有问题.....

任何有一点解释的想法都非常感谢....

【问题讨论】:

  • python 有sum?
  • 是的,我们试过了,但它仍然不能在队列中工作......它给出了同样的错误@MZ
  • 你的队列只是一个列表,所以我看不出它是如何工作的,但还有什么错误?
  • queue.append(item) 每次循环都附加相同的变量item
  • 是的,根据实际发布的代码,甚至不清楚item 的来源。

标签: python list queue


【解决方案1】:

Python 之禅:简单胜于复杂

无论您构建什么自定义函数,它都不会比内置函数更快。
l = [6,6,6]
total = sum(l)
print('The list is: {}'.format(l)
print('the sum of items : {}'.format(total))

如果您仍然需要自定义函数

def arr_sum(arr):
    holder = 0
    if len(arr) == 0:
        return 0
    else:
        holder = [holder + i for i in arr if type(i) == int]
        return holder

【讨论】:

  • 我知道内置更好......但我应该在这里使用队列......所以任何带有代码的总和都是有帮助的......
  • 我在答案中添加了自定义函数
  • 是的,是的,我正在实现它们......非常感谢你在结合了两个部分(简单的和自定义的)之后它完美地工作了非常感谢你......@stackz
猜你喜欢
  • 2019-03-31
  • 2018-05-09
  • 1970-01-01
  • 1970-01-01
  • 2020-10-26
  • 2021-01-19
  • 2014-06-16
  • 2021-12-10
  • 1970-01-01
相关资源
最近更新 更多