Question

In: Computer Science

def compare_elevations_within_row(elevation_map: List[List[int]], map_row: int, level: int) -> List[int]: """Return a new list containing the three...

def compare_elevations_within_row(elevation_map: List[List[int]], map_row: int, level: int) -> List[int]:
"""Return a new list containing the three counts: the number of
elevations from row number map_row of elevation map elevation_map
that are less than, equal to, and greater than elevation level.

Precondition: elevation_map is a valid elevation map.
0 <= map_row < len(elevation_map).

>>> compare_elevations_within_row(THREE_BY_THREE, 1, 5)
[1, 1, 1]
>>> compare_elevations_within_row(FOUR_BY_FOUR, 1, 2)
[0, 1, 3]

"""
for i in elevation_map[map_row]:
differences=[0,0,0]
if i<level:
differences[0] += 1
elif i==level:
differences[1] += 1
else:
differences[2] += 1
return differences

This was written in python, I am wondering why does the header of the function shows syntax error starting from List. (List[List[int]], map_row: int, level: int) -> List[int]:)these are all underedline

Solutions

Expert Solution

# do comment if any problem arises

# Code

# you are missing this import

from typing import List


def compare_elevations_within_row(elevation_map: List[List[int]], map_row: int, level: int) -> List[int]:

    """Return a new list containing the three counts: the number of

    elevations from row number map_row of elevation map elevation_map

    that are less than, equal to, and greater than elevation level.

    Precondition: elevation_map is a valid elevation map.

    0 <= map_row < len(elevation_map).

    >>> compare_elevations_within_row(THREE_BY_THREE, 1, 5)

    [1, 1, 1]

    >>> compare_elevations_within_row(FOUR_BY_FOUR, 1, 2)

    [0, 1, 3]

    """

    for i in elevation_map[map_row]:

        differences = [0, 0, 0]

        if i < level:

            differences[0] += 1

        elif i == level:

            differences[1] += 1

        else:

            differences[2] += 1

    return differences

Screenshot:


Related Solutions

from typing import List def longest_chain(submatrix: List[int]) -> int: """ Given a list of integers, return...
from typing import List def longest_chain(submatrix: List[int]) -> int: """ Given a list of integers, return the length of the longest chain of 1's that start from the beginning. You MUST use a while loop for this! We will check. >>> longest_chain([1, 1, 0]) 2 >>> longest_chain([0, 1, 1]) 0 >>> longest_chain([1, 0, 1]) 1 """ i = 0 a = [] while i < len(submatrix) and submatrix[i] != 0: a.append(submatrix[i]) i += 1 return sum(a) def largest_rectangle_at_position(matrix: List[List[int]], x:...
def warmer_year(temps_then: List[int], temps_now: List[int]) -> List[str]: """Return a list of strings representing whether this year's...
def warmer_year(temps_then: List[int], temps_now: List[int]) -> List[str]: """Return a list of strings representing whether this year's temperatures from temps_now are warmer than past temperatures in temps_then. The resulting list should contain "Warmer" at the indexes where this year's temperature is warmer, and "Not Warmer" at the indexes where the past year was warmer, or there is a tie. Precondition: len(temps_then) == len(temps_now) >>> warmer_year([10], [11]) ['Warmer'] >>> warmer_year([26, 27, 27, 28], [25, 28, 27, 30]) ['Not Warmer', 'Warmer', 'Not Warmer',...
def largest_rectangle_in_matrix(matrix: List[List[int]]) -> int: """ Returns the area of the largest rectangle in <matrix>. The...
def largest_rectangle_in_matrix(matrix: List[List[int]]) -> int: """ Returns the area of the largest rectangle in <matrix>. The area of a rectangle is defined as the number of 1's that it contains. Again, you MUST make use of <largest_rectangle_at_position> here. If you managed to code largest_rectangle_at_position correctly, this function should be very easy to implement. Similarly, do not modify the input matrix. Precondition: <matrix> will only contain the integers 1 and 0. >>> case1 = [[1, 0, 1, 0, 0], ... [1,...
from partition import partition def quicksort(a: list, l: int, u: int) -> None: '''Sort the given...
from partition import partition def quicksort(a: list, l: int, u: int) -> None: '''Sort the given list a in non-descending order. Precondition: 0 <= l and u < len(a)''' if l < u: mid = (l + u) // 2 three = [a[l], a[mid], a[u]] three.sort() if three[1] == a[l]: pivot_loc = l elif three[1] == a[u]: pivot_loc = u else: pivot_loc = mid a[u], a[pivot_loc] = a[pivot_loc], a[u] pivot = a[u] i = partition(a, l, u - 1, pivot)...
1. Int abc; 2. Int def = 8; 3. abc = def; ➢ Describe the procedure...
1. Int abc; 2. Int def = 8; 3. abc = def; ➢ Describe the procedure how much memory will be allocated when these three lines of codes will execute. ➢ Describe what will happened after execution of each of these line of code in term of memory allocation and data storage.
def num_to_digit_rec(num, base): """ Return a list of digits for num with given base; Return an...
def num_to_digit_rec(num, base): """ Return a list of digits for num with given base; Return an empty list [] if base < 2 or num <= 0 """ # Write your code here return [] def digit_sum(num, base): """ Return the sum of all digits for a num with given base Your implementation should use num_to_digit_rec() function """ # Write your code here return 0 def digit_str(num, base): """ Given a number and a base, for base between [2, 36]...
Qno.1 Part(A). IN jAVA if 1.Int abc; 2. Int def = 8; 3. abc = def;...
Qno.1 Part(A). IN jAVA if 1.Int abc; 2. Int def = 8; 3. abc = def; ➢ Describe the procedure how much memory will be allocated when these three lines of codes will execute. ➢ Describe what will happened after execution of each of these line of code in term of memory allocation and data storage Qno.1 Part(B) A capacitor is constructed from two conductive metal plates 30cm x 50cm which are spaced 6mm apart from each other, and uses...
Write a static method remove(int v, int[] in) that will return a new array of the...
Write a static method remove(int v, int[] in) that will return a new array of the integers in the given array, but with the value v removed. For example, if v is 3 and in contains 0, 1, 3, 2, 3, 0, 3, and 1, the method will return an array containing 0, 1, 2, 0, and 1. Hint: You can follow two steps to solve this problem: Create an array in the method, let say you called it result....
############callbacks ##def function_1( x ) : return x ** 2 ##def function_2( x ) : return...
############callbacks ##def function_1( x ) : return x ** 2 ##def function_2( x ) : return x ** 3 ##def function_3( x ) : return x ** 4 ## ###### create a list of callbacks to each of the functions ######by referencing their names ## ##callbacks = [ function_1 , function_2 , function_3 ] ## ######display a heading and the result of passing a value to each of the ######named functions: ## ##print( '\nNamed Functions:' ) ##for function in callbacks...
In Python Complete the oddNumbers() functions to take an int (as a parameter). Return a list...
In Python Complete the oddNumbers() functions to take an int (as a parameter). Return a list of all of the odd numbers between 1 and one less than the parameter. Also, complete the evenNumbers() functions to take an int (as a parameter). Return a list of all of the even numbers between 2 and one less than the parameter.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT