# HW6

May 17th, 2021
452
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. # A
2. from random import choice
3. def qsort(ls):
4.     if len(ls)<=1:
5.         return ls
6.     pivot = choice(ls)
7.     left=[i for i in ls if i < pivot]
8.     x =[i for i in ls if i == pivot]
9.     right =[ i for i in ls if i >pivot]
10.     return qsort(left) + x + qsort(right)
11.
12. def fun(ls, n):
13.     original = ls.copy()
14.     if len(ls) <= 2*n:
15.         print("You didn't enter enough values.")
16.     else:
17.         ls = qsort(ls)
18.         for i in range(n):
19.             ls.pop(0)
20.             ls.pop(-1)
21.         print('With outliers removed: [', end='')
22.         print(*ls, sep=', ', end='')
23.         print(']')
24.         print('The original data: [', end='')
25.         print(*original, sep=', ', end='')
26.         print(']')
27.
28. ls = []
29. while True:
30.     try:
31.         num = int(input("Enter a value(blank to quit): "))
32.         ls.append(num)
33.     except:
34.         n = int(input('Enter the number of outliers to be removed: '))
35.         fun(ls, n)
36.         break
37.
38. # B
39. from random import randint
40. n = randint(1, 10)
41. arr = [[randint(1, 10) for _ in range(n)] for _ in range(n)]
42. left = []
43. right = []
44. sum_left = 0
45. sum_right = 0
46. for i in range(n):
47.     left.append(arr[i][i])
48.     sum_left += arr[i][i]
49.     right.append(arr[i][n-i-1])
50.     sum_right += arr[i][n-i-1]
51.
52. print("Random data:")
53. for i in range(n):
54.     for j in range(n):
55.         print(f'{arr[i][j]:2d}', end=' '*5)
56.     print()
57.
58. print("\nLeft diagonal-> [", end='')
59. print(*left, sep=', ', end='')
60. print('] ->', sum_left)
61.
62. print("Right diagonal-> [", end='')
63. print(*right, sep=', ', end='')
64. print('] ->', sum_right)
65.
66. if sum_left < sum_right:
67.     print('Sum of the left diagonal is less than sum of the right diagonal.')
68. elif sum_left > sum_right:
69.     print('Sum of the left diagonal is greater than sum of the right diagonal.')
70. else:
71.     print('Sum of the left diagonal is equal to sum of the right diagonal.')
72.
73. # C
74. def fun(ls):
75.     ave = 0
76.     c = 0
77.     for i in ls:
78.         ave += i
79.         c += 1
80.     ave /= c
81.
82.     print("Average value: ", ave)
83.     less = []
84.     equal = []
85.     greater = []
86.     for i in ls:
87.         if i < ave:
88.             less.append(i)
89.         elif i == ave:
90.             equal.append(i)
91.         else:
92.             greater.append(i)
93.     print('Less than Average:', *less)
94.     print('Average: ', *equal)
95.     print('Greater than Average:', *greater)
96.
97. ls = []
98. while True:
99.     try:
100.         num = int(input("Enter a value - [blank to quit]->"))
101.         ls.append(num)
102.     except:
103.         print('A list of numbers:\n [', end='')
104.         print(*ls, sep=', ', end='')
105.         print(']\n')
106.
107.         fun(ls)
108.         break
109.
110. # D
111. from random import choice
112. def qsort_ascending(ls):
113.     if len(ls)<=1:
114.         return ls
115.     pivot = choice(ls)
116.     left=[i for i in ls if i < pivot]
117.     x =[i for i in ls if i == pivot]
118.     right =[ i for i in ls if i >pivot]
119.     return qsort_ascending(left) + x + qsort_ascending(right)
120.
121. def qsort_descending(ls):
122.     if len(ls)<=1:
123.         return ls
124.     pivot = choice(ls)
125.     left=[i for i in ls if i < pivot]
126.     x =[i for i in ls if i == pivot]
127.     right =[ i for i in ls if i >pivot]
128.     return qsort_descending(right) + x + qsort_descending(left)
129.
130. def fun(ls):
131.     asc_ls = qsort_ascending(ls)
132.     desc_ls = qsort_descending(ls)
133.
134.     if asc_ls == ls:
135.         print('A list is ascendigly sorted')
136.     elif desc_ls == ls:
137.         print('A list is descendingly sorted')
138.     else:
139.         print('A list unsorted')
140.
141. ls = []
142. while True:
143.     try:
144.         num = int(input("Enter a value - [blank to quit]->"))
145.         ls.append(num)
146.     except:
147.         fun(ls)
148.         break
149.
150. # E
151. from random import uniform
152. def sum_of_coor(arr):
153.     s = 0
154.     for i in arr:
155.         s+=i
156.     return s
157.
158. def sum_of_squares(x):
159.     s = 0
160.     for i in x:
161.         s += i**2
162.     return s
163.
164. def sum_of_coor_mult(x, y):
165.     s = 0
166.     for i in range(len(x)):
167.         s += x[i] * y[i]
168.     return s
169.
170. def line(x, y, n=3):
171.     m = (sum_of_coor_mult(x, y) - sum_of_coor(x)*sum_of_coor(y)/n) / (sum_of_squares(x) - sum_of_coor(x)**2 / n)
172.     b = sum_of_coor(y)/n - m * sum_of_coor(x) / n
173.     return m, b
174.
175. x = [uniform(0, 5) for _ in range(3)]
176. y = [uniform(0, 5) for _ in range(3)]
177.
178. print('X coordinates: [', end='')
179. for i in range(3):
180.     if i != 2:
181.         print(f'{x[i]:.2f}, ', end='')
182.     else:
183.         print(f'{x[i]:.2f}]')
184.
185. print('Y coordinates: [', end='')
186. for i in range(3):
187.     if i != 2:
188.         print(f'{y[i]:.2f}, ', end='')
189.     else:
190.         print(f'{y[i]:.2f}]')
191.
192. m, b = line(x, y)
193. print(f'f(x) = {m:.2f}*x + {b:.2f}')
194.
RAW Paste Data