From 6ddba93f4b3718b0c1ca73c14fc108ed9c346af4 Mon Sep 17 00:00:00 2001 From: Eduards Alexis Mendez Chipatecua Date: Wed, 25 Sep 2024 10:57:27 -0500 Subject: [PATCH] The solution allows other types of data because the previous solution only allowed data of type int.:: --- ch03-lists-tuples/e10b1_mysum_bigger_than.py | 48 ++++++++++++++------ 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/ch03-lists-tuples/e10b1_mysum_bigger_than.py b/ch03-lists-tuples/e10b1_mysum_bigger_than.py index 48ec86b..79dbf7c 100755 --- a/ch03-lists-tuples/e10b1_mysum_bigger_than.py +++ b/ch03-lists-tuples/e10b1_mysum_bigger_than.py @@ -1,17 +1,39 @@ -#!/usr/bin/env python3 """Solution to chapter 3, exercise 10, beyond 1: mysum_bigger_than""" +from typing import Tuple, Union + +def mysum_bigger_than(*args: Tuple[Union[int, float, str, list]]): + """ + Sum items, which should be of the same type. + Ignore any below the value of threshold. + The arguments should handle the + operator. + If passed no arguments, then return an empty tuple. + """ + + if not args: + return args + + first_element = args[0] + + # Initialize output based on the type of first_element + if isinstance(first_element, (int, float)): + output = 0 + elif isinstance(first_element, str): + output = "" + elif isinstance(first_element, list): + output = [] + else: + return None # In case an unsupported type is passed. + + for element in args[1:]: + if element > first_element: + output += element -def mysum_bigger_than(threshold, *items): - """Sum items, which should be of the same type. -Ignore any below the value of threshold. -The arguments should handle the + operator. -If passed no arguments, then return an empty tuple. -""" - if not items: - return items - output = 0 - for item in items: - if item > threshold: - output += item return output + +# Test cases +print(mysum_bigger_than([1, 2, 3], [4, 5, 6], [23, 52])) # Expecting [4, 5, 6] + [23, 52] +print(mysum_bigger_than(1, 2, 3)) # Expecting 2 + 3 +print(mysum_bigger_than('abc', 'def', 'ghi')) # Expecting 'def' + 'ghi' + +