Peter’s blog ✴ Week 389 ✴ 31 August 2026

THE WEEKLY CHALLENGE
Musical zigzags

The Perl Camel

Task 1

Reorder notes

You are given an array [composer, notes, permutation].

Reconstruct the melody by using each permutation value as the destination position of the corresponding note. Use no explicit for, foreach, or while loops. Output each result as COMPOSER => reordered notes.

ASSUMPTION: Input is valid; the notes array and permutation array have identical lengths, and the permutation contains each position from 1 to N exactly once.

Examples


Example 1
Input: $melody = ['Bach', [qw(C D E F# G A B)], [7, 1, 6, 2, 5, 3, 4]]
Output: BACH => D F# A B G E C
Note 1 (C)  moves to position 7.
Note 2 (D)  moves to position 1.
Note 3 (E)  moves to position 6.
Note 4 (F#) moves to position 2.
Note 5 (G)  moves to position 5.
Note 6 (A)  moves to position 3.
Note 7 (B)  moves to position 4.

Example 2
Input: $melody = ['Beethoven', [qw(C D F# G Ab)], [1, 3, 5, 2, 4]]
Output: BEETHOVEN => C G D Ab F#
Note 1 (C)  stays at position 1.
Note 2 (D)  moves to position 3.
Note 3 (F#) moves to position 5.
Note 4 (G)  moves to position 2.
Note 5 (Ab) moves to position 4.

Example 3
Input: $melody = [ 'Brahms', [qw(C Db Eb F G Ab Bb C D)], [9, 3, 7, 1,
   8, 5, 2, 6, 4] ]
Output: BRAHMS => F Bb Db D Ab C Eb G C

Example 4
Input: $melody = [ 'Bruckner', [qw(G F# Bb C D Eb F)], [4, 7, 2, 6, 1,
   5, 3] ]
Output: BRUCKNER => D Bb F G Eb C F#

Example 5
Input: $melody = ['Berg', [qw(C#)], [1]]
Output: BERG => C#

Analysis

Well, hmm, I've done it without using for, foreach or while, but maybe you hadn't thought of banning do .. until.

I could not identify any of the melodies as being used by the stated composers, though I feel sure Alban Berg used plenty of C♯s and maybe the rest are buried somewher in the respective composers' oeuvres. I've added a fragment of Mozart which is perhaps better known.

I had never noticed before how hard it is to get Perl to accept a '#' which isn't signalling a comment, but happily Unicode provides 'proper' ♯ and ♭ symbols.

Try it 

Your input:



eg: Strauss



eg: A A F♯ D D



eg: 5, 4, 3, 2, 1

Script


#!/usr/bin/perl

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

use v5.26;    # The Weekly Challenge - 2026-08-31
use utf8;     # Week 389 - task 1 - Reorder notes
use warnings; # Peter Campbell Smith
binmode STDOUT, ':utf8';
use Encode;

reorder_notes('Bach', [qw(C D E F♯ G A B)], 
    [7, 1, 6, 2, 5, 3, 4]);
reorder_notes('Beethoven', [qw(C D F♯ G A♭)], 
    [1, 3, 5, 2, 4]);
reorder_notes('Brahms', [qw(C D♭ E♭ F G A♭ B♭ C D)], 
    [9, 3, 7, 1, 8, 5, 2, 6, 4]);
reorder_notes('Bruckner', [qw(G F♯ B♭ C D E♭ F)], 
    [4, 7, 2, 6, 1, 5, 3]);
reorder_notes('Berg', [qw(C♯)], [1]);
reorder_notes('Mozart', [qw(G B♭ C A D C♯ D)], 
    [7, 6, 5, 4, 3, 2, 1]);

sub reorder_notes {
    
    my ($composer, @notes, @sequence, @tune, $i);
    
    # initialise
    $composer = $_[0];
    @notes = @{$_[1]};
    @sequence = @{$_[2]};
    
    # rearrange notes
    $i = 0;
    do {
        $tune[$sequence[$i] - 1] = $notes[$i];
        $i ++;
    } until ($i == @notes);
        
    # report
    say qq[\nInput:  \$composer = '$composer'];
    say qq[        \@notes    = ] . join(', ', @notes); 
    say qq[        \@sequence = ] . join(', ', @sequence);
    say qq[Output: ] . uc($composer) . qq[ => ] . join (' ', @tune);
}


14 lines of code
Completed after the closing date and not submitted to GitHub

Output from script


Input:  $composer = 'Bach'
        @notes    = C, D, E, F♯, G, A, B
        @sequence = 7, 1, 6, 2, 5, 3, 4
Output: BACH => D F♯ A B G E C

Input:  $composer = 'Beethoven'
        @notes    = C, D, F♯, G, A♭
        @sequence = 1, 3, 5, 2, 4
Output: BEETHOVEN => C G D A♭ F♯

Input:  $composer = 'Brahms'
        @notes    = C, D♭, E♭, F, G, A♭, B♭, C, D
        @sequence = 9, 3, 7, 1, 8, 5, 2, 6, 4
Output: BRAHMS => F B♭ D♭ D A♭ C E♭ G C

Input:  $composer = 'Bruckner'
        @notes    = G, F♯, B♭, C, D, E♭, F
        @sequence = 4, 7, 2, 6, 1, 5, 3
Output: BRUCKNER => D B♭ F G E♭ C F♯

Input:  $composer = 'Berg'
        @notes    = C♯
        @sequence = 1
Output: BERG => C♯

Input:  $composer = 'Mozart'
        @notes    = G, B♭, C, A, D, C♯, D
        @sequence = 7, 6, 5, 4, 3, 2, 1
Output: MOZART => D C♯ D A C B♭ G

 

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