Professional Documents
Culture Documents
Học viên làm các bài tập rèn luyện dưới đây: (Lưu ý các bài tập này không tính vào
điểm quá trình)
Ans:
import statistics
n = int(input())
x = list(map(float, input().split()))
mean = statistics.mean(x)
median = statistics.median(x)
mode = statistics.mode(sorted(x))
print(mean)
print(median)
print(mode)
Hoặc:
import statistics
N = int(input())
arr_ = list(map(int, input().split()))
arr_.sort()
print(round(statistics.mean(arr_), 1))
print(round(statistics.median(arr_), 1))
print(round(statistics.mode(arr_), 1))
hoặc:
n = int(input('The number of elements: '))
a = input('N space-separated integers: ')
if n % 2 == 0:
y = (x[int((n/2)-1)] + x[int(n/2)])/2
else:
y = x[int((n-1)/2)]
print(y)
counts = dict()
for i in x:
counts[i] = counts.get(i, 0) + 1
Weighted Mean
n = int(input())
a = input()
b = input()
X = list(map(int, a.split()))
W = list(map(int, b.split()))
X = list(map(int, a.split()))
W = list(map(int, b.split()))
hoặc:
n = int(input())
a = input()
b = input()
x = list(map(int, a.split()))
w = list(map(int, b.split()))
lst = list()
for i in range(n):
r = x[i] * w[i]
lst.append(r)
weight_mean = round(sum(lst)/sum(w), 1)
print(weight_mean)
Quartiles
Tính tứ phân vị
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'quartiles' function below.
#
# The function is expected to return an INTEGER_ARRAY.
# The function accepts INTEGER_ARRAY arr as parameter.
#
import statistics
def quartiles(arr):
arr.sort()
m = statistics.median(arr)
arr1,arr2 = [],[]
for i in arr:
if i < m:
arr1.append(i)
elif i > m:
arr2.append(i)
m1 = statistics.median(arr1)
m2 = statistics.median(arr2)
return int(m1),int(m),int(m2)
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
n = int(input().strip())
res = quartiles(data)
fptr.write('\n'.join(map(str, res)))
fptr.write('\n')
fptr.close()
Sửa lại thành file của mình như sau (sửa dòng in đậm):
Hoặc:
Q2 = (n-1)/2
y1 = (n-1)/4
x1 = (n-1)/4 – 1 = (n-5)/4
Q1 = (x1 +y1)/2
y3 = ¾(n-1) + 1 = (3n+1)/4
Q3 = (x3 + y3)/2
x2 = n/2 – 1
y2 = n/2
Q2 = (x2 + y2)/2
x2 = n/2 – 1
y2 = n/2
Q2 = (x2 + y2)/2
y2 = n/4
x1 = n/4 – 1
Q1 = (x1 + y1)/2
Q3 = (x3 + y3)/2
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'quartiles' function below.
#
# The function is expected to return an INTEGER_ARRAY.
# The function accepts INTEGER_ARRAY arr as parameter.
#
import statistics
def quartiles(arr):
arr.sort()
if n % 2 != 0:
Q2 = arr[int((n - 1) / 2)]
x1 = arr[int((n - 5) / 4)]
y1 = arr[int((n - 1) / 4)]
Q1 = (x1 + y1) / 2
x3 = arr[int(3 / 4 * (n - 1))]
y3 = arr[int((3 * n + 1) / 4)]
Q3 = (x3 + y3) / 2
else:
if n / 2 % 2 != 0:
x2 = arr[int((n / 2) - 1)]
y2 = arr[int(n / 2)]
Q2 = (x2 + y2) / 2
Q1 = arr[int((n - 2) / 4)]
Q3 = arr[int((3 * n - 2) / 4)]
else:
x2 = arr[int((n / 2) - 1)]
y2 = arr[int(n / 2)]
Q2 = (x2 + y2) / 2
y1 = arr[int(n / 4)]
x1 = arr[int((n / 4) - 1)]
Q1 = (x1 + y1) / 2
x3 = arr[int((3 / 4 * n) - 1)]
y3 = arr[int(3 / 4 * n)]
Q3 = (x3 + y3) / 2
return int(Q1), int(Q2), int(Q3)
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
n = int(input().strip())
fptr.write('\n'.join(map(str, res)))
fptr.write('\n')
fptr.close()
Hoặc:
n = int(input())
arr = list(map(int, input().split()))
arr.sort()
if n % 2 != 0:
Q2 = arr[int((n - 1) / 2)]
x1 = arr[int((n - 5) / 4)]
y1 = arr[int((n - 1) / 4)]
Q1 = (x1 + y1) / 2
x3 = arr[int(3 / 4 * (n - 1))]
y3 = arr[int((3 * n + 1) / 4)]
Q3 = (x3 + y3) / 2
else:
if n/2 % 2 != 0:
x2 = arr[int((n/2)-1)]
y2 = arr[int(n / 2)]
Q2 = (x2 + y2) / 2
Q1 = arr[int((n - 2) / 4)]
Q3 = arr[int((3*n-2) / 4)]
else:
x2 = arr[int((n / 2)-1)]
y2 = arr[int(n / 2)]
Q2 = (x2 + y2) / 2
y1 = arr[int(n / 4)]
x1 = arr[int((n / 4)-1)]
Q1 = (x1 + y1) / 2
x3 = arr[int((3 / 4 * n)-1)]
y3 = arr[int(3/4*n)]
Q3 = (x3 + y3) / 2
print(int(Q1))
print(int(Q2))
print(int(Q3))
Interquartile Range
import math
import os
import random
import re
import sys
import statistics
#
# Complete the 'interQuartile' function below.
#
# The function accepts following parameters:
# 1. INTEGER_ARRAY values
# 2. INTEGER_ARRAY freqs
#
if __name__ == '__main__':
n = int(input().strip())
interQuartile(val, freq)
Hoặc
#!/bin/python3
import math
import os
import random
import re
import sys
import statistics
#
# Complete the 'interQuartile' function below.
#
# The function accepts following parameters:
# 1. INTEGER_ARRAY values
# 2. INTEGER_ARRAY freqs
#
if __name__ == '__main__':
n = int(input().strip())
val = list(map(int, input().rstrip().split()))
freq = list(map(int, input().rstrip().split()))
interQuartile(val, freq)
Standard Deviation
def stdDev(arr):
mean = statistics.mean(arr)
s = 0
for i in arr:
s += (i - mean)**2
dlc = math.sqrt(s/len(arr))
print(dlc)
if __name__ == '__main__':
n = int(input().strip())
vals = list(map(int, input().rstrip().split()))
stdDev(vals)
Ans: x = 5.0
print(x)
http://www.stat.yale.edu/Courses/1997-98/101/rvmnvar.htm
Ans: x = 5.0
print(x)
Ans: x = 10.0
print(x)
Khi thêm hoặc bớt giá trị vào mẫu thì phương sai không thay đổi như sau:
Properties of Variances
If a random variable X is adjusted by multiplying by the value b and adding the
value a, then the variance is affected as follows:
Since the spread of the distribution is not affected by adding or subtracting a constant,
the value a is not considered. And, since the variance is a sum of squared terms, any
multiplier value b must also be squared when adjusting the variance.
http://www.stat.yale.edu/Courses/1997-98/101/rvmnvar.htm
Ans:
x = 2 * 10
print(float(x))