Weekly Challenge: Average Progression

Published: (December 14, 2025 at 06:58 AM EST)
3 min read
Source: Dev.to

Source: Dev.to

Weekly Challenge 351

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. It’s a great way for us all to practice some coding.

Challenge, My solutions

Task 1: Special Average

Task

You are given an array of integers. Write a script to return the average excluding the minimum and maximum of the given array.

My solution (Python)

def special_average(ints: list) -> float:
    min_value = min(ints)
    max_value = max(ints)
    short_list = [n for n in ints if n != min_value and n != max_value]

    if not short_list:
        return 0

    return sum(short_list) / len(short_list)

My solution (Perl)

sub main (@ints) {
    my $min_value = min(@ints);
    my $max_value = max(@ints);
    my @short_array = grep { $_ != $min_value && $_ != $max_value } @ints;

    if ($#short_array == -1) {
        say 0;
    }

    say sum(@short_array) / scalar(@short_array);
}

Examples

$ ./ch-1.py 8000 5000 6000 2000 3000 7000
5250.0

$ ./ch-1.py 100000 80000 110000 90000
95000.0

$ ./ch-1.py 2500 2500 2500 2500
0

$ ./ch-1.py 2000
0

$ ./ch-1.py 1000 2000 3000 4000 5000 6000
3500.0

Task 2: Arithmetic Progression

Task

You are given an array of numbers. Write a script to return true if the given array can be re‑arranged to form an arithmetic progression, otherwise return false.

A sequence of numbers is called an arithmetic progression if the difference between any two consecutive elements is the same.

My solution (Python)

Floating‑point precision can be tricky (e.g., 0.1 + 0.2 != 0.3). For reliable results, inputs should be integers or Decimal values.

def arithmetic_progression(ints: list) -> bool:
    sorted_ints = sorted(ints)
    diff = sorted_ints[1] - sorted_ints[0]

    for i in range(2, len(sorted_ints)):
        if sorted_ints[i] - sorted_ints[i - 1] != diff:
            return False

    return True

My solution (Perl)

Perl suffers the same floating‑point issues; Math::BigFloat can be used for higher precision.

sub main (@ints) {
    my @sorted_ints = map { Math::BigFloat->new($_) } sort { $a  $b } @ints;
    my $diff        = $sorted_ints[1] - $sorted_ints[0];

    foreach my $i ( 2 .. $#sorted_ints ) {
        if ( $sorted_ints[$i] - $sorted_ints[ $i - 1 ] != $diff ) {
            say 'false';
            return;
        }
    }

    say 'true';
}

Examples

$ ./ch-2.py 1 3 5 7 9
True

$ ./ch-2.py 9 1 7 5 3
True

$ ./ch-2.py 1 2 4 8 16
False

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

$ ./ch-2.py 1.5 3 0 4.5 6
True

$ ./ch-2.py 0.1 0.3 0.2 0.4
True
Back to Blog

Related posts

Read more »

Leetcode 39 Combination Sum

Problem Statement Given an array of distinct integers candidates and a target integer target, return all unique combinations of candidates where the chosen num...

Palindrome Checker

What is a palindrome? A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward ignoring spaces, punctua...