RogerBW's Blog

The Weekly Challenge 390: Decode the Order 13 September 2026

I’ve been doing the Weekly Challenges. The latest involved a string decompression and permutations. (Note that this ends today.)

Task 1: Decode String

You are given an encoded string.

Write a script to return the decoded string of the given encoded string.

The encoding rule is: K[encoded_string], where the encoded_string inside the square brackets is repeated exactly K > 0 times.

This is a job for regular expressions! But constructing the replacement is a little fiddly.

In Perl:

sub decodestring($a) {

This is the core of it. This regexp will match an inner block, i.e. a K[x] where x contains no further replacement elements.

  while ($a =~ /(([0-9]+)\[([^\[\]]*)\])/) {

We have a match. So extract orig, the full text of the match, ct, the count, and rep, the unit to be repeated.

    my $orig = $1;
    my $ct = $2;
    my $rep = $3;

Build dest, the repeated unit, and note the length of orig.

    my $dest = $rep x $ct;
    my $l = length($orig);

While we can find orig in the string, replace it with dest. (This doesn't need to be a regexp any more.)

    while (1) {
      my $ix = index($a, $orig);
      if ($ix == -1) {
        last;
      } else {
        substr($a, $ix, $l) = $dest;
      }
    }

And then try again, until the regexp doesn't match.

  }
  $a;
}

Task 2: Order Characters

You are given a string $s (containing only alphabetic characters) and an integer $k > 0.

Write a script to choose one of the first $k letters of given string and append it at the end of the string. You keep doing this until you have lexicographically smallest string and return the string.

Sadly this is a bit simpler than it looks. If k == 1 then it's a rotation of the string; if k > 1 then it's the characters of the string in order. But I pretended I didn't know that so as to have an excuse to use a data structure I don't often employ.

In Rust:

use std::collections::BTreeSet;

fn ordercharacters(a: &str, k: usize) -> String {

Split the string into a list of characters. That's what I'll be working with internally.

    let cc = a.chars().collect::<Vec<char>>();

Initialise the stack.

    let mut stack = Vec::new();
    stack.push(cc.clone());

Initialise the list of combinations we've seen.

    let mut seen = BTreeSet::new();

Standard DFS pattern, pop off the top of the stack…

    while let Some(s) = stack.pop() {

and iterate over the possible transformations of it.

        for i in 0 .. k {
            let mut sp = s.clone();
            let c = sp.remove(i);
            sp.push(c);

If it's a result we haven't seen before, store it, and push it onto the stack for further transformation.

            if !seen.contains(&sp) {
                seen.insert(sp.clone());
                stack.push(sp);
            }
        }
    }

And here's where it gets cunning, because the BTreeSet guarantees that all the entries are in sorted order. So all I need to do is to take the first one, convert it back into a string, and return it.

    seen.into_iter().nth(0).unwrap().into_iter().collect()
}

In PostScript I don't have a BTreeSet. (Though I should learn how they work and write one.)

/ordercharacters {
    0 dict begin

Store k and initialise the stack. (I don't need to keep the initial string around after that.)

    /k exch def
    dup length /l exch def
    /stack exch
    [ exch
    ] def

Initialise the seen list, just a plain dict which I'll use as a set.

    /seen 0 dict def

Usual DFS loop.

    {
        stack length 0 eq {
            exit
        } if
        /stack stack apop.right /s exch def def

Iterate over the possible transformations of this string.

        0 1 k 1 sub {
            /i exch def

Convert the string to an array (which makes a fresh copy of it)

            s s2a

Move that arracy onto the stack.

            aload pop

Transform it.

            l i sub -1 roll

Pack it up into an array again, and convert that into a string.

            l array astore a2s
            /sn exch def

If we haven't seen it, store it, as above in the Rust code.

            seen sn known not {
                seen sn 1 put
                /stack stack sn apush.right def
            } if
        } for
    } loop

Now I have a dict with all the available permutations. But string keys in dicts are automatically converted to names. So I'll get out the keys.

    seen keys

Convert them all to strings (we know the length in advance, which makes life easy)

    { l string cvs } map

Sort them.

    quicksort

And return the first one.

    0 get
    end
} bind def

Full code in all tagged languages is on codeberg.

Add A Comment

Your Name
Your Email
Your Comment

Note that I will only approve comments that relate to the blog post itself, not ones that relate only to previous comments. This is to ensure that the blog remains outside the scope of the UK's Online Safety Act (2023).

Your submission will be ignored if any field is left blank, but your email address will not be displayed. Comments will be processed through markdown.

Search
Archive
Tags 1920s 1930s 1940s 1950s 1960s 1970s 1980s 1990s 2000s 2010s 2300ad 3d printing action advent of code adventure aeronautics aikakirja anecdote animation anime army astronomy audio audio tech aviation base commerce battletech bayern beer boardgaming book of the week bookmonth chain of command children chris chronicle church of no redeeming virtues cold war comedy computing contemporary cornish smuggler cosmic encounter coup covid-19 crime crystal cthulhu eternal cycling dead of winter disaster doctor who documentary drama driving drone ecchi economics en garde espionage essen 2015 essen 2016 essen 2017 essen 2018 essen 2019 essen 2022 essen 2023 essen 2024 essen 2025 existential risk falklands war fandom fanfic fantasy feminism filk film firefly first world war flash point flight simulation food garmin drive gazebo genesys geocaching geodata gin gkp gurps gurps 101 gus harpoon historical history horror horrorm science fiction hugo 2014 hugo 2015 hugo 2016 hugo 2017 hugo 2018 hugo 2019 hugo 2020 hugo 2021 hugo 2022 hugo 2023 hugo 2024 hugo 2025 hugo 2026 hugo-nebula reread humour in brief avoid instrumented life javascript julian simpson julie enfield kickstarter kotlin learn to play leaving earth linux liquor lovecraftiana lua mecha men with beards mpd museum music mystery naval noir non-fiction one for the brow openscad opera parody paul temple perl perl weekly challenge photography podcast poetry politics postscript powers prediction privacy project woolsack pyracantha python quantum rail raku ranting raspberry pi reading reading boardgames social real life restaurant review reviews romance rpg a day rpgs ruby rust scala science fiction scythe second world war security shipwreck simutrans smartphone south atlantic war squaddies stationery steampunk stuarts suburbia superheroes suspense talon television the resistance the weekly challenge thirsty meeples thriller tin soldier torg toys trailers travel type 26 type 31 type 45 typst vietnam war war wargaming weather wives and sweethearts writing about writing x-wing young adult
Special All book reviews, All film reviews
Produced by aikakirja v0.1