3.4k views
2 votes
Consider a method named biggerThan that (1) takes two arguments - a list of ints named a and a single int named thresh; and then (2) returns a new list containing the elements from a that are > thresh; or None if there are no elements.

What should biggerThan return when called with a list a equal to [1, 2, 4, 6, 7, 8] and thresh set to 4? [1]
Provide example arguments a and thresh such that biggerThan should return a None. [1]
Write the method in correct, executable, Python. [3]

1 Answer

2 votes

Final answer:

The 'bigger Than' method should return a new list containing elements greater than the threshold value, or None if there are no elements. Here is the correct and executable Python code for the 'bigger Than' method.

Step-by-step explanation:

The bigger Than method, when called with a list a equal to [1, 2, 4, 6, 7, 8] and thresh set to 4, should return a new list containing the elements from a that are greater than thresh. In this case, the new list would be [6, 7, 8].

If there are no elements in the list a that are greater than thresh, the bigger Than method should return None. For example, if a is [1, 2, 3] and thresh is set to 4, the method should return None.

Here is the correct and executable Python code for the bigger Than method:

def bigger Than(a, thresh):
new _list = []
for num in a:
if num > thresh:
new _list .append(num)
if length (new _list) == 0:
return None
return new _list

The method 'bigger Than' when called with list [1, 2, 4, 6, 7, 8] and threshold 4 should return [6, 7, 8]. With list [1, 2, 3] and threshold 5, it should return None. An example Python implementation is provided.

The method bigger Than should return a new list containing the elements from the list a that are greater than thresh. When bigger Than is called with a equal to [1, 2, 4, 6, 7, 8] and thresh set to 4, it should return [6, 7, 8]. For example, if we provide arguments a as [1, 2, 3] and thresh set to 5, bigger Than should return None because there are no elements greater than 5.

Here's how you could write this method in Python:

def biggerThan(a, thresh):
result = [x for x in a if x > thresh]
return result if result else None
User Odane
by
8.6k points