DEV Community

Simon Green
Simon Green

Posted on

Strong counting

Weekly Challenge 277

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: Count Common

Task

You are given two array of strings, @words1 and @words2.

Write a script to return the count of words that appears in both arrays exactly once.

My solution

In my day job, I usually don't use TDD for writing new code. Things are different when I work on The Weekly Challenge, as there are examples that can be used to test success. Once I've pull the code into my local repo and before I write any code, I create test.py with the expected output. I then do my code, and ensure the tests past. And occasionally I find out my code doesn't pass the tests. Today was such an example.

My original solution for this was to convert the lists into sets and perform an intersection of them. Simple one liner. However the third test failed because the words 'is' appears more than once.

In the end I took a different approach. I take each word for from first list and check that it appears once (and only once) in all the lists. While that task takes two arrays of strings, my solution will take many.

def count_common(*words_list: list[list]) -> int:
    count = 0

    for word in set(words_list[0]):
        for words in words_list:
            if words.count(word) != 1:
                break
        else:
            count+=1

    return count
Enter fullscreen mode Exit fullscreen mode

The only funky thing here (from a Perl perspective) is the for ... else feature. In Python, the else part is only executed when the loop is not broken out of.

For the command line input a take a strings of space-separated values to make each list.

Examples

$ ./ch-1.py "Perl is my friend" "Perl and Raku are friend"
2

$ ./ch-1.py "Perl and Python are very similar" "Python is top in guest languages"
1

$ ./ch-1.py "Perl is imperative Lisp is functional" "Crystal is similar to Ruby"
Enter fullscreen mode Exit fullscreen mode

Task 2: Strong Pair

Task

You are given an array of integers, @ints.

Write a script to return the count of all strong pairs in the given array. A pair of integers x and y is called strong pair if it satisfies: 0 < |x - y| < min(x, y).

My solution

This doesn't need much explanation, I simply loop through the unique positions, and count the number of times the condition is met.

def strong_pair(ints: list) -> int:
    ints = list(set(ints))
    count = 0

    for i in range(len(ints)-1):
        for j in range(i+1, len(ints)):
            if abs(ints[i]-ints[j]) < min(ints[i], ints[j]):
                count += 1

    return count
Enter fullscreen mode Exit fullscreen mode

Examples

$ ./ch-2.py 1 2 3 4 5
4

$ ./ch-2.py 5 7 1 7
1
Enter fullscreen mode Exit fullscreen mode

Top comments (0)