Python Recursion question 1

Write a Python program to calculate the sum of a list of numbers.
def list_sum(num_List): | |
if len(num_List) == 1: | |
return num_List[0] | |
else: | |
return num_List[0] + list_sum(num_List[1:]) | |
| |
print(list_sum([2, 4, 5, 6, 7])) |
Search for a command to run...

Write a Python program to calculate the sum of a list of numbers.
def list_sum(num_List): | |
if len(num_List) == 1: | |
return num_List[0] | |
else: | |
return num_List[0] + list_sum(num_List[1:]) | |
| |
print(list_sum([2, 4, 5, 6, 7])) |
No comments yet. Be the first to comment.
string copy , compare , concat question . #include<iostream>#include<cstring>using namespace std; int main() { char a[1000] = "apple"; char b[1000]; // calc length cout << strlen(a) << endl; // strcpy strcpy(b,a); cout << b...
Find Displacement : Given a long routes containing N,S,E,W directions, Find the shortest path to reach out the location. sample input :SNNNEWE Sample output : NNE solution : ------------- #include<iostream> using namespace std; int mai...