Cookie Consent by Free Privacy Policy Generator 📌 Uncommon matrix


✅ Uncommon matrix


💡 Newskategorie: Programmierung
🔗 Quelle: dev.to

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]

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"
('')

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")

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

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
...

✅ Uncommon matrix


📈 35.61 Punkte

✅ Common and Uncommon types of SQL Injection


📈 24.16 Punkte

✅ Emotet’s Uncommon Approach of Masking IP Addresses


📈 24.16 Punkte

✅ VMs Help Ransomware Attackers Evade Detection, but It's Uncommon


📈 24.16 Punkte

✅ Paradise Ransomware Distributed via Uncommon Spam Attachment


📈 24.16 Punkte

✅ 5 Uncommon Advices from one beginner coder to another beginner coder!


📈 24.16 Punkte

✅ The Uncommon Becomes Ordinary: 4 Trends That Defined Data Breaches in 2019


📈 24.16 Punkte

✅ HPR4043: Uncommon Commands, Episode 1


📈 24.16 Punkte

✅ Microsoft Warns of Uncommon Windows 10 Thunderbolt Issue


📈 24.16 Punkte

✅ New Mirai Variant Employs Uncommon Tactics to Distribute Malware


📈 24.16 Punkte

✅ Why GNU/Linux Viruses are fairly uncommon - GNU Project


📈 24.16 Punkte

✅ Uncommon infection methods—part 2


📈 24.16 Punkte

✅ Cryptocurrency Miners Not Uncommon on Industrial Systems


📈 24.16 Punkte

✅ 'No All Nighters' And Other Uncommon Tips For Running A Successful Hackathon


📈 24.16 Punkte

✅ Statistical Analysis to Detect Uncommon Code


📈 24.16 Punkte

✅ Mozilla Adds Download Protections in Firefox for PUPs and Uncommon Downloads


📈 24.16 Punkte

✅ Uncommon infection and malware propagation methods


📈 24.16 Punkte

✅ Mozilla Adds Download Protections in Firefox for PUPs and Uncommon Downloads


📈 24.16 Punkte

✅ Matrix 5: Mehr Matrix ist ein Grund zur Freude!


📈 22.91 Punkte

✅ FOSDEM 2023: Matrix 2.0 — How we're making Matrix go voom!


📈 22.91 Punkte

✅ News: Matrix 1.0 und die »Matrix.org Foundation« vorgestellt


📈 22.91 Punkte

✅ Matrix Live S09E21 — Matrix & Element demos!


📈 22.91 Punkte

✅ Google Flights Matrix - Google Matrix Airfare Search (ITA Software) Deutsch


📈 22.91 Punkte

✅ Matrix 1.0 und die »Matrix.org Foundation« vorgestellt


📈 22.91 Punkte

✅ Matrix Live S09E20 — Moodle adopts Matrix


📈 22.91 Punkte

✅ Matrix: Would it be reasonable to run a matrix home server in a basic VPS?


📈 22.91 Punkte

✅ Introducing Matrix 1.0 and the Matrix.org Foundation


📈 22.91 Punkte

✅ Matrix Live S09E18 — Matrix crypto update. Able to decrypt?


📈 22.91 Punkte

✅ matrix-media-repo up to 1.2.6 on Matrix resource consumption


📈 22.91 Punkte

✅ Matrix Live S09E17 — Element X, Call and Matrix Auth Service updates


📈 22.91 Punkte

✅ Arch Conf 2020 - Enter the Matrix: Install your own Matrix server on Arch Linux


📈 22.91 Punkte

✅ Matrix Live S09E14 — The Matrix Elm SDK


📈 22.91 Punkte

✅ Combating abuse in Matrix - without backdoors (Matrix blog)


📈 22.91 Punkte

✅ Matrix Live S09E07 — Element X and Matrix Authentication Service


📈 22.91 Punkte











matomo

Datei nicht gefunden!