2.1get_diagonal(grid)
Requirements for the get_diagonal function:
grid is a 2-dimensional list
given the length of grid is N, the length of each list inside of grid is N
N could be any positive integer (grid could be any size, but will be square)
return a value that is a list of size N
the returned list consists of the diagonal of grid
the diagonal of grid is defined as a list that:
starts with the 1st item in the 1st list in grid
next has the 2nd item in the 2nd list in grid
continues down the diagonal until the last item in the returned list is the last item in the last list of grid
your function should not ask for any input or print anything out
For example, running this:
grid1 = [ [4, 7, 3], [1, 5, 2], [1, 3, 6], ] grid2 = [ [19, 12, 34, 22], [71, 10, 54, 36], [44, 92, 42, 15], [73, 67, 17, 84] ] print(get_diagonal(grid1)) print(get_diagonal(grid2))
Should print this:
[4, 5, 6] [19, 10, 42, 84]
2.2sum_all(string1)
Requirements for the sum_all function:
The string that is passed in consists of numbers separated by commas
The numbers will be integers and only positive values
There may be one or more newline characters at the beginning at end of the string that should be ignored
Return the sum of the numbers as an integer type
Your function should not ask for any input or print anything out
For example, running this:
string1 = "4,7,2" string2 = "\n\n12,11,10,2,100\n\n\n" print(sum_all(string1)) print(sum_all(string2))
Should print this:
13 135
2.3groceries(d,i,n)
Requirements for the groceries function:
The function will receive a dictionary, an ID number, and a name
Add the key-value pair, ID:name, to the dictionary unless the ID number is already being used
For ID numbers that are already in the dictionary, use the ID number 0. It is guaranteed that the key 0 will not be present when the dictionary is passed in.
Your function should not ask for any input or print anything out
For example, running this:
dictionary1 = {192: 'apples', 453: 'bananas', 12: 'tomatoes'} new_ID1 = 249 new_name1 = 'oranges' print(groceries(dictionary1, new_ID1, new_name1)) dictionary2 = {17: 'cookies', 124: 'chips'} new_ID2 = 124 new_name2 = 'bread' print(groceries(dictionary2, new_ID2, new_name2))
Should print this:
{192: 'apples', 453: 'bananas', 12: 'tomatoes', 249: 'oranges} {17: 'cookies', 124: 'chips', 0: 'bread'}