• 1
    Input and Output Data
    • Tasks
  • 2
    Conditions
    • Tasks
  • 3
    For Loop
    • Tasks
  • 4
    Strings
    • Tasks
  • 5
    While Loop
    • Tasks
  • 6
    Lists
    • Tasks
  • 7
    Two-Dimensional Arrays
    • Tasks
  • 8
    Dictionaries
    • Tasks
  • 9
    Sets
    • Tasks
  • 10
    Functions and Recursion
    • Tasks
  • к

Занятие 6. Lists

Difficulty level:

Task«Symmetry of the matrix»

💻 Python
You are developing a program for a social network that analyzes the friendship table. Table 1 means that people are friends, 0 are not. The table should be symmetrical, since friendship is a mutual phenomenon (if a friend b, then b friend a). The program checks the correctness of the table.

Input format

constructing the matrix, where each line contains numbers 0 and 1, separated by spaces
The input ends with the word end

Output format

Yes-if the table is symmetrical
no-if the table is a table asymmetric

Example

Input

1 0 1
0 1 0
1 0 1
end

Output

Yes

Hint

There will be no clue here, decide for yourself!

main.py
Test 1
Test 2
Test 3
Test 4
Test 5
Test 6
Test 7
Test 8
Test 9
Test 10
Developer’s solution
# Создаем пустой список для хранения матрицы
matrix = []

# Считываем строки матрицы
while True:
    line = input()
    if line == 'end':  # Если встретили 'end', прекращаем ввод
        break
    # Преобразуем строку в список чисел и добавляем в матрицу
    row = [int(x) for x in line.split()]
    matrix.append(row)

# Проверяем симметричность
is_symmetric = True
for i in range(len(matrix)):
    for j in range(len(matrix)):
        if matrix[i][j] != matrix[j][i]:  # Сравниваем симметричные элементы
            is_symmetric = False
            break
    if not is_symmetric:
        break

# Выводим результат
print("YES" if is_symmetric else "NO")

🎉 Congratulations! 🎉

You did an excellent job with the task! It was a challenging problem, but you found the correct solution. You are one step closer to mastering programming! Keep up the good work, because every stage you pass makes you even stronger.

AD

Advertisement

red-snake blue-snake green-snake

Running your code...

Помощник ИИ

Привет! Я твой помощник по программированию. Задавай любые вопросы по Python, я могу рассказать о функциях, методах, обьяснить то, что тебе не понятно, а так же о текущей задаче!