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
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 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.
Write a program in Python to print all possible combinations of phone numbers. The length of...
Write a program in Python to print all possible combinations of phone numbers. The length of the number will be given. Also 3 digits will be given, which can not be used. No two consecutive digits can be same. A number containing 4 would always have 4 in the beginning.
Given a list x = [2,3,5,6,7,8,9], write a python program that find those numbers which are...
Given a list x = [2,3,5,6,7,8,9], write a python program that find those numbers which are divisible by 3. Arrange these numbers in a list called “result”. Hint: similar to page 9 in the slides.
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: [ ]...
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
Given n. Write a program in PYTHON to Generate all numbers with number of digits equal...
Given n. Write a program in PYTHON to Generate all numbers with number of digits equal to n, such that the digit to the right is greater than the left digit (ai+1 > ai). E.g. if n=3 (123,124,125,……129,234,…..789)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT