Weekly Challenge: Maximum conflict

Published: (April 5, 2026 at 06:02 AM EDT)
3 min read
Source: Dev.to

Source: Dev.to

Simon Green

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.

Task 1 – Max Odd Binary

Task

You are given a binary string that has at least one 1.
Write a script to rearrange the bits in such a way that the resulting binary number is the maximum odd binary number and return the resulting binary string. The resulting string can have leading zeros.

My solution (Python)

The idea is simple:

  1. The number must be odd → the last bit must be 1.
  2. To maximise the value, all remaining 1s should be placed as far left (most‑significant) as possible.
import re

def max_odd_binary(input_string: str) -> str:
    """Return the maximum odd binary string that can be built from the bits
    of *input_string*.

    The input must contain at least one '1' and consist only of '0' and '1'.
    """
    if not re.search(r'^[01]*1[01]*$', input_string):
        raise ValueError("Input not in expected format")

    zeros = input_string.count('0')
    ones  = input_string.count('1')

    # (ones‑1) leading 1s, all zeros, final 1 to make it odd
    return '1' * (ones - 1) + '0' * zeros + '1'

Perl counterpart

The Perl version follows the same logic, using tr/// to count the bits and the x operator for repetition.

Examples

$ ./ch-1.py 1011
1101

$ ./ch-1.py 100
001

$ ./ch-1.py 111000
110001

$ ./ch-1.py 0101
1001

$ ./ch-1.py 1111
1111

Task 2 – Conflict Events

Task

You are given two (or more) events, each defined by a start and an end time in HH:MM format.
Write a script to determine whether any two events conflict, i.e. have a non‑empty intersection.

My solution (Python)

  1. Convert HH:MM to minutes after midnight (hm2m).
  2. Build a list of minute ranges for each event, handling events that cross midnight.
  3. Walk through the minutes, using a set to detect overlaps.
import re

def hm2m(t: str) -> int:
    """Convert HH:MM to minutes after midnight."""
    if not re.search(r'^([01][0-9]|2[0-3]):[0-5][0-9]$', t):
        raise ValueError("Input is not in the expected format (HH:MM)")
    return int(t[:2]) * 60 + int(t[3:])

def conflict_events(events) -> bool:
    """Return True if any two (or more) events overlap.

    *events* is a list of (start, end) tuples, each in HH:MM format.
    """
    event_minutes = []

    for start, end in events:
        start_min = hm2m(start)
        end_min   = hm2m(end)

        # Zero‑length events are ignored
        if start_min == end_min:
            continue

        # Event crosses midnight → split into two ranges
        if end_min < start_min:
            event_minutes.extend([
                range(start_min, 1440),   # up to midnight
                range(0, end_min)         # after midnight
            ])
        else:
            event_minutes.append(range(start_min, end_min))

    minutes_seen = set()
    for r in event_minutes:
        for minute in r:
            if minute in minutes_seen:
                return True
            minutes_seen.add(minute)

    return False

Perl counterpart

The Perl version mirrors the same algorithm; it stores start/end minutes in an array because Perl lacks a built‑in range object.

Examples

$ ./ch-2.py 10:00 12:00 11:00 13:00
True

$ ./ch-2.py 09:30 10:30 10:30 12:00
False

$ ./ch-2.py 14:00 15:30 14:30 16:00
True

$ ./ch-2.py 08:00 09:00 09:01 10:00
False

$ ./ch-2.py 23:00 00:30 00:00 01:00
True

Happy coding!

0 views
Back to Blog

Related posts

Read more »

PWC 367 Overlapping Oddities

Task 1 – Maximum Odd Binary It’s the week of the Artemis 2 mission to the moon, and we have a problem about odd numbers. I’d call that a Space Oddity. You are...