II – Amaliy (1 ta masala tushadi, dastur tuziladi)
Natural n soni va A(1:n) haqiqiy sonlar to’plami berilgan. Berilgan sonlarning eng kichigini va unig o’rnini toping.
n = int(input("Natural sonning sonini kiriting: "))
A = []
for i in range(n):
A.append(float(input(f"{i+1}-chi haqiqiy sonni kiriting: ")))
min_value = min(A)
min_index = A.index(min_value)
print(f"Eng kichik son: {min_value}, uning indeksi: {min_index}")
Natural n soni va A(1:n) haqiqiy sonlar to’plami berilgan. Berilgan sonlarning o’rta arifmetik qiymatini toping.
n = int(input("Natural sonning sonini kiriting: "))
A = []
for i in range(n):
A.append(float(input(f"{i+1}-chi haqiqiy sonni kiriting: ")))
avg_value = sum(A) / n
print(f"Berilgan sonlar o'rta arifmetik qiymati: {avg_value}")
Natural n soni va a1, a2, ..., an haqiqiy sonlar berilgan. max(a1, ..., an) ga teng bo‘lgan elementdan oldingi barcha elementlar ko‘paytmasini toping.
n = int(input("Natural sonni kiriting: "))
A = []
for i in range(n):
A.append(float(input(f"{i+1}-chi haqiqiy sonni kiriting: ")))
max_value = max(A)
max_index = A.index(max_value)
product = 1
for i in range(max_index):
if A[i] < max_value:
product *= A[i]
print(f"max(a1, ..., an) ga teng bo‘lgan elementdan oldingi barcha elementlar ko‘paytmasi: {product}")
Natural n soni va A(1:n) butun sonlar jadvali berilgan. Undagi tartib nomeri toq, o‘zi juft bo‘lgan elementlarning yig‘indisi topilsin.
n = int(input("Natural sonni kiriting: "))
A = []
for i in range(n):
A.append(int(input(f"{i+1}-chi butun sonni kiriting: ")))
sum_value = 0
for i in range(n):
if i % 2 == 0 and A[i] % 2 == 0:
sum_value += A[i]
print(f"Tartib nomeri toq, o‘zi juft bo‘lgan elementlarning yig‘indisi: {sum_value}")
O‘zaro har xil bo‘lgan a1, a2, ..., a20 haqiqiy sonlar ketma-ketligi berilgan. Bu ketme-ketlikning eng katta va kichik elementlari o‘rinlarini almashtirilsin.
A = []
for i in range(20):
A.append(float(input(f"{i+1}-chi haqiqiy sonni kiriting: ")))
max_value = max(A)
min_value = min(A)
max_index = A.index(max_value)
min_index = A.index(min_value)
A[max_index], A[min_index] = A[min_index], A[max_index]
print(f"barcha ketma-ketlikning eng katta va kichik elementlari o‘rinlarini almashtirildi: {A}")
Vektorning o'rtacha, minimal va maksimal qiymatlarini hisoblash dasturini yozing (funksiya yarating).
def vector_stats(vector):
mean = sum(vector) / len(vector)
minimum = min(vector)
maximum = max(vector)
return mean, minimum, maximum
vector = [2.6, 5.8, 1.3, 4.7, 3.2]
mean, minimum, maximum = vector_stats(vector)
print(f"Vektorning o'rtachasi: {mean}")
print(f"Vektorning minimal qiymati: {minimum}")
print(f"Vektorning maksimal qiymati: {maximum}")
Vektor qiymatlari bo'yicha dispersiyani hisoblash dasturini yozing (funksiya yarating).
def vector_variance(vector):
mean = sum(vector) / len(vector)
diffs_sq = [(x - mean) ** 2 for x in vector]
variance = sum(diffs_sq) / len(vector)
return variance
vector = [2.6, 5.8, 1.3, 4.7, 3.2]
variance = vector_variance(vector)
print(f"Vektorning dispersiyasi: {variance}")
X va Y vektorlar uchun korrelyatsiya koeffitsientini hisoblash dasturini yozing (funksiya yarating)
def correlation_coefficient(X, Y):
n = len(X)
mean_X = sum(X) / n
mean_Y = sum(Y) / n
numerator = sum([(X[i] - mean_X) * (Y[i] - mean_Y) for i in range(n)])
denominator_X = sum([(X[i] - mean_X)**2 for i in range(n)])
denominator_Y = sum([(Y[i] - mean_Y)**2 for i in range(n)])
denominator = (denominator_X * denominator_Y)**0.5
correlation = numerator / denominator
return correlation
# sample input vectors
X = [3, 1, 4, 2, 5]
Y = [2, 3, 5, 1, 4]
# call the function and print the result
correlation = correlation_coefficient(X, Y)
print(f"Vektorlar X va Y uchun korrelyatsiya koeffitsienti: {correlation}")
Dostları ilə paylaş: |