1. Define a function in python that returns the sum of the following 4 lists. Remember to store and re- use your code instead of summing the values for each list 4 times. Hence, define a function that takes as an argument a list and returns the sum of values.
Copy the following lists and paste them in a Python file after you define the sum_of_values function
Call the function and pass to it a different list each call as an argument
list1 = [5, 7, 4, 1, 8, 9, 56, 34, 65, 100]
list2 = [1, 2, 5, 6, 8, 6, 0, 19, 8, 10, 11, 12]
list3 = [1.1, 1.6, 7.8, 9.4, 6.7, 8.9, 99.7, 65.6, 45.5, 54.5,
12.7] list4 = [10.5, 100.5, 20.5]
Here is code:
def sum_of_values(list):
sum = 0
for val in list:
sum = sum + val
return round(sum,1) # round value
list1 = [5, 7, 4, 1, 8, 9, 56, 34, 65, 100]
list2 = [1, 2, 5, 6, 8, 6, 0, 19, 8, 10, 11, 12]
list3 = [1.1, 1.6, 7.8, 9.4, 6.7, 8.9, 99.7, 65.6, 45.5, 54.5,
12.7]
list4 = [10.5, 100.5, 20.5]
print("The sum of list1 is : ", sum_of_values(list1))
print("The sum of list2 is : ", sum_of_values(list2))
print("The sum of list3 is : ", sum_of_values(list3))
print("The sum of list4 is : ", sum_of_values(list4))
Output:
1. Define a function in python that returns the sum of the following 4 lists. Remember...
in PYTHON! Define a function count() that compute and returns the sum of the first 100 even numbers.
Define a Python function that takes a list and returns the length of the list using map and sum.
1. Write a function called ordinal_sum that accepts a string argument and returns the sum of the ordinal values of each character in the string. The ordinal value of a character is its numeric Unicode code point in decimal. 2. Write code that creates a Python set containing each unique character in a string named my_string. For example, if the string is 'hello', the set would be {'h', 'e', 'l', 'o'} (in any order). Assign the set to a variable...
PYTHON Define a function reverseNestedList(...) which receives one possibly nested list (lst1) and returns the reverse of both the given list and any nested lists inside it. Note that the testing inputs will not include lists that are nested more than twice (a list in a list in a list ++...) [[[...],1], 1] Good luck! As an example, the following code fragment: lst1 = [[1, 2], [3, 4]] print(reverseNestedList(lst1)) should produce the output: [[4, 3], [2, 1]]
package homework; import java.util.Arrays; import stdlib.*; /** CSC300Homework4 version 1.0 * * * * Find the 3 Sections marked TODO: * * TODO #1: SumOddsRecursive * TODO #2: ReverseArray * TODO #3: MergeArrays * * You must not add static variables. You MAY add static functions. * * It is okay to add functions, such as * * <pre> * public static double sumOfOddsHelper (double[] list, int i) { * </pre> * * but it is NOT okay to...
Python problem. 3. (6 pts) Define the following four sorting functions: each takes an argument that is a list of int or str or both values (otherwise raise an AssertionError exception with an appropriate error message) that will be sorted in a different way. Your function bodies must contain exactly one assert statement followed by one return statement. In parts a-c, create no other extra/temporary lists other than the ones returned by calling sorted. a. (2 pts) Define the mixed...
Python: Arrays and Lists (do code on repl.it) Part C (4 points) - more advanced lists You will need a Python repl to solve part C. In Python, define a function named secondHighest which accepts a list of numbers, and returns the second highest number from the list. If the highest value occurs more than once, then it is also the second highest (see example). Assume the input list will always have length at least two (so there is always...
ANSWER USING PYTHON Write a recursive function that takes 2 sorted lists as arguments and returns a single, merged sorted list as a result. For example, if the two input lists are [0, 2, 4, 6, 8] and [1, 3, 5, 7, 9], the returned result should be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]. Remember that you can add lists in Python using the + operator ([0] + [1,2,3] = [0,1,2,3]) and can take slices of...
use python 2a. Union (1 points) Make a union function which takes in 2 lists as parameters and returns a list. It should return a list that contain all elements that are in either list. Your results list should contain no duplicates. For example, if the two lists are [1,5,6,5, 2] and [3,5, 1,9] then the result list is (1,5,6,2,3,9). Note that the list sizes don't have to be equal. Note that the input lists could have duplicates, but your...
in python A. Define a function called contains_only_integers() that takes a tuple, returns True if all the items in the tuple are integers(2), and returns False otherwise. For example, contains_only_integers (3, 5, 17, 257, 65537) ), contains_only_integers( (-1,) ), and contains_only_integers( ) should all return True, but contains_only_integers (2.0,4.0)) and contains_only_integers (8, 4, "2", 1)) should both return false. Your function should use a while loop to do this calculation. 121 Hint: the is instance() built-in function provides the most...