Ad
Fundamentals
Strings
Code
Diff
  • reverse_string = lambda s: s[::-1]
    • def reverse_string(s):
    • return s[::-1]
    • reverse_string = lambda s: s[::-1]
Matrix

Determine whether a given square matrix of order n is a "magic" matrix. A magic matrix is a matrix in which the sum of numbers in each row, column and diagonal is the same.

def is_magic_square(matrix):
    n = len(matrix)

    diagonal_sum = sum(matrix[i][i] for i in range(n))
    anti_diagonal_sum = sum(matrix[i][n - 1 - i] for i in range(n))
    if diagonal_sum != anti_diagonal_sum:
        return False

    for i in range(n):
        row_sum = sum(matrix[i])
        col_sum = sum(matrix[j][i] for j in range(n))
        if row_sum != diagonal_sum or col_sum != diagonal_sum:
            return False

    return True
Fundamentals
Logic
Code
Diff
  • def identify_data_type(n):
        return n.__class__.__name__
    
    • identify_data_type=lambda i:type(i).__name__
    • def identify_data_type(n):
    • return n.__class__.__name__
Code
Diff
  • def Greeting(n, rank='', formal=False):
        determinant = f'{rank} {n}' if formal and rank else f'{n}'
        return f"Hello, {determinant}." if formal else f"Hey, {determinant}!"
    • def Greeting(n, rank='', formal=0):
    • determinant = f'{rank} {n}' if formal and rank else f'{n}'
    • return f"Hello, {determinant}." if formal else f"Hey, {determinant}!"
    • def Greeting(n, rank='', formal=False):
    • determinant = f'{rank} {n}' if formal and rank else f'{n}'
    • return f"Hello, {determinant}." if formal else f"Hey, {determinant}!"
Arrays
Sorting
Code
Diff
  • import re
    
    def longest_words(word_list, num_words):
        if not isinstance(word_list, list) or not isinstance(num_words, int) or num_words < 0:
            return 'Invalid Parameters'
    
        valid_words = [re.sub(r'[^A-Za-z]', '', word) for word in word_list if word and re.sub(r'[^A-Za-z]', '', word)]
    
        return sorted(valid_words, key=len, reverse=True)[:num_words] if num_words <= len(valid_words) else 'Invalid Parameters'
    
    • import re
    • def longest_words(array, num):
    • valid_words = [re.sub(r'[^A-Za-z]', '', word) for word in array if word and re.sub(r'[^A-Za-z]', '', word)]
    • return sorted(valid_words, key=len, reverse=True)[:num] if num <= len(valid_words) else 'Invalid Parameters'
    • def longest_words(word_list, num_words):
    • if not isinstance(word_list, list) or not isinstance(num_words, int) or num_words < 0:
    • return 'Invalid Parameters'
    • valid_words = [re.sub(r'[^A-Za-z]', '', word) for word in word_list if word and re.sub(r'[^A-Za-z]', '', word)]
    • return sorted(valid_words, key=len, reverse=True)[:num_words] if num_words <= len(valid_words) else 'Invalid Parameters'