Camel
Peter
Peter Campbell Smith

Days to decompress

Weekly challenge 326 — 16 June 2025

Week 326: 16 Jun 2025

Task 2

Task — Decompressed list

You are given an array of positive integers having even elements. Write a script to to return the decompress list. To decompress, pick adjacent pair (i, j) and replace it with j, i times.

Examples


Example 1
Input: @ints = (1, 3, 2, 4)
Output: (3, 4, 4)
Pair 1: (1, 3) => 3 one time  => (3)
Pair 2: (2, 4) => 4 two times => (4, 4)

Example 2
Input: @ints = (1, 1, 2, 2)
Output: (1, 2, 2)
Pair 1: (1, 1) => 1 one time  => (1)
Pair 2: (2, 2) => 2 two times => (2, 2)

Example 3
Input: @ints = (3, 1, 3, 2)
Output: (1, 1, 1, 2, 2, 2)
Pair 1: (3, 1) => 1 three times => (1, 1, 1)
Pair 2: (3, 2) => 2 three times => (2, 2, 2)

Analysis

There seems little alternative to doing what it says: process the integers in @ints two at a time, and push them onto a new array as described in the challenge. Leaving the original array unchanged - rather than unshifting the values into scalars - probably saves a couple of microseonds.

Try it 

Try running the script with any input:



example: 3, 1, 4, 1, 5, 9, 2, 6, 5, 4

Script


#!/usr/bin/perl

# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge

use v5.26;    # The Weekly Challenge - 2025-06-16
use utf8;     # Week 326 - task 2 - Decompressed list
use warnings; # Peter Campbell Smith
binmode STDOUT, ':utf8';
use Encode;

decompressed_list(1, 3, 2, 4);
decompressed_list(1, 1, 2, 2);
decompressed_list(3, 1, 3, 2);

# bigger example
my @ints;
push @ints, int(rand(5) + 1) for 0 .. 99;
decompressed_list(@ints);

sub decompressed_list {
    
    my (@list, @new_list, $j);
    
    # initialise
    @list = @_;
    
    # do what it says
    for ($j = 1; $j <= $#list; $j += 2) {
        push @new_list, $list[$j] for 1 .. $list[$j - 1];
    }
    
    # report
    say qq[\nInput:  \@ints1 = (] . join(', ', @list) . q[)];
    say qq[Output: \@ints2 = (] . join(', ', @new_list) . q[)];
}

Output


Input:  @ints1 = (1, 3, 2, 4)
Output: @ints2 = (3, 4, 4)

Input:  @ints1 = (1, 1, 2, 2)
Output: @ints2 = (1, 2, 2)

Input:  @ints1 = (3, 1, 3, 2)
Output: @ints2 = (1, 1, 1, 2, 2, 2)

Input:  @ints1 = (3, 1, 1, 1, 1, 1, 4, 4, 1, 1, 1, 2, 1,
   5, 2, 4, 5, 5, 4, 1, 3, 5, 2, 3, 1, 1, 3, 5, 2, 5, 2,
   3, 5, 5, 4, 4, 4, 4, 1, 1, 1, 4, 2, 4, 3, 1, 3, 3, 5,
   1, 5, 5, 4, 2, 4, 3, 2, 1, 2, 2, 3, 4, 5, 3, 2, 5, 2,
   4, 4, 1, 2, 2, 5, 2, 3, 2, 4, 5, 4, 1, 3, 4, 4, 1, 4,
   3, 4, 3, 1, 3, 3, 3, 5, 5, 5, 2, 3, 2, 1, 3)
Output: @ints2 = (1, 1, 1, 1, 1, 4, 4, 4, 4, 1, 2, 5, 4,
   4, 5, 5, 5, 5, 5, 1, 1, 1, 1, 5, 5, 5, 3, 3, 1, 5, 5,
   5, 5, 5, 3, 3, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
   1, 4, 4, 4, 1, 1, 1, 3, 3, 3, 1, 1, 1, 1, 1, 5, 5, 5,
   5, 5, 2, 2, 2, 2, 3, 3, 3, 3, 1, 1, 2, 2, 4, 4, 4, 3,
   3, 3, 3, 3, 5, 5, 4, 4, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2,
   2, 2, 2, 2, 5, 5, 5, 5, 1, 1, 1, 1, 4, 4, 4, 1, 1, 1,
   1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 5, 5, 5, 5,
   2, 2, 2, 2, 2, 2, 2, 2, 3)

 

Any content of this website which has been created by Peter Campbell Smith is in the public domain