Thursday, December 7, 2023
HomeSoftware EngineeringCalculate the Sum of the 2 lowest constructive integers in Python

Calculate the Sum of the 2 lowest constructive integers in Python


The problem

Create a perform that returns the sum of the 2 lowest constructive numbers given an array of minimal 4 constructive integers. No floats or non-positive integers might be handed.

For instance, when an array is handed like [19, 5, 42, 2, 77], the output must be 7.

[10, 343445353, 3453445, 3453545353453] ought to return 3453455.

The answer in Python code

Choice 1:

def sum_two_smallest_numbers(numbers):
    return sum(sorted(numbers)[:2])

Choice 2:

def sum_two_smallest_numbers(num_list):
    num_list.type()
    return num_list[0] + num_list[1]

Choice 3:

sum_two_smallest_numbers = lambda A: sum( sorted( filter( lambda x:x>0, A) )[:2] )

Check instances to validate our answer

import check
from answer import sum_two_smallest_numbers

@check.describe("Fastened Exams")
def fixed_tests():
    @check.it('Fundamental Check Circumstances')
    def basic_test_cases():
        check.assert_equals(sum_two_smallest_numbers([5, 8, 12, 18, 22]), 13)
        check.assert_equals(sum_two_smallest_numbers([7, 15, 12, 18, 22]), 19)
        check.assert_equals(sum_two_smallest_numbers([25, 42, 12, 18, 22]), 30)
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments