Sort by

recency

|

593 Discussions

|

  • + 0 comments

    Here is HackerRank Matrix Script in Python solution - https://programmingoneonone.com/hackerrank-matrix-script-problem-solution-in-python.html

  • + 0 comments

    How is # and % alpha numeric? I do not understand.

  • + 1 comment
    import re
    
    def solve(matrix, n, m):
        string = ""
        # Here I use zip which transposes the matrix; that means it converts rows into columns
        # You can also use numpy
        # Using nested loops is not ideal in Python, so zip is used to avoid that
        matrix_T = list(zip(*matrix))
      
        for i in matrix_T:
            string += "".join(list(i))
        
        clear = re.sub(r'(?<=[a-zA-Z0-9_])[!@#$%&" "]+(?=[a-zA-Z0-9_])', ' ', string)
        
        print(clear)
    
  • + 2 comments

    I am confused about why this is not working. If anyone can help I'll apreceate.

    import math
    import os
    import random
    import re
    import sys
    
    if __name__ == '__main__':
        first_multiple_input = input().rstrip().split()
    
        n = int(first_multiple_input[0])
    
        m = int(first_multiple_input[1])
    
        matrix = []
    
        for _ in range(n):
            matrix_item = input()
            matrix.append(matrix_item)
    
        text = ''
        for j in range(m):
            for i in range(n):
                if j < len(matrix[i]):
                    text += matrix[i][j]
                else:
                    text += ' '
        result = ''
        i = 0
        while i < len(text):
            if text[i].isalnum():
                result += text[i]
                i += 1
            elif text[i].isspace():
                result += text[i]
                i += 1
            else:
                next_alnum = i + 1
                while next_alnum < len(text) and not text[next_alnum].isalnum():
                    next_alnum += 1
                
                if (i > 0 and text[i-1].isalnum() and 
                    next_alnum < len(text) and text[next_alnum].isalnum()):
                    result += ' '
                    i = next_alnum
                else:
                    result += text[i]
                    i += 1
        if result.strip() and result.strip()[0] == '#':
            result = result.rstrip() + ' @'
        
        print(result)
    
  • + 1 comment

    Here is a really simple but hopefully readable solution:

    #!/bin/python3
    
    import re
    n, m = map(int, input().split())        
    matrix = [ input() for _ in range(n) ] 
    
    # a very Pythonic way to transpose and flatten a list of iterables - barely readable but you know what it's doing
    transpose = ''.join([c for ls in zip(*matrix) for c in ls])
    
    # Lookaheads and Lookbehinds let us use a simple 'sub' to replace every sandwiched cluster of symbols.
    print(re.sub(r"(?<=\w)[!@#$%& ]+(?=\w)", " ", transpose))