|
| 1 | +def is_palindrome(word): |
| 2 | + return word == word[::-1] |
| 3 | + |
| 4 | + |
| 5 | +def contains_word(word, text): |
| 6 | + if is_palindrome(word): |
| 7 | + return text.count(word) |
| 8 | + |
| 9 | + return text.count(word) + text[::-1].count(word) |
| 10 | + |
| 11 | + |
| 12 | +def build_matrix(rows, cols): |
| 13 | + matrix = [] |
| 14 | + rows_inputted = 0 |
| 15 | + print('Enter matrix: ') |
| 16 | + |
| 17 | + while rows_inputted < rows: |
| 18 | + row_input = input() |
| 19 | + |
| 20 | + row = row_input.strip().split(' ') |
| 21 | + if len(row) != cols: |
| 22 | + return 'Wrong input.' |
| 23 | + |
| 24 | + matrix.append(row) |
| 25 | + rows_inputted += 1 |
| 26 | + |
| 27 | + return matrix |
| 28 | + |
| 29 | + |
| 30 | +def word_occurances_in_rows(matrix, word): |
| 31 | + word_occurances = 0 |
| 32 | + |
| 33 | + for row in matrix: |
| 34 | + word_occurances += contains_word(word, ''.join(row)) |
| 35 | + |
| 36 | + return word_occurances |
| 37 | + |
| 38 | + |
| 39 | +def word_occurances_in_cols(matrix, word): |
| 40 | + word_occurances = 0 |
| 41 | + cols_count = len(matrix[0]) |
| 42 | + |
| 43 | + for i in range(cols_count): |
| 44 | + column = [] |
| 45 | + for row in matrix: |
| 46 | + column.append(row[i]) |
| 47 | + |
| 48 | + word_occurances += contains_word(word, ''.join(column)) |
| 49 | + |
| 50 | + return word_occurances |
| 51 | + |
| 52 | + |
| 53 | +def word_occurances_in_right_diagonals(matrix, word): |
| 54 | + word_occurances = 0 |
| 55 | + rows_count = len(matrix) |
| 56 | + cols_count = len(matrix[0]) |
| 57 | + |
| 58 | + for c in range(rows_count + cols_count - 1): |
| 59 | + diagonal = [] |
| 60 | + for i in range(rows_count): |
| 61 | + for j in range(cols_count): |
| 62 | + if i + j == c: |
| 63 | + diagonal.append(matrix[i][j]) |
| 64 | + |
| 65 | + word_occurances += contains_word(word, ''.join(diagonal)) |
| 66 | + |
| 67 | + return word_occurances |
| 68 | + |
| 69 | + |
| 70 | +def word_occurances_in_left_diagonals(matrix, word): |
| 71 | + word_occurances = 0 |
| 72 | + rows_count = len(matrix) |
| 73 | + cols_count = len(matrix[0]) |
| 74 | + |
| 75 | + for c in range(1 - cols_count, rows_count): |
| 76 | + diagonal = [] |
| 77 | + for i in range(rows_count): |
| 78 | + for j in reversed(range(cols_count)): |
| 79 | + if j - i == c: |
| 80 | + diagonal.append(matrix[i][j]) |
| 81 | + |
| 82 | + word_occurances += contains_word(word, ''.join(diagonal)) |
| 83 | + |
| 84 | + return word_occurances |
| 85 | + |
| 86 | + |
| 87 | +def word_counter(): |
| 88 | + word = input('Enter word: ') |
| 89 | + size = input('Enter matrix size (format: N M): ') |
| 90 | + |
| 91 | + n = int(size.split(' ')[0]) |
| 92 | + m = int(size.split(' ')[1]) |
| 93 | + |
| 94 | + if len(word) > min([n, m]): |
| 95 | + return 'Invalid number of rows or columns!' |
| 96 | + |
| 97 | + matrix = build_matrix(n, m) |
| 98 | + |
| 99 | + word_occurances = word_occurances_in_rows(matrix, word) |
| 100 | + word_occurances += word_occurances_in_cols(matrix, word) |
| 101 | + word_occurances += word_occurances_in_right_diagonals(matrix, word) |
| 102 | + word_occurances += word_occurances_in_left_diagonals(matrix, word) |
| 103 | + |
| 104 | + return word_occurances |
| 105 | + |
| 106 | + |
| 107 | +print(word_counter()) |
0 commit comments