<jupyter_start><jupyter_text>Problema Matriz Dado uma matriz MxN de números inteiros, busque um número na matriz toda e retorne a posição deste elemento. Retorne também os vizinhos deste elemento.<jupyter_code>import numpy as np

# matrix with m rows and n columns
def create_matrix(m, n, min_, max_):
    return np.random.random_integers(min_, max_, (m, n))
matrix = create_matrix(3, 4, 1, 23)
matrix
matrix[0][0]
matrix[1][2]  # row, column
matrix.shape
def search_element(x, matrix):
    rows, columns = matrix.shape
    list_index_element = list()
    
    for r in range(rows):
        for c in range(columns):
            if x == matrix[r][c]:
                list_index_element.append((r, c))
    
    return list_index_element
search_element(2, matrix)
search_element(8, matrix)
def return_neighbors(x, m, n, matrix):
    
    list_index_elements = search_element(x, matrix)
    
    list_neighbors = []
    
    for i in list_index_elements:
        
        # Upper
        if i[0]-1 >= 0: 
            list_neighbors.append((i[0]-1, i[1]))
        else:
            list_neighbors.append(0)
        
        # Down
        if i[0]+1 <= m-1: 
            list_neighbors.append((i[0]+1, i[1]))
        else:
            list_neighbors.append(0)
        
        # Right
        if i[1]-1 >= 0:
            list_neighbors.append((i[0], i[1]-1))
        else:
            list_neighbors.append(0)
        
        # Left
        if i[1]+1 <= n-1:
            list_neighbors.append((i[0], i[1]+1))
        else:
            list_neighbors.append(0)
    
    return list_index_elements, list_neighbors # upper, down, left, right
x = 2
m = 3
n = 4

return_neighbors(x, m, n, matrix)
x = 8
m = 3
n = 4

return_neighbors(x, m, n, matrix)
x = 6
m = 3
n = 4

return_neighbors(x, m, n, matrix)
x = 17

return_neighbors(x, m, n, matrix)
list_index, list_neighbors = return_neighbors(12, m, n, matrix)
list_index
list_neighbors
def find_element_and_print(list_index, list_neighbors, matrix):

    for i in list_index:
        print("Position:", i)
        counter = 0
        for j in list_neighbors:
            if j == 0:
                counter += 1
            else:
                if counter == 0:
                    print("Upper:", matrix[j[0], j[1]])
                if counter == 1:
                    print("Down:", matrix[j[0], j[1]])
                if counter == 2:
                    print("Right:", matrix[j[0], j[1]])
                if counter == 3:
                    print("Left:", matrix[j[0], j[1]])
        counter = 0
find_element_and_print(list_index, list_neighbors, matrix)
matrix
def search_duplicates(matrix):
    
    list_duplicates = []
    
    rows, columns = matrix.shape
    
    for i in range(rows):
        for j in range(columns):
            
            a = matrix[i][j]
            
            for r in range(i, rows):
                for c in range(columns):
                    if a == matrix[r][c] and (i != r or j != c):
                        list_duplicates.append(a)

    return list(set(list_duplicates))
search_duplicates(matrix)<jupyter_output><empty_output><jupyter_text>Dado um arquivo de texto com palavras, uma em cada linha, ache os palíndromos<jupyter_code>def find_palindromes(file):
    list_palindromes= []
    
    with open(file, "r") as a_file:
        for line in a_file:
            stripped_line = line.strip()
            if str(stripped_line) == str(stripped_line[::-1]) and len(str(stripped_line)) > 2:
                list_palindromes.append(stripped_line)
                
    return list_palindromes
# https://github.com/thoughtworks/dadoware/blob/master/7776palavras.txt
find_palindromes('7776palavras.txt')
# https://github.com/dwyl/english-words/blob/master/words_alpha.txt
file = 'words_alpha.txt'
find_palindromes(file)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11644908616187989, 'fraction_numerical': 0.020365535248041775, 'mean_word_length': 2.189841798501249, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 12, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '444519', 'license': 'MIT', 'n_tokens_mistral': 1382, 'n_tokens_neox': 1303, 'n_words': 312, 'path': '100DOC/007.ipynb', 'repo_name': 'marcelomiky/python_code', 'size': '3830'}