Urgent.News

What's breaking now, across thousands of outlets.

Tech

Weekly Challenge: Rearranging Atoms

Weekly Challenge 387 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. Unless otherwise stated, Copilot (and other AI tools) have NOT been used to generate the solution. It's a great way for us all to practice some coding. Challenge , My solutions…

Weekly Challenge 387 Each week Mohammad S. Anwar provides a coding challenge with two tasks. Participants can solve the tasks using any programming language, though Python solutions are shared first before converting them to Perl. The challenge encourages practicing coding skills without using AI tools.

Task 1: Rearrange Binary String

Participants are given a binary string and must rearrange it by replacing all occurrences of "01" with "10" until no more "01" patterns remain. The task is to determine the total number of steps required to complete the rearrangement.

The Python solution starts by validating the input string to ensure it only contains "0" and "1" characters. A counter variable tracks the number of iterations needed. While "01" is present in the string, the loop increments the counter and replaces "01" with "10" using a regular expression substitution. Finally, the count of iterations is returned.

In Perl, the solution follows a similar logic. Input validation, iteration counter, and the loop for replacing "01" with "10" are implemented. The total count of replacements is displayed as the output.

Example input-output:

- 111000 produces 0 steps as no "01" patterns exist.

- 00011 requires 4 steps to fully rearrange.

- 01011 takes 3 steps to resolve.

- 010101 also needs 3 iterations.

- 00001 takes 4 steps to reach the solution.

Task 2: Rational Numbers

The second task involves analyzing a chemical formula containing elements, numbers, and parentheses. Participants must expand any grouped multipliers, count the total number of atoms for each element, and format the final inventory as a string sorted alphabetically by element name. Only elements with a count greater than 1 are included in the output.

A function called expand_parens is defined to handle the expansion of the innermost parentheses followed by a number. It multiplies the atom count for each element by the provided multiplier.

In the main atom_count function, the code expands all parentheses using a regular expression, ensuring the final string consists solely of alphanumeric characters. It then counts the occurrences of each element in the input string. If the input lacks proper formatting, such as mismatched parentheses or incorrect symbols, an error is raised.

Finally, the atom count for each element is returned, sorted alphabetically in the resulting string. Elements with a count greater than 1 are included, while those with a count of 1 or less are omitted.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Crowdsourcing User Contributions to Expand US Basketball Court Database in App Development

Introduction: The Quest for the Perfect Court Imagine this: you’re in a new city, the sun is out, and the only thing missing is a basketball court.

  • Crowdsourcing transforms users into active contributors to basketball court database.
  • Aligning rewards with intrinsic motivations boosts engagement over points systems.
  • Maintenance through updates keeps data current and addresses accessibility barriers.

More from Sunday 23 August →