Find highest and lowest number from the string of numbers

后端 未结 5 860
慢半拍i
慢半拍i 2021-01-21 09:44

I\'m trying to write a function that returns the highest and lowest number in a list.

def high_and_low(numbers):

    return max(numbers), min(numbers)

print(hi         


        
5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-21 10:38

    In order to achieve your desired result you can call split() on the string you are passing in. This essentially creates a list() of your input string—which you can call the min() and max() functions on.

    def high_and_low(numbers: str):
        """
        Given a string of characters, ignore and split on
        the space ' ' character and return the min(), max()
    
        :param numbers: input str of characters
        :return: the minimum and maximum *character* values as a tuple
        """
        return max(numbers.split(' ')), min(numbers.split(' '))
    

    As others have pointed out you can also pass in a list of values you'd like to compare and can call the min and max functions on that directly.

    def high_and_low_of_list(numbers: list):
        """
        Given a list of values, return the max() and 
        min()
    
        :param numbers: a list of values to be compared
        :return: the min() and max() *integer* values within the list as a tuple
        """
        return min(numbers), max(numbers)
    

    Your original functions does technically work, however, it is comparing numerical values for each character and not just the integer values.

提交回复
热议问题