Peter’s blog ✴ Week 396 ✴ 19 October 2026

THE WEEKLY CHALLENGE
A race and some hills

The Perl Camel

Task 1

Finishing order

You are given two array of integers, @order and @friends. The @order (size $n) contains every integer from 1 to $n exactly once. The @friends contains ID sorted in strictly increasing order.

Write a script to return the array containing IDs in finishing order.

Examples


Example 1
Input: @order = (3, 1, 4, 2, 5)
       @friends = (1, 2, 4)
Output: (1, 4, 2)
ID 1 finished 2nd.
ID 4 finished 3rd.
ID 2 finished 4th.

Example 2
Input: @order = (5, 4, 3, 2, 1)
       @friends = (1, 3, 5)
Output: (5, 3, 1)
ID 5 finished 1st.
ID 3 finished 3rd.
ID 1 finished 5th.

Example 3
Input: @order = (2, 4, 1, 3)
       @friends = (1, 2, 3, 4)
Output: (2, 4, 1, 3)
ID 2 finished 1st.
ID 4 finished 2nd.
ID 1 finished 3rd.
ID 3 finished 4th.

Example 4
Input: @order = (4, 1, 3, 2)
       @friends = (3)
Output: (3)

Example 5
Input: @order = (1, 3, 2, 6, 5, 4)
       @friends = (1, 2, 3)
Output: (1, 3, 2)
ID 1 finished 1st.
ID 3 finished 2nd.
ID 2 finished 3rd.

Analysis

This is slightly mind-bending task for a Monday morning, but in the end I reduced it to two lines of code, which could be combined into one if you really wanted.

My first line inverts the @order array to create @places, where $places[$j] is the placing of friend $j in the race. So for example $places[1] is the winner. (The + 1 is there because the winner is placed 1st, not 0th)

I can then use the useful Perl construct:

sort {$places[$a] <=> $places[$b]} @friends
to sort the friends in place order, as required.

Try it 

Your input:



eg: 3, 1, 4, 2, 5 - must be 1 .. $n in any order (see above)



eg: 1, 2, 4 - must be in range 1 .. $n in increasing order

Script


#!/usr/bin/perl

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

use v5.26;    # The Weekly Challenge - 2026-10-19
use utf8;     # Week 396 - task 1 - Finishing order
use warnings; # Peter Campbell Smith
binmode STDOUT, ':utf8';
use Encode;

finishing_order([3, 1, 4, 2, 5], [1, 2, 4]);  # 1 4 2
finishing_order([5, 4, 3, 2, 1], [1, 3, 5]);
finishing_order([2, 4, 1, 3], [1, 2, 3, 4]);
finishing_order([4, 1, 3, 2], [3]);
finishing_order([1, 3, 2, 6, 5, 4], [1, 2, 3]);

sub finishing_order {
    
    my (@order, @friends, @places);
    
    # initialise
    @order = @{$_[0]};
    @friends = @{$_[1]};
    say qq[\nInput:  \@order = (] . join(', ', @order) .
        qq[), \@friends = (] . join(', ', @friends) . ')';
    
    # make $places[$j] == placing of $friend[$j]
    $places[$order[$_]] = $_ + 1 for 0 .. $#order;
    
    # order friends by their placing
    @friends = sort {$places[$a] <=> $places[$b]} @friends;
    
    # report
    say qq[Output: (] . join(', ', @friends) . ')';
}

8 lines of code

Output from script


Input:  @order = (3, 1, 4, 2, 5), @friends = (1, 2, 4)
Output: (1, 4, 2)

Input:  @order = (5, 4, 3, 2, 1), @friends = (1, 3, 5)
Output: (5, 3, 1)

Input:  @order = (2, 4, 1, 3), @friends = (1, 2, 3, 4)
Output: (2, 4, 1, 3)

Input:  @order = (4, 1, 3, 2), @friends = (3)
Output: (3)

Input:  @order = (1, 3, 2, 6, 5, 4), @friends = (1, 2, 3)
Output: (1, 3, 2)

 

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