Question

Constructing a List from Another List One typical kind of list processing is to construct a...

Constructing a List from Another List

One typical kind of list processing is to construct a list from information in another list. This is typically done by iterating over the input list using a for loop and building up the result in another list.

Write a function definition of all_gt that takes a list of numbers, say nums, and a number, say n and returns the list of numbers from nums that are greater than n. The order of elements should be preserved.

For example:
all_gt([1,2,3,4], 2) => [3,4]
all_gt([1,2,3,4], 4) => []

def all_gt(nums, n):
"""Return the list of all numbers from nums that are bigger than n.

Parameters:
nums (list<number>): a list of numbers
n (number): threshold value

Return:
list<numbers>: numbers which are > n
  
"""
# add your code here

Homework Answers

Answer #1
def all_gt(nums, n):
    """Return the list of all numbers from nums that are bigger than n.

    Parameters:
    nums (list<number>): a list of numbers
    n (number): threshold value

    Return:
    list<numbers>: numbers which are > n

    """
    result = []
    for num in nums:
        if num > n:
            result.append(num)
    return result


print(all_gt([1, 2, 3, 4], 2))
print(all_gt([1, 2, 3, 4], 4))

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an array of ints, an int value named element, and an int value named end. Return a bool based on whether the element appears in the array starting from index 0 and up to but not including the end index. Generate Random Array Write a function that takes as parameters an array of integers and another integer for the size of the array. Create a...
[PART ONE OF PROJECT, ALREADY COMPLETED] An accumulator is a primitive kind of calculator that can...
[PART ONE OF PROJECT, ALREADY COMPLETED] An accumulator is a primitive kind of calculator that can evaluate arithmetic expressions. In fact, the Arithmetic-Logic Unit (ALU) of the rst computers was just an accumulator. An arithmetic expression, as you know, consists of two kinds of tokens: operands and operators All our operands will be (float) numbers and for a start, we shall use only two operators: + (plus) and - (minus) A sample run of the program would look like this....
What are 4 key things you learned about the topic from reading their paper? How does...
What are 4 key things you learned about the topic from reading their paper? How does the topic relate to you and your current or past job? Critique the paper in terms of the organization and quality. Team 3 answer questions above. Part I In today’s world we see fear among people when dealing with sexual harassment. This leads to people not reporting sexual harassment. A misconception about sexual harassment is that it’s only about touching and forcing other people...
read Seasons of Love chapter:measuring a child's life after suicide. please answer the questions : reflect...
read Seasons of Love chapter:measuring a child's life after suicide. please answer the questions : reflect on what happens to the families when there is a suicide in the family, based on the Seasons of Love chapter...how should people be told? What details are best left unshared? below is the story These theories may have a certain face-validity, but they often neglect environmental or contextual factors that are innate to answering the question of “why” a person might engage in...
What tools could AA leaders have used to increase their awareness of internal and external issues?...
What tools could AA leaders have used to increase their awareness of internal and external issues? ???ALASKA AIRLINES: NAVIGATING CHANGE In the autumn of 2007, Alaska Airlines executives adjourned at the end of a long and stressful day in the midst of a multi-day strategic planning session. Most headed outside to relax, unwind and enjoy a bonfire on the shore of Semiahmoo Spit, outside the meeting venue in Blaine, a seaport town in northwest Washington state. Meanwhile, several members of...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT