Exploring The Weekly Challenge: Uncommon Words and Parentheses
A breakdown of solutions for The Weekly Challenge tackling uncommon words and outermost parentheses removal in Python and Perl.

Stock photo for illustration only, not from the actual event
- Mohammad S. Anwar sends out The Weekly Challenge with two tasks weekly.
- The first task requires returning a list of all uncommon words.
- The second task involves removing outermost parentheses from primitive strings.
- Solutions are written in Python first and then converted to Perl.
Every week, Mohammad S. Anwar sends out The Weekly Challenge, offering developers a chance to submit solutions to two weekly tasks. The author writes their solutions in Python first and then converts them to Perl, without using Copilot or other AI tools to generate the code, serving as a great practice for coding skills.
The first challenge requires writing a script to return a list of all uncommon words where the order is not important. The author begins with a Counter called word_freq, a specialized dictionary ideal for counting frequencies.

Stock photo for illustration only, not from the actual event
Taking one or more sentences as input, the script loops through each sentence, splits them by spaces, and increments the word_freq counter before returning all words with a frequency of exactly 1. Since Python 3.6, dictionaries maintain their insertion order, preserving the sequence from the supplied sentences. In contrast, Perl does not maintain the order of hashes, prompting the Perl solution to sort the unique words alphabetically.
Utilizing Python's collections.Counter is an idiomatic and highly efficient approach for frequency analysis tasks. It operates in O(n) time complexity, making text processing significantly cleaner and faster than manual dictionary counting loops.
The second challenge asks to write a script that returns the string after removing the outermost parentheses of every primitive string in the primitive decomposition of a given string. The function starts by verifying that the input string strictly contains only opening and closing parentheses using a regular expression.
Two variables are then established: output to store the returned string, and open_count to track unclosed open parentheses. The function loops through each character, incrementing or decrementing open_count, while validating that closing parentheses never precede opening ones and that all open parentheses are properly closed by the end of the string.
Source: Dev.to
Found something wrong in this article? Report an issue with this article
Comments
Leave a Comment