You are on page 1of 10

Exercises 1

 Bookmark this page

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)

 Mean, Median, and Mode

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: ')

x = [int(i) for i in a.split()]


# print(x)
x.sort()
s = set(x)
print(round(sum(x)/n, 1))

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

for key in counts:


if len(x) == len(s):
print(x[0])
break
elif counts[key] == max(counts.values()):
print(key)
break

 Weighted Mean
n = int(input())
a = input()
b = input()

X = list(map(int, a.split()))
W = list(map(int, b.split()))

print(round(sum([X[i]*W[i] for i in range(len(X))])/sum(W), 1))


hoặc:
n = int(input())
a = input()
b = input()

X = list(map(int, a.split()))
W = list(map(int, b.split()))

print(round(sum([x*w for x,w in zip(X,W)]) / sum(W), 1))

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())

data = list(map(int, input().rstrip().split()))

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):

fptr = open('OUTPUT_PATH.txt', 'w')

Hoặc:

1.Đối với n là số lẻ:

Q2 = (n-1)/2
y1 = (n-1)/4

x1 = (n-1)/4 – 1 = (n-5)/4

Q1 = (x1 +y1)/2

x3 = (n-1)/2 + (n-1)/4 = ¾(n-1)

y3 = ¾(n-1) + 1 = (3n+1)/4

Q3 = (x3 + y3)/2

2.As n is even number:

a. If n/2 is odd number:

x2 = n/2 – 1

y2 = n/2

Q2 = (x2 + y2)/2

Q1 = (n/2 - 1)/2 = (n-2)/4

Q3 = n/2 + (n-2)/4 = (3n-2)/4

b. If n/2 is even number:

x2 = n/2 – 1

y2 = n/2

Q2 = (x2 + y2)/2

y2 = n/4

x1 = n/4 – 1

Q1 = (x1 + y1)/2

x3 = n/2 + (n/4 -1) = 3/4*n – 1


y3 = n/3 + n/4 = ¾*n

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())

data = list(map(int, input().rstrip().split()))


res = quartiles(data)

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

Tính độ trải giữa


#!/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
#

def interQuartile(values, freqs):


# Print your answer to 1 decimal place within this function
arr = list()
# Nhân values với freqs
size = len(values)
for i in range(size):
arr += [values[i]] * freqs[i]
arr.sort()
middle = sum(freqs) // 2
if middle % 2 == 0:
Low = arr[:middle]
Upper = arr[middle:]
else:
Low = arr[:middle - 1]
Upper = arr[middle + 1:]
Q1 = statistics.median(Low)
Q3 = statistics.median(Upper)
print(round((Q3 - Q1), 1))

if __name__ == '__main__':
n = int(input().strip())

val = list(map(int, input().rstrip().split()))

freq = list(map(int, input().rstrip().split()))

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
#

def interQuartile(values, freqs):


# Print your answer to 1 decimal place within this function
x = []
# Nhân values với freqs
for i in range(len(values)):
for j in range(int(freqs[i])):
x.append(values[i])
x.sort()
mid = sum(freqs) // 2
if mid % 2 == 0:
left = x[:mid]
right = x[mid:]
else:
left = x[:(mid - 1)]
right = x[(mid + 1):]
Q1 = statistics.median(left)
Q3 = statistics.median(right)
ir = round(float(Q3 - Q1), 1)
print(ir)

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

Tính độ lệch chuẩn


import math
import os
import random
import re
import sys
import statistics

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)

 Standard Deviation Puzzles - 1

 Standard Deviation Puzzles - 2


mean = 0.675
stdev = 0.065
value = 0.9025
print(round((value - mean)/stdev, 2))

 Standard Deviation Puzzles - 3

Ans: x = 5.0
print(x)

Công thức tính phương sai tổng, hiệu của X,Y:

Chi tiết xem tại link dưới, từ đó suy ra độ lệch chuẩn

http://www.stat.yale.edu/Courses/1997-98/101/rvmnvar.htm

 Standard Deviation Puzzles - 4

Ans: x = 5.0
print(x)

 Standard Deviation Puzzles - 5

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.

Chi tiết xem tại link:

http://www.stat.yale.edu/Courses/1997-98/101/rvmnvar.htm

 Standard Deviation Puzzles - 6

Ans:

x = 2 * 10
print(float(x))

You might also like