Pergunta de entrevista da empresa Tomofun

Define a class which fit following conditions 1. initialize this class to contain the input string, which includes various numbers with delimiter "#". Following is an example: "100#3#18#11#123#500#96" 2. this class provides following 3 functions and output type should be integer: max: return the maximum number from the input string min: return the minimum number from the input string median: return the median number from the input string The input will be a string as following example:"100#3#18#11#123#500#96" The output should be an the integer sequence by the ordering: max, median and min separated by a dash, following is the example: 500-96-3 - Examples Input: "1#2#3" Output: 3-2-1 Input: "4#2#3#1" Output: 4-2-1

Resposta da entrevista

Sigiloso

29 de mai. de 2024

class Numbers: def __init__(self, input_string): self.numbers = [int(num) for num in input_string.split('#')] def max(self): return max(self.numbers) def min(self): return min(self.numbers) def median(self): self.numbers.sort() index = len(self.numbers) // 2 if len(self.numbers) % 2 == 0: return (self.numbers[index - 1]) else: return self.numbers[index] def MedianSeparation(strParam): if strParam.counter('#')<2: return "Not enough number" number_list = Numbers(strParam) return f"{number_list.max()}-{number_list.median()}-{number_list.min()}" # keep this function call here print(MedianSeparation(input()))