Advertisement
padznich

Untitled

Apr 23rd, 2019
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.11 KB | None | 0 0
  1. import fractions
  2. import decimal
  3. import collections
  4. import typing
  5.  
  6.  
  7. MinMax = collections.namedtuple('MinMax', ['max_value', 'min_value'])
  8.  
  9.  
  10. def get_max_and_min(data: typing.Set[typing.Union[decimal.Decimal, fractions.Fraction, str, int, float]]) -> typing.NamedTuple:
  11.  
  12.     numbers = []
  13.  
  14.     for num in data:
  15.         if isinstance(num, str):
  16.             if '\\' in num or '/' in num:
  17.                 num = num.replace('\\', '/').replace(' ', '')
  18.                 numbers.append(fractions.Fraction(num))
  19.             else:
  20.                 numbers.append(decimal.Decimal(num))
  21.         elif type(num) in (int, float, decimal.Decimal, fractions.Fraction):
  22.             numbers.append(num)
  23.         else:
  24.             print('-' * 20)
  25.             print(f'Not a number: {num}')
  26.             print('-' * 20)
  27.  
  28.     return MinMax(max_value=max(numbers), min_value=min(numbers))
  29.  
  30.  
  31. result = get_max_and_min({
  32.     '1.1',
  33.     5,
  34.     5.1,
  35.     decimal.Decimal('3.1'),
  36.     fractions.Fraction('3/5'),
  37.     '3 / 2',
  38.     r'3 \ 3',
  39.     r'4\2',
  40.     '6/2',
  41. })
  42.  
  43. print(f'MAX: {result.max_value}')
  44. print(f'MIN: {result.min_value}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement