Advertisement
SenyaSych

Тестовое задание #2

Mar 28th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.77 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. import random
  4. import sys
  5.  
  6. def Bubble_Sort(list_to_sort):
  7.     for i in range (len(list_to_sort), 0, -1):
  8.         for j in range(1, i):
  9.             if list_to_sort[j-1] > list_to_sort[j]:
  10.                 temporary_number = list_to_sort[j-1]
  11.                 list_to_sort[j-1] = list_to_sort[j]
  12.                 list_to_sort[j] = temporary_number
  13.     return list_to_sort
  14.  
  15. def Arithmetic_Mean(list_to_calculate):
  16.     average = 0
  17.     for i in list_to_calculate:
  18.         average += i
  19.     average /= len(list_to_calculate)
  20.     return round(average, 2)
  21.  
  22. def Median_Value(list_to_calculate):
  23.     if (len(list_of_numbers) % 2 == 0):
  24.         first_index = int (len(list_of_numbers) / 2)
  25.         second_index = int (len(list_of_numbers) / 2 - 1)
  26.         median = (list_of_numbers[first_index] + list_of_numbers[second_index]) / 2
  27.         return round(median, 2)
  28.     else:
  29.         index = int ((len(list_of_numbers) - 1) / 2)
  30.         median = list_of_numbers[index]
  31.         return round(median, 2)
  32.        
  33. try:
  34.     amount_of_numbers = int(input("Введите число элементов списка: "))
  35. except ValueError:
  36.     sys.exit("Количество элементов — натуральное значение!")
  37.  
  38. list_of_numbers = []
  39.  
  40. for i in range (amount_of_numbers):
  41.     list_of_numbers.append(round(random.uniform(0, 50), 2))
  42.  
  43. print("Исходный список:")
  44. for i in list_of_numbers:
  45.     print(i, end = " ")
  46.  
  47. list_of_numbers = Bubble_Sort(list_of_numbers)
  48.  
  49. print("\nОтсортированный список:")
  50. for i in list_of_numbers:
  51.     print(i, end = " ")
  52.    
  53. print ("\nСреднее арифметическое:", Arithmetic_Mean(list_of_numbers))
  54. print ("Медиана:", Median_Value(list_of_numbers))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement