Question

In: Computer Science

Write a Python program that calls a function to sum all the numbers in a list...

Write a Python program that calls a function to sum all the numbers in a list and returns the result to the caller. The main program creates a list (with hard-coded or user input) and passes the list as an argument to the function. You may not use the built-in function, sum.

The program calls a second function to multiply all the numbers in a list passed to it by main and returns the product back to the caller.

List can be created with user input or hard-coded elements.

The program calls a third function that takes the list of words (below) passed to it from main and determines which elements are palindromes without changing the original list.

['racecar', 'Python', 'mom', 'java','level', 'DNA','101101' ]

If true, the function appends the word to a new list and returns it to the caller. The main program prints the list of palindromes returned from the function. The function should not modify the original list passed to it from the main (no side effect).

A palindrome is a word, phrase, number or sequence of words that reads the same backward as forward

Solutions

Expert Solution

"""
Python version : 3.6
Python program to create and test functions
"""

def sum_list(lst):
   """
   function to calculate and return sum of all the numbers in the given list, lst
   """
   # initialize result to 0
   result = 0
   # loop over the list elements adding the elements to result
   for num in lst:
       result += num
      
   return result

def product_list(lst):
   """
   function to calculate and return the product of all numebrs in the given list, lst
   """
   # initialize result to 1
   result = 1
   # loop over the list elements multiplying the elements to result
   for num in lst:
       result *= num
      
   return result

def palindromes_list(lst):
   """
   function that accepts a list of words as input and returns a list of all the strings
   that are palindrome without changing the input list
   """  
   # create an empty list
   palindrome_list = []
  
   # loop over input list
   for s in lst:
       # if reverse of s = s, then it is a palindrome, hence append s to palindrome_list
       if s == s[-1::-1]:
           palindrome_list.append(s)
          
   return palindrome_list

# test the functions

numbers = [12, 8, 10, 6, 7.3, 22]
print(sum_list(numbers))  
print(product_list(numbers))
words = ['racecar', 'Python', 'mom', 'java','level', 'DNA','101101' ]
print(palindromes_list(words))  

#end of program

Code Screenshot:

Output:


Related Solutions

Write a python program to sum the prime numbers existing in an array . For instance...
Write a python program to sum the prime numbers existing in an array . For instance , if A = [4, 7, 12, 3, 9] the output should be 10
Write a function in Python to compute the sample mean of a list of numbers. The...
Write a function in Python to compute the sample mean of a list of numbers. The return value should be the sample mean and the input value should be the list of numbers. Do not use a built-in function for the mean. Show an example of your function in use.    Thank You
Write a Python program that print out the list of couples of prime numbers that are...
Write a Python program that print out the list of couples of prime numbers that are less than 50, but their sum is bigger than 40. For instance(29,13)or(37,17),etc. Your program should print all couples
Write a function which receives a list and returns a number. In the list, all numbers...
Write a function which receives a list and returns a number. In the list, all numbers have been repeated twice except one number that is repeated once. The function should return the number that is repeated once and return it.write a python program for this question. use main function.
Using the Python program: a/ Write a program that adds all numbers from 1 to 100...
Using the Python program: a/ Write a program that adds all numbers from 1 to 100 to a list (in order). Then remove the multiples of 3 from the list. Print the remaining values. b/ Write a program that initializes a list with ten random integers from 1 to 100. Then remove the multiples of 3 and 5 from the list. Print the remaining values. Please show your work and explain. Thanks
USING PYTHON Write a program to create a number list. It will call a function to...
USING PYTHON Write a program to create a number list. It will call a function to calculate the average values in the list. Define main ():                        Declare variables and initialize them                        Create a list containing numbers (int/float)                        Call get_avg function that will return the calculated average values in the list.                                       Use a for loop to loop through the values in the list and calculate avg                        End main()
Using LIST and FUNCTION Write a program in Python that asks for the names of three...
Using LIST and FUNCTION Write a program in Python that asks for the names of three runners and the time it took each of them to finish a race. The program should display who came in first, second, and third place.
Write a function that takes two integer inputs and returns the sum of all even numbers...
Write a function that takes two integer inputs and returns the sum of all even numbers between these inputs, and another function that takes two integer inputs and returns the sum of odd numbers between these inputs .In main function, the program will asks the user to enter two integer numbers and then passes them to these two functions and display the result of each of them
Write a MIPS Assembly program that computes the sum of all the odd numbers from 1...
Write a MIPS Assembly program that computes the sum of all the odd numbers from 1 ..99 and print out the answer.
In python, write a function, called ThreeSum, that accepts a list of non-negative numbers as input,...
In python, write a function, called ThreeSum, that accepts a list of non-negative numbers as input, and returns the highest sum of three neighboring elements in it. Write a main method that initializes the following five lists, gets the ThreeSum result for all of them using the above function, and prints the result to the screen. Example of the output: List 1: [4,5,4,5] , Three sum = 14 List 2: [7] , Three sum = 7 List 3: [ ]...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT