Define a function called collapse() which takes
a list as input. Each element of the list will either be an
integer, or a list of integers. The function should modify the
input list by replacing all of the elements which themselves are
lists with the sum of their elements.
For example:
Test | Result |
---|---|
vals = [1, 2, 3, 4, 5] collapse(vals) print(vals) |
[1, 2, 3, 4, 5] |
vals = [1, [2, 3], 4, 5] collapse(vals) print(vals) |
[1, 5, 4, 5] |
and
Define a function called get_number that repeatedly asks the user to enter a number until they enter a number between the values of 1 and 10 inclusive, and should return that value as a floating point number. Your function should handle any kind of input.
For example:
Test | Input | Result |
---|---|---|
print(get_number()) |
hello 12 10 |
Enter a number: hello Enter a number: 12 Enter a number: 10 10.0 |
and
Define a function called pair_sum() which takes two inputs: a list of integers and a total.
The function should return a list of tuples, where each value in the tuple is a different value from the input list, and where the sum of the tuple elements equals the total, and where (i, j) is a tuple in the output list, then i should appear to the left of j in the input list.
In other words, the first element of the tuple must have an index value less than the second element of the tuple.
For example, if the input list is [3, 2, 1] and the total is 4, then the output list will only contain the tuple (3, 1) and not the tuple (1, 3).
But if the input list was [3, 2, 1, 3] and the total is 4, then the output list will contain both the tuple (3, 1) and the tuple (1, 3). Neither case would contain the tuple (2, 2).
For example:
Test | Result |
---|---|
print(pair_sum([4, 6, 2, 7, 3], 10)) |
[(4, 6), (7, 3)] |
print(pair_sum([4, 7, 8, 9, 3, 2, 6, 11, 1, 5, 10], 14)) |
[(4, 10), (8, 6), (9, 5), (3, 11)] |
Please find the following program using python
Program: collapse
import sys
def collapse(lis):
total=0
for i in range(len(lis)):
#if its a list, add it
and update the list as a single element
if isinstance(lis[i],
list):
for j in lis[i]:
total = total + j
lis[i]=total
vals = [1, [2, 3], 4, 5]
collapse(vals)
print(vals)
Output:
[1, 5, 4, 5]
Program: get_number
import sys
def get_number():
while 1:
#if the input is not
integer, ask the user again
try:
input1=input("Enter the number: ")
input2 = int(input1)
#if the input with in the range of 1 to 10, exit after the
float
if input2 >= 1 and input2 <= 10:
return (float(input2))
except ValueError:
continue
print(get_number())
Output:
Enter the number: 12
Enter the number: as
Enter the number: 321
Enter the number: 7
7.0
Program: pair_sum
def pair_sum(lis1, value):
itr =0
out=[]
tup=()
#iterate the complete list
for i in lis1:
total=0
#iterate the same list again and always compare it with the next
indxed element
#from outer loop
for j in
range(len(lis1)):
if j > itr:
total = i + lis1[j]
if total == value:
tup=(i,lis1[j])
#append the tuple to the list
out.append(tup)
break
itr = itr + 1
#return the list of tuples that has sum of the value
return (out)
print(pair_sum([3,2,1,3], 4))
print(pair_sum([4, 7, 8, 9, 3, 2, 6, 11, 1, 5, 10], 14))
Output:
[(3, 1), (1, 3)]
[(4, 10), (8, 6), (9, 5), (3, 11)]
Screen shots
Define a function called collapse() which takes a list as input. Each element of the list...
Define a function called get_n_largest(numbers, n) which takes a list of integers and a value n as parameters and returns a NEW list which contains the n largest values in the parameter list. The values in the returned list should be in increasing order. The returned list must always be of length n. If the number of values in the original list is less than n, the value None should be repeated at the end of the returned list to...
Write a function to find the smallest odd number in a list. The function takes a list of numbers (each number is greater than or equal to 0) as an input. The function outputs a tuple, where the first number in the tuple is the smallest odd number and the second number in the tuple is the number of occurrences of that number. For example, if the input is [1, 4, 7, 3, 5, 2, 1, 3, 6] then the...
Define a function called “mtok” that takes one input parameter representing a distance in miles and prints out the distance in Kilometers. Assume 1 mile equals 1.5 Km. Example Function Calls: mtok(2) # should print: 2 mile(s) equals 3.0 km mtok(10) # should print: 10 mile(s) equals 15 km
Write the function deep_contains. It takes as input a number and a list. That list might contain lists as elements, and those lists might contain lists, etc. The deep_contains' function should return a Boolean indicating whether the number is contained inputted list, or a sublist of it, or a sublist of that, and so forth. For example: deep_contains (3, (1,3,5]) returns True deep_contains(3, 11, [3,5]]) returns True deep_contains (3, 1, [[3,4],5),6]) returns True deep_contains (2, (1,3,5]) returns False This function...
Write the following function in Java: A function "makeMangler", that takes as input a list M of three numbers. It then builds and returns a "Mangler" function based on M. The "Mangler" function that is produced (by your function makeMangler) would have the property that it takes as input a list, and returns the "mangled" version of that list. "Mangling" a list means doing the following sequence of operations to each item in a list: (1) multiply by the first...
Define a function called max_association(), which recieves 2 lists, lst1, which is a list of strings, and lst2, which is a list of integers. The function must return a list of strings associated to the highest integers in lst1. Assume both lists have the same length As an example, the following code fragment: L1 = ['a','b','c','d','e','f','g','h','i','j'] L2 = [1,2,3,1,3,2,3,1,2,1] result = max_association(L1,L2) print(result) should produce the output: ['c', 'e', 'g']
0% of final (S1 2019) Write a function called sum_of_products(x), where x is a list of lists of integers, this function returns a number which is the sum of the products of each list of integers. For example: print(sum_of_products ([[1,2], [5], [2,5]])) will result in the output: 17 That is, the result of 2 + 5+ 10 For example: Test Result print(sum_of_products( [2,3]])) print sum_of_products([[2],[3]])) print(sum_of_products([[1,21,[S],[2,5]1)) 17 0% of final (S1 2019) Write a function called sum_of_products(x), where x is...
Define a function called AddEvenPosDigs(string), which takes a string (with symbols and characters) as an argument, and returns an integer. This function should add the digits of a string that are in an even position. If there are no digits, the function should return -1. As an example, the following code fragment: string = "a12b056jk"; result=AddEvenPosDigs(string); print(result) should produce the output: 8
Matlab code 4) Write a function leadzero(v) which takes as input a vector v and as output, c, returns the number of zeros at the beginning of v (number of zero elements before any non-zero element). For example, for input (-5, 5, 11] the output would be 0. If the input is [0, 0, 3, 0, 0, 0], the output would be 2. If the input is [0, 0, 0, 0, 7, 4) the output would be 4. 5) Write...
Scheme Language: Define a function nondecreastream, which takes in a stream of numbers and outputs a stream of lists, which overall has the same numbers in the same order, but grouped into segments that are non-decreasing. For example, if the input is a stream containing elements 1 2 3 4 1 2 3 4 1 1 1 2 1 1 0 4 3 2 1 ... the output should contain elements (1 2 3 4) (1 2 3 4) (1...