From 7487bfbbba1e3c0c525f0a806f345d58f8171861 Mon Sep 17 00:00:00 2001 From: Stanislav Mykhailenko Date: Thu, 27 Oct 2022 22:18:25 +0300 Subject: [PATCH] Add Lesson 2 Task 5 --- Lesson_2/task5.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Lesson_2/task5.py diff --git a/Lesson_2/task5.py b/Lesson_2/task5.py new file mode 100644 index 0000000..ec9c5d9 --- /dev/null +++ b/Lesson_2/task5.py @@ -0,0 +1,35 @@ +# 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)