DEV Community

Simon Green
Simon Green

Posted on

Weekly Challenge: An average reflection

Weekly Challenge 397

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

Task 1: Absent Smallest Positive

You are given an array of integers.

Write a script to return the smallest absent positive integer such that it is strictly greater than the average of all elements in the given array.

My solution

This is relatively straight forward. I start by calculating the average and storing it in the variable average. The result variable one more than the average or 1 if the average is less than 1. I then have a loop that increments result if it is in the ints list (array in Perl).

def asp(ints: list[int]) -> int:
    average = sum(ints) // len(ints)
    result = max(1, average + 1)

    while result in ints:
        result += 1

    return result
Enter fullscreen mode Exit fullscreen mode

The Perl code follows similar logic, using some functions from the List::Util module.

use List::Util qw(any max sum);

sub main (@ints) {
    # Calculate the average
    my $average = sum(@ints) / scalar(@ints);
    my $result = max( 1, int($average) + 1 );
    ++$result while ( any { $_ == $result } @ints );
    say $result;
Enter fullscreen mode Exit fullscreen mode

Examples

$ ./ch-1.py 1 2 4 5 6
7

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

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

$ ./ch-1.py 2 3 7 8
6

$ ./ch-1.py 1 1 1 10
4
Enter fullscreen mode Exit fullscreen mode

Task 2: Binary Reflection

You are given an array of integers.

Write a script to sort the given array in ascending order based on the binary reflection of each element. If two different numbers have the same binary reflection, the smaller original number should appear first.

My solution

For this task, I create a function called calc_binary_reflection. Given an integer i, it will convert it to a binary value ([2:] strips the 0b in that string), reverses it and converts to back to an integer.

def calc_binary_reflection(i: int) -> int:
    b = bin(i)[2:]
    return int(b[::-1], 2)
Enter fullscreen mode Exit fullscreen mode

The main function does a double sort. The inner sort ensures that integers with the same reflection integer are sorted numerically, while the outer sort uses the above function.

def binary_reflection(ints: list[int]) -> list[int]:
    return sorted(sorted(ints), key=calc_binary_reflection)
Enter fullscreen mode Exit fullscreen mode

For the Perl solution, I can achieve the same functionality with a single call to the sort function.

sub calc_binary_reflection($i) {
    my $b = sprintf "%b", $i;
    return oct( "0b" . reverse($b) );
}

sub main (@ints) {
    my @sorted_ints = sort {
        calc_binary_reflection($a) <=> calc_binary_reflection($b) || $a <=> $b
    } @ints;

    say "(" . join( ", ", @sorted_ints ) . ")";
}
Enter fullscreen mode Exit fullscreen mode

Examples

$ ./ch-2.py 3 6 12
(3, 6, 12)

$ ./ch-2.py 1 2 4 8
(1, 2, 4, 8)

$ ./ch-2.py 7 8 9 10
(8, 10, 7, 9)

$ ./ch-2.py 5 9 15 17
(5, 9, 15, 17)

$ ./ch-2.py 11 13 14 19
(14, 13, 11, 19)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)