10-amaliy mashg’ulot Mavzu: Python dasturlash tilida massivlar va ulardan foydalanish. Bir va ikki o’lchovli massivlar bilan ishlash. Massivlar bilan ishlash


Download 67.5 Kb.
Sana15.11.2023
Hajmi67.5 Kb.
#1776371
Bog'liq
10 amaliy mashg’ulot Mavzu Python dasturlash tilida massivlar v


10-amaliy mashg’ulot
Mavzu: Python dasturlash tilida massivlar va ulardan foydalanish. Bir va ikki o’lchovli massivlar bilan ishlash.


Massivlar bilan ishlash

  1. A(n) massiv berilgan. Uning elementlarining eng kattasi va eng kichigini topish algoritmi va dasturini tuzing

import numpy as np


n = int(input('n='))
array = [int(input(f'a[{i+1}]=')) for i in range(n)]
a = np.array(array)
print(a)
print(f'Max = {max(a)}')
print(f'Min = {min(a)}')

  1. A(n) massiv berilgan. Massiv elementlarini o‘sib borish tartibida joy-lashtirish algoritmi va dasturini tuzing.

import numpy as np


n = int(input('n='))
array = [int(input(f'a[{i+1}]=')) for i in range(n)]
a = np.array(array)
print(a)
print(sorted(a))

  1. A(n) massiv berilgan. Uning elementlarining eng kattasini 1-soni bilan, eng kichigini esa -1 soni bilan almashtirish algoritmi va dasturini tuzing.

import numpy as np


n = int(input('n='))
array = [int(input(f'a[{i+1}]=')) for i in range(n)]
a = np.array(array)
print(a)

max = max(a)


min = min(a)
for i in range(n):
if a[i]==max:
a[i]=1
if a[i]==min:
a[i]=-1
print(a)

  1. A(n) massiv berilgan. Uning elementlarining eng kattasi va eng kichigini hamda ularning o’rnini aniqlash algoritmi va dasturini tuzing.

import numpy as np


n = int(input('n='))
array = [int(input(f'a[{i+1}]=')) for i in range(n)]
a = np.array(array)
print(a)

max = a[0]


for i in range(n):
if maxmax = a[i]
k = i+1

min = a[0]


for i in range(n):
if min>a[i]:
min = a[i]
m = i+1
print(f'Max index = {k}\n'
f'Min index = {m}')

  1. A(n) massiv berilgan. Uning manfiy, musbat va nol elementlari sonini aniqlash algoritmi va dasturini tuzing.

import numpy as np


n = int(input('n='))
array = [int(input(f'a[{i+1}]=')) for i in range(n)]
a = np.array(array)
print(a)

musbat = 0


manfiy = 0
nol = 0
for i in range(n):
if a[i]>0:
musbat += 1
elif a[i]<0:
manfiy += 1
else:
nol += 1

print(f'Musbat elementlari soni: {musbat}\n'


f'Manfiy elementlari soni: {manfiy}\n'
f'Nol elementlari soni: {nol}')

  1. x1, x2, …,x55 massiv berilgan: x1(x2+x3)  (x4+x5+x6) … (x46+x47+…,x55) ni hisoblash algoritmi va dasturini tuzing.

n = 10
k = 1


massiv = []
p = 1
for i in range(1, n + 1):
qator = []
for j in range(i):
qator.append(k)
k += 1
massiv.append(qator)
p *= sum(qator)
for qator in massiv:
print(qator)
print(p)

  1. x1, x2, …,xn massiv berilgan. Uning toq elementlaridan U, juft element-laridan Z massiv hosil qilish algoritmi va dasturini tuzing.

import numpy as np


import random
royxat = []
for i in range(10):
e = random.randint(1,20)
royxat.append(e)
x = np.array(royxat)
print(x)

toq = []
juft = []


for i in x:
if i%2==0:
juft.append(i)
else:
toq.append(i)
print(set(toq))
print(set(juft))

  1. A(n) massiv berilgan. Uning birinchi va oxirgi elementlari o’rnini almashtirish algoritmi va dasturini tuzing.

def almashtirish(massiv):


if len(massiv) < 2:
return massiv

birinchi_element = massiv[0]


oxirgi_element = massiv[-1]

massiv[0] = oxirgi_element


massiv[-1] = birinchi_element

return massiv


import numpy as np


import random
royxat = []
for i in range(10):
e = random.randint(1,20)
royxat.append(e)
x = np.array(royxat)
print(x)
natija = almashtirish(x)
print(natija)

  1. A(n) massiv berilgan. Uning manfiy elementlarining eng kattasini aniqlash algoritmi va dasturini tuzing.

import numpy as np
import random
n = int(input('n='))
a = []
for i in range(n):
e = random.randint(-10,5)
a.append(e)
massiv_a = np.array(a)
print(massiv_a)
b = []
for i in range(n):
if massiv_a[i] < 0:
b.append(massiv_a[i])
print(max(b))

  1. va ikkita massivning elementlari butun sonlarda iborat. massivning toq elementlardan tashkil topgan, lekin massivning elementi bo‘lmagan massivni hosil qiling.

import numpy as np


import random
n = int(input('n='))
a = []
for i in range(n):
e = random.randint(1,20)
a.append(e)
massiv_a = np.array(a)
b = []
for i in range(n):
e = random.randint(1,20)
b.append(e)
massiv_b = np.array(b)
print(massiv_a)
print(massiv_b)
c = []
for i in range(n):
if (a[i] % 2 != 0) and (a[i] not in b):
c.append(a[i])
massiv_c = np.array(c)
print(massiv_c)
Download 67.5 Kb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling