एक सरणी को देखते हुए और हमें फ़ंक्शन का उपयोग करके उपसर्ग योग सरणी करना है। इसलिए उन्हें केवल उन फ़ंक्शंस या लूप्स द्वारा एक्सेस किया जाना चाहिए जो स्ट्रीम को छोटा करते हैं। एक पुनरावर्तक बनाएं जो संचित रकम लौटाए। तत्व दशमलव या अंश सहित कोई भी जोड़ने योग्य प्रकार हो सकते हैं। यदि वैकल्पिक फ़ंक्शन तर्क की आपूर्ति की जाती है, तो यह दो तर्कों का एक फ़ंक्शन होना चाहिए और इसका उपयोग जोड़ के बजाय किया जाएगा।
उदाहरण
Input: Data = [1, 0, 2, 3, 5] >>> list(accumulate(data)) # running summation Output: [1, 1, 3, 6, 11]
एल्गोरिदम
Step 1: Create list. Step 2: Use list(accumulate( ))) function, its return running total. Step 3: Display total.
उदाहरण कोड
# Python program to print prefix
#sum array using accumulate function
from itertools import accumulate
def summation(A):
print ("The List after Summation ::>", list(accumulate(A)))
# Driver program
if __name__ == "__main__":
A=list()
n=int(input("Enter the size of the First List ::"))
print("Enter the Element of First List ::")
for i in range(int(n)):
k=int(input(""))
A.append(k)
summation(A)
आउटपुट
Enter the size of the First List ::5 Enter the Element of First List :: 1 2 3 4 5 The List after Summation ::> [1, 3, 6, 10, 15]