Вопрос задан 18.06.2023 в 03:22. Предмет Информатика. Спрашивает Оганесян Виктория.

Решить задачи в IDLE Python 4. Найти разность количества положительных и отрицательных элементов

массива, количество нулей в массива 5. Найти среднее арифметическое элементов массива, которые больше 180 (длина массива и его элементы вводятся пользователем) 6. Найти сумму четных элементов массива, которые оканчиваются на 6
0 0
Перейти к ответам

Ответы на вопрос

Внимание! Ответы на вопросы дают живые люди. Они могут содержать ошибочную информацию, заблуждения, а также ответы могут быть сгенерированы нейросетями. Будьте внимательны. Если вы уверены, что ответ неверный, нажмите кнопку "Пожаловаться" под ответом.
Отвечает Астапов Александр.

Ответ:

ВСЕ элементы массивы вводятся на одной строке через пробел

4:

list = [int(i) for i in input().split()]

otr, pol, zero = 0, 0, 0

for i in list:

if i > 0:

pol += 1

elif i < 0:

otr += 1

else:

zero += 1

print(pol - otr, zero)

5:

длина массива это кол-во его элементов, поэтому я убрал ввод длины

list = [int(i) for i in input().split() if int(i) > 180]

print(sum(list) / len(list))

6:

list = [int(i) for i in input().split() if int(i) % 2 == 0 and int(i) % 10 == 6]

print(sum(list))

0 0
Отвечает нейросеть ChatGpt. Будьте внимательны, ответы нейросети могут содержать ошибочные или выдуманные данные.

Task 4: Finding the Difference between the Number of Positive and Negative Elements in an Array

To find the difference between the number of positive and negative elements in an array, you can follow these steps in Python:

1. Initialize two variables, `positive_count` and `negative_count`, to keep track of the number of positive and negative elements, respectively. 2. Iterate through each element in the array. 3. If the element is greater than 0, increment the `positive_count` variable by 1. 4. If the element is less than 0, increment the `negative_count` variable by 1. 5. Calculate the difference between `positive_count` and `negative_count`.

Here's an example code snippet that demonstrates this:

```python def find_difference(array): positive_count = 0 negative_count = 0

for element in array: if element > 0: positive_count += 1 elif element < 0: negative_count += 1

difference = positive_count - negative_count return difference

# Example usage array = [1, -2, 3, -4, 5] result = find_difference(array) print("The difference between the number of positive and negative elements is:", result) ```

Task 5: Finding the Number of Zeros in an Array

To find the number of zeros in an array, you can use the `count()` method in Python. Here's how you can do it:

1. Initialize a variable, `zero_count`, to keep track of the number of zeros. 2. Use the `count()` method to count the number of occurrences of 0 in the array.

Here's an example code snippet that demonstrates this:

```python def find_zero_count(array): zero_count = array.count(0) return zero_count

# Example usage array = [1, 0, 2, 0, 3, 0] result = find_zero_count(array) print("The number of zeros in the array is:", result) ```

Task 6: Finding the Average of Elements Greater than 180 in an Array

To find the average of elements greater than 180 in an array, you can follow these steps in Python:

1. Initialize two variables, `sum_elements` and `count`, to keep track of the sum of elements and the count of elements greater than 180, respectively. 2. Iterate through each element in the array. 3. If the element is greater than 180, add it to the `sum_elements` variable and increment the `count` variable by 1. 4. Calculate the average by dividing the `sum_elements` by the `count`.

Here's an example code snippet that demonstrates this:

```python def find_average(array): sum_elements = 0 count = 0

for element in array: if element > 180: sum_elements += element count += 1

if count > 0: average = sum_elements / count return average else: return 0

# Example usage array = [150, 200, 170, 220, 190] result = find_average(array) print("The average of elements greater than 180 in the array is:", result) ```

Task 7: Finding the Sum of Even Elements Ending in 6 in an Array

To find the sum of even elements ending in 6 in an array, you can follow these steps in Python:

1. Initialize a variable, `sum_even_ending_six`, to keep track of the sum of even elements ending in 6. 2. Iterate through each element in the array. 3. Check if the element is even and ends with 6 by using the modulo operator `%`. 4. If the condition is satisfied, add the element to the `sum_even_ending_six` variable. 5. Return the `sum_even_ending_six` variable.

Here's an example code snippet that demonstrates this:

```python def find_sum_even_ending_six(array): sum_even_ending_six = 0

for element in array: if element % 2 == 0 and str(element)[-1] == '6': sum_even_ending_six += element

return sum_even_ending_six

# Example usage array = [16, 26, 36, 46, 56] result = find_sum_even_ending_six(array) print("The sum of even elements ending in 6 in the array is:", result) ```

Please note that the above code snippets assume that the array and its elements are provided by the user.

0 0

Похожие вопросы

Топ вопросов за вчера в категории Информатика

Последние заданные вопросы в категории Информатика

Задать вопрос