Уровень В. Напишите программу, которая заполняет массив из 10 элементов случайными числами на отрезке [-2; 2] и находит произведение ненулевых элементов.
массив: -2 0 2 1 -1 2 0 1 2 -1

Уровень С. Напишите программу, которая заполняет массив из 10 элементов случайными числами на отрезке [100; 1000] и находит отдельно сумму элементов в первой и второй половинах массива.
Пример: Массив: 154 896 724 104 327 903 356 539 531 778 ​

Ответы

Ответ дал: Аноним
0

Відповідь:

B.

import random

# Generate an array of 10 random numbers in the interval [-2; 2]

array = [random.uniform(-2,2) for _ in range(10)]

# Find the product of the non-zero elements

product = 1

for element in array:

   if element != 0:

       product *= element

print("The array is", array)

print("The product of the non-zero elements is", product)

С.

Вы можете использовать следующий код, чтобы сгенерировать массив из 10 случайных чисел в интервале [100; 1000] и найти сумму первой и второй половин массива по отдельности:

import random

# Generate an array of 10 random numbers in the interval [100; 1000]

array = [random.randint(100,1000) for _ in range(10)]

# Find the sum of the first and second halves of the array separately

half_length = len(array) // 2

first_half_sum = 0

for element in array[:half_length]:

   first_half_sum += element

second_half_sum = 0

for element in array[half_length:]:

   second_half_sum += element

print("The array is", array)

print("The sum of the first half of the array is", first_half_sum)

print("The sum of the second half of the array is", second_half_sum)

Пояснення:

Sorry for the English, I hope I helped you in some way!

If something does not copy, I have attached below how it should look in the input line

Have a great day!

Приложения:

ppiratiseoyr3st: неплохо
Аноним: Thanks for your review, I'll keep trying!!
Вас заинтересует