Check if all Values in Array are Smaller in Python

0 min read 189 words

The challenge

You will be given an array and a limit value. You must check that all values in the array are below or equal to the limit value. If they are, return true. Else, return false.

You can assume all values in the array are numbers.

The solution in Python code

Option 1:

def small_enough(array, limit):
    return True if max(array)<=limit else False

Option 2:

def small_enough(array, limit):
    return max(array)<=limit

Option 3:

def small_enough(array, limit):
    return all(a <= limit for a in array)

Test cases to validate our solution

import test
from solution import small_enough

@test.describe("Fixed Tests")
def fixed_tests():
    @test.it('Basic Test Cases')
    def basic_test_cases():

        tests = (
            ([[66, 101] ,200], True),
            ([[78, 117, 110, 99, 104, 117, 107, 115] ,100], False),
            ([[101, 45, 75, 105, 99, 107], 107], True),
            ([[80, 117, 115, 104, 45, 85, 112, 115] ,120], True),
            ([[1, 1, 1, 1, 1, 2] ,1], False),
            ([[78, 33, 22, 44, 88, 9, 6] ,87], False),
            ([[1, 2, 3, 4, 5, 6, 7, 8, 9] ,10], True),
            ([[12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12] ,12], True),
        )
        
        for inp, exp in tests:
            test.assert_equals(small_enough(*inp), exp)
Tags:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags

Recent Posts