Вопрос задан 18.06.2023 в 10:55. Предмет Информатика. Спрашивает Сафина Сюмбель.

Задачи: №1 Дан двумерный список (матрица) : N*M элементов. Найти в матрице сумму всех элементов в

четных столбцах. №2 Дан двумерный список (матрица) : N*N элементов. Найти в матрице сумму элементов главной и побочной диагонали, а также их абсолютную разность. №3 Дан двумерный список (матрица) : N*M элементов. Поменять в матрице столбец с самым большим по значению элементом на столбец с самым маленьким элементов. Вывести результат/ PYTHON, пожалуйста, помогите, не понимаю этого
0 0
Перейти к ответам

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

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

# Задача 1

n = int(input())

m = int(input())

arr = [[int(input()) for _ in range(m)] for _ in range(n)]

res = 0

for i in arr:

   for j in range(len(i)):

       if j % 2 != 0:

           res += i[j]

print(res)

# Задача 2

n = int(input())

arr = [[int(input()) for _ in range(n)] for _ in range(n)]

diag_1 = 0

diag_2 = 0

for i in range(n):

   diag_1 += arr[i][i]

   diag_2 += arr[i][n - i - 1]

print(diag_1 + diag_2)

print(abs(diag_1 - diag_2))

# Задача 3

n = int(input())

m = int(input())

from random import randint

arr = [[int(randint(1, 10)) for _ in range(m)] for _ in range(n)]

for i in arr:

   print(i)

print()

results = {}

for i in arr:  # Добавление элементов в словарь

   for j in range(len(i)):

       if j not in results:

           results[j] = i[j]

       else:

           results[j] += i[j]

arr_max = 0, 0

arr_min = 0, 999999

for key, val in results.items():  # Нахождение наибольшего и наименьшего элемента

   if val > arr_max[1]:

       arr_max = key, val

   if val < arr_min[1]:

       arr_min = key, val

max_values = [[i[j] for j in range(len(i)) if j == arr_max[0]][0] for i in arr]

min_values = [[i[j] for j in range(len(i)) if j == arr_min[0]][0] for i in arr]

for i in arr:

   for j in range(len(i)):

       if j == arr_min[0]:

           i[j] = max_values[0]

           del max_values[0]

       elif j == arr_max[0]:

           i[j] = min_values[0]

           del min_values[0]

for i in arr:

   print(i)


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

Task 1: Finding the sum of elements in even columns of a matrix

To find the sum of all elements in even columns of a given matrix, you can follow these steps:

1. Iterate through each column of the matrix. 2. Check if the column index is even. 3. If the column index is even, iterate through each row of the matrix and add the element at that position to a running sum. 4. Return the final sum.

Here's a Python code implementation for this task:

```python def sum_of_elements_in_even_columns(matrix): rows = len(matrix) columns = len(matrix[0]) total_sum = 0

for col in range(columns): if col % 2 == 0: for row in range(rows): total_sum += matrix[row][col]

return total_sum ```

Please note that the `matrix` parameter should be a 2D list representing the matrix.

Task 2: Finding the sum and absolute difference of elements in the main and secondary diagonals of a matrix

To find the sum and absolute difference of elements in the main and secondary diagonals of a given square matrix, you can follow these steps:

1. Initialize variables `main_sum` and `secondary_sum` to 0. 2. Iterate through each row and column index of the matrix. 3. If the row index is equal to the column index, add the element at that position to `main_sum`. 4. If the row index is equal to the difference between the total number of rows and the column index minus 1, add the element at that position to `secondary_sum`. 5. Calculate the absolute difference between `main_sum` and `secondary_sum`. 6. Return `main_sum`, `secondary_sum`, and the absolute difference.

Here's a Python code implementation for this task:

```python def sum_and_difference_of_diagonals(matrix): size = len(matrix) main_sum = 0 secondary_sum = 0

for i in range(size): for j in range(size): if i == j: main_sum += matrix[i][j] if i == size - j - 1: secondary_sum += matrix[i][j]

difference = abs(main_sum - secondary_sum) return main_sum, secondary_sum, difference ```

Please note that the `matrix` parameter should be a square 2D list representing the matrix.

Task 3: Swapping the column with the largest value with the column with the smallest value in a matrix

To swap the column with the largest value with the column with the smallest value in a given matrix, you can follow these steps:

1. Find the column index with the largest value and the column index with the smallest value. 2. Iterate through each row of the matrix and swap the elements at the found column indices. 3. Return the modified matrix.

Here's a Python code implementation for this task:

```python def swap_columns_with_extreme_values(matrix): rows = len(matrix) columns = len(matrix[0]) max_column_index = 0 min_column_index = 0

for col in range(1, columns): if max(matrix[row][col] for row in range(rows)) > max(matrix[row][max_column_index] for row in range(rows)): max_column_index = col if min(matrix[row][col] for row in range(rows)) < min(matrix[row][min_column_index] for row in range(rows)): min_column_index = col

for row in range(rows): matrix[row][max_column_index], matrix[row][min_column_index] = matrix[row][min_column_index], matrix[row][max_column_index]

return matrix ```

Please note that the `matrix` parameter should be a 2D list representing the matrix.

I hope this helps! Let me know if you have any further questions.

0 0

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

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

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

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