Wednesday, February 21, 2024
HomeSoftware EngineeringLearn how to Type a Checklist of Numbers in Python

Learn how to Type a Checklist of Numbers in Python

The problem

End the answer in order that it kinds the passed-in array/record of numbers. If the operate passes in an empty array/record or null/None worth then it ought to return an empty array/record.


resolution([1,2,3,10,5]) # ought to return [1,2,3,5,10]
resolution(None) # ought to return []

The answer in Python code

Choice 1:

def resolution(nums):
    if not nums:
        return []
    return sorted(nums)

Choice 2:

def resolution(nums):
    return sorted(nums) if nums else []

Choice 3:

def resolution(nums):
    return sorted(nums or [])

Take a look at instances to validate our resolution

import take a look at
from resolution import resolution

@take a look at.describe("Mounted Exams")
def fixed_tests():
    @take a look'Fundamental Take a look at Instances')
    def basic_test_cases():
        take a look at.assert_equals(resolution([1,2,3,10,5]), [1,2,3,5,10])
        take a look at.assert_equals(resolution(None), [])
        take a look at.assert_equals(resolution([]), [])
        take a look at.assert_equals(resolution([20,2,10]), [2,10,20])
        take a look at.assert_equals(resolution([2,20,10]), [2,10,20])


Please enter your comment!
Please enter your name here

Most Popular

Recent Comments