def calculate_total(price_list: List[list]) -> int:
"""Return the sum of all second elements in the sublists of
price_list.
Precondition: price_list is a list of lists of length 2, and the
second
element of it sublist is an int.
>>> price_list = [["apple", 1], ["sugar", 5], ["mango",
3],
... ["coffee", 9], ["trail mix", 6]]
>>> calculate_total(price_list)
In this Python program
- We have defined a function called calculate_total which accepts a list and returns an int variable.
- In the list price_list, we have a list of lists which needs to satisfy 2 conditions one of them is it should have length 2 and also the 2nd element in the sublist should be an int
- Only those sublists which satisfy the above 2 conditions, those 2nd elements in the sublist got summed up
- Finally we are returning the int variable called sum as an output
Program:
def calculate_total(price_list: list) -> int:
sum=0
for i in price_list:
if( len(i)==2 and isinstance(i[1], int) ):
sum=sum+i[1]
return sum
price_list = [["apple", 1], ["sugar", 5], ["mango", 3],
["coffee", 9], ["trail mix", 6]]
print(calculate_total(price_list))
Output:
Hope this answer is clear and Helps!!!
If not please comment, I will Help you with that...
def calculate_total(price_list: List[list]) -> int: """Return the sum of all second elements in the sublists of...
def most_expensive_item(price_list: List[list]) -> str: """Return the name of the most expensive item in price_list. Precondition: price_list is a list of lists in the following format: [ [str, int], [str, int], ... ] where each 2-element list represents a name (str) and a price (int) of an item. price_list has at least one element. >>> price_list = [["apple", 1], ["sugar", 5], ["mango", 3], ... ["coffee", 9], ["trail mix", 6]] >>> most_expensive_item(price_list) """ please complete the function body in Python
def get_indices(lst, elm): ''' (list of lists, object) -> tuple of two ints Given a list of lists and an element, find the first pair of indices at which that element is found and return this as a tuple of two ints. The first int would be the index of the sublist where the element occurs, and the second int would be the index within this sublist where it occurs. >>> get_indices([[1, 3, 4], [5, 6, 7]], 1) (0, 0)...
def slice_list(lst: List[Any], n: int) -> List[List[Any]]: """ Return a list containing slices of <lst> in order. Each slice is a list of size <n> containing the next <n> elements in <lst>. The last slice may contain fewer than <n> elements in order to make sure that the returned list contains all elements in <lst>. === Precondition === n <= len(lst) >>> slice_list([3, 4, 6, 2, 3], 2) == [[3, 4], [6, 2], [3]] True >>> slice_list(['a', 1, 6.0, False],...
def _merge(lst: list, start: int, mid: int, end: int) -> None: """Sort the items in lst[start:end] in non-decreasing order. Precondition: lst[start:mid] and lst[mid:end] are sorted. """ result = [] left = start right = mid while left < mid and right < end: if lst[left] < lst[right]: result.append(lst[left]) left += 1 else: result.append(lst[right]) right += 1 # This replaces lst[start:end] with the correct sorted version. lst[start:end] = result + lst[left:mid] + lst[right:end] def find_runs(lst: list) -> List[Tuple[int, int]]: """Return a...
In this assignment, you will use a Hashtable to determine the common elements in all the lists. If an element appears more than once in one or more lists, the algorithm should capture the instances the element is present in all the lists. You are given a code that implements a Hashtable as an array of linked lists. You are also given a main function that will create an array of Lists using the input variable values that you enter....
def verifyMagic( matrix, num): "!" Given a list of lists called matrix, recursively verify that each list sums to num. Return True if that's the case, otherwise, return false. If the list is empty, return None. Assume that the correct input types were given." # Base case 1: a matrix with no elements if return # Base case 2: a matrix with one element if # Check if the sum of the only element in the list is num. #...
#C++, Programming: Program Design Including Data Structures, 7th Edition, it is too long so i can't add the these two file.(unorderedlinked.h and linkedlist.h) as well as the output Please use the file names listed below since your file will have the following components: Ch16_Ex5_MainProgram.cpp - given file //22 34 56 4 19 2 89 90 0 14 32 88 125 56 11 43 55 -999 #include #include "unorderedLinkedList.h" using namespace std; int main() { unorderedLinkedList list, subList; int num; ...
match_enzymes: (str, List[str], List[str]) -> List[list] The return type is a list of two-item [str, List[int]] lists The first parameter represents a strand of DNA. The last two parameters are parallel lists: the second parameter is a list of restriction enzyme names, and the third is the corresponding list of recognition sequences. (For example, if the first item in the second parameter is 'BamHI', then the first item in the third parameter would be 'GGATCC', since the restriction enzyme named...
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...