This repository has been archived on 2024-10-24. You can view files and clone it, but cannot push or open issues or pull requests.
NG_2022_Stanislav_Mykhailenko/Lesson_2/task5.py

36 lines
884 B
Python
Raw Normal View History

2022-10-27 19:18:25 +00:00
# Lesson 2 Task 5: sort a comma-separated array of numbers, output the smallest and the biggest elements, and the sum of the rest elements
# Author: Stanislav Mykhailenko
# License: Unlicense
# Return codes:
# 0 - OK
def is_float(number):
try:
float(number)
except ValueError:
return False
return True
list = [element for element in list(dict.fromkeys(input("Enter a comma-separated list: ").split(','))) if is_float(element)] # remove all duplicates and non-number values
if len(list) == 0:
print("The list is empty")
else:
list.sort(key = float)
print(list)
print ("The " + ("only" if len(list) == 1 else "smallest") + " element is: " + str(list[0]))
list.remove(list[0])
if len(list) > 0:
print ("The biggest element is: " + str(list[-1]))
list.remove(list[-1])
if len(list) > 0:
sum = 0
for elem in list:
sum = sum + float(elem)
print(sum)