Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #367 (“Max Odd Binary” and “Conflict Events”)

For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, with a new pair of tasks each Monday. You can find it here: The Weekly Challenge

The Weekly Challenge for the week of 2026-03-30 through 2026-04-05 is #367. The tasks for challenge #367 are as follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 367-1: Max Odd Binary
Submitted by: Mohammad Sajid Anwar
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.

Example 1
Input: $str = "1011"
Output: "1101"

Example 2
Input: $str = "100"
Output: "001"

Example 3
Input: $str = "111000"
Output: "110001"

Example 4
Input: $str = "0101"
Output: "1001"

Example 5
Input: $str = "1111"
Output: "1111"

A positive integer is "odd" if-and-only-if its least-significant binary digit is "1", and it is "maximum" if all of the 1s are clustered on the left end. So the "x" operator will work for this. Given $m 0s and $n 1s, max is given by:

my $max = '1'x($n-1) . '0'x$m . '1';

Robbie Hatley's Perl Solution to The Weekly Challenge 367-1

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 367-2: Conflict Events
Submitted by: Mohammad Sajid Anwar
You are given two events start and end time. Write a script to
find out if there is a conflict between the two events.
A conflict happens when two events have some non-empty
intersection. [Comment by Robbie Hatley: This task is ambiguous
because no dates are given. Example 5 is especially ambiguous
because it can be interpreted in two equally-valid ways (see
my notes below). So to disambiguate, I'll assume that if event2
has a start time before that of event1, then event2's times are
intended to mean the next day.]

Example 1
Input: @event1 = ("10:00", "12:00")
       @event2 = ("11:00", "13:00")
Output: true
Both events overlap from "11:00" to "12:00".

Example 2
Input: @event1 = ("09:00", "10:30")
       @event2 = ("10:30", "12:00")
Output: false
Event1 ends exactly at 10:30 when Event2 starts.
Since the problem defines intersection as non-empty,
exact boundaries touching is not a conflict.

Example 3
Input: @event1 = ("14:00", "15:30")
       @event2 = ("14:30", "16:00")
Output: true
Both events overlap from 14:30 to 15:30.

Example 4
Input: @event1 = ("08:00", "09:00")
       @event2 = ("09:01", "10:00")
Output: false
There is a 1-minute gap from "09:00" to "09:01".

Example 5
Input: @event1 = ("23:30", "00:30")
       @event2 = ("00:00", "01:00")
Output: false? true? (Ambiguous.)

Given Event E1 as a reference, the outcome will depend on the beginning and end of event E2.

If E2beg is before E1beg, conflict iff E2end is > E1beg.
If E2beg is during E1, conflit.
If E2beg is after  E1, no conflict.

However, keep in-mind that this task is ambiguous, because no date is associated with any of the times. Example 5 is especially ambiguous because there are two equally valid ways of interpreting it: One can assume that event2's times are both the next day (in which case, there is a conflict), or the same day (in which case there is no conflict because event1 began 22.5 hours after event2 ended). To disambiguate, I'll assume that if event2 has a start time before that of event1, then event2's times are intended to mean the next day.

Robbie Hatley's Perl Solution to The Weekly Challenge 367-2

That's it for challenge 367; see you on challenge 368!

Comments

Popular posts from this blog

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #334 (“Range Sum” and “Nearest Valid Point”)

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #336 (“Equal Group” and “Final Score”)

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #326 (“Day of Year” and “Decompressed List”)