DEV Community

Simon Green
Simon Green

Posted on

Uncommon matrix

Weekly Challenge 266

Each week Mohammad S. Anwar sends out The Weekly Challenge, a chance for all of us to come up with solutions to two weekly tasks. My solutions are written in Python first, and then converted to Perl. It's a great way for us all to practice some coding.

Challenge, My solutions

Task 1: Uncommon Words

Task

You are given two sentences, $line1 and $line2.

Write a script to find all uncommmon words in any order in the given two sentences. Return ('') if none found.

A word is uncommon if it appears exactly once in one of the sentences and doesn’t appear in other sentence.

My solution

For this task, I took word to mean things separated by spaces. This is rather simplified of a real world sentence, which usually would be terminated by a punctuation character. I also don't convert the characters to a single case. Therefore 'Mango' and 'mango' would be considered different words.

With that in mind, I calculate the frequency of each word and return words that only occur once. In Python 3.7 or greater, the list will be in the order that they appear. In older Python and Perl, the list order is not deterministic.

While the task specifies two sentences, this script can take as many lines as desired.

def uncommon_words(*strings) -> list:
    for string in strings:
        for word in string.split(' '):
            freq[word] += 1

    return [w for w in freq if freq[w] == 1]
Enter fullscreen mode Exit fullscreen mode

Examples

$ ./ch-1.py "Mango is sweet" "Mango is sour"
('sweet', 'sour')

$ ./ch-1.py "Mango Mango" "Orange"
('Orange')

$ ./ch-1.py "Mango is Mango" "Orange is Orange"
('')
Enter fullscreen mode Exit fullscreen mode

Task 2: X Matrix

Task

You are given a square matrix, $matrix.

Write a script to find if the given matrix is X Matrix.

A square matrix is an X Matrix if all the elements on the main diagonal and antidiagonal are non-zero and everything else are zero.

My solution

For this task, I take the input as a JSON array. The first step I take is to check that the array is square. Each column must have the same number of integers as the number of rows.

rows = len(matrix)

for row in range(rows):
    if len(matrix[row]) != rows:
        raise ValueError("Please specify a square matrix")
Enter fullscreen mode Exit fullscreen mode

I then check that every cell is the correct values (non-zero for the cross diagonal or zero otherwise). For a give row (0 to one less than the length of the square), a non zero value is required for column row or rows - 1 - row. I return False as soon as the an invalid value is found.

If all cells have the correct value, I return True.

for row in range(rows):
    for col in range(rows):
        if col == row or col == rows - 1 - row:
            if matrix[row][col] == 0:
                return False
        elif matrix[row][col] != 0:
            return False

return True
Enter fullscreen mode Exit fullscreen mode

Examples

$ ./ch-2.py "[ [1, 0, 0, 2], [0, 3, 4, 0], [0, 5, 6, 0], [7, 0, 0, 1] ]"
true

$ ./ch-2.py "[ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]"
false

$ ./ch-2.py "[ [1, 0, 2], [0, 3, 0], [4, 0, 5] ]"
true
Enter fullscreen mode Exit fullscreen mode

Top comments (0)