RogerBW's Blog

The Weekly Challenge 145: Tree Product 28 December 2021

I’ve been doing the Weekly Challenges. The latest involved vector multiplication and a recently-invented data structure. (Note that this is open until 2 January 2022.)

Task 1: Dot Product

You are given 2 arrays of same size, @a and @b.

Write a script to implement Dot Product.

Pretty straightforward, really. Raku:

sub dotproduct(@a,@b) {
  my $p=0;
  for 0..@a.end -> $i {
    $p += @a[$i]*@b[$i];
  }
  return $p;
}

In an enumerating language (here Rust):

fn dotproduct(a: Vec<u32>, b: Vec<u32>) -> u32 {
    let mut p=0;
    for (i,va) in a.iter().enumerate() {
        p+=va*b[i];
    }
    p
}

Task 2: Palindromic Tree

You are given a string $s.

Write a script to create a Palindromic Tree for the given string.

The actual tree structure is quite fiddly to produce - and the test cases don't test that you've actually produced one. I found a Python implementation by TimSC on Rosetta Code and used that as the basis for my Perl, Raku and Ruby solutions; "Purefox" there had already translated it to Kotlin. PostScript doesn't have an object system, and I don't know how to drive Lua's, so I used a more basic O(N²)-ish algorithm to produce the same list of all palindromes found in the input; and because I don't really get Rust lifetimes and references yet, I ended up using it there too.

(I've found several posts about this structure, but no practical ways of using it; it's a lot more faff to get O(N)-ish performance, and arraying the results in order removes that advantage anyway. I suppose if I were using really huge strings…)

The full version is too long to include, but here's the cheating O(N²) version in Lua:

function eertree(str)
   pal={}
   q={}
   for i = 1,#str do
      for j = i,#str do
         strpal=string.sub(str,i,j)
         strrev=string.reverse(strpal)
         if strpal == strrev and q[strpal] == nil then
            table.insert(pal,strpal)
            q[strpal]=true
         end
      end
   end
   return pal
end

Full code on github.

Comments on this post are now closed. If you have particular grounds for adding a late comment, comment on a more recent post quoting the URL of this one.

Search
Archive
Tags 1920s 1930s 1940s 1950s 1960s 1970s 1980s 1990s 2000s 2010s 3d printing action advent of code aeronautics aikakirja anecdote animation anime army astronomy audio audio tech base commerce battletech 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 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 existential risk falklands war fandom fanfic fantasy feminism 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 hugo 2014 hugo 2015 hugo 2016 hugo 2017 hugo 2018 hugo 2019 hugo 2020 hugo 2021 hugo 2022 hugo 2023 hugo 2024 hugo-nebula reread 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 opera parody paul temple perl perl weekly challenge photography podcast politics postscript powers prediction privacy project woolsack pyracantha python quantum rail raku ranting raspberry pi reading reading boardgames social real life restaurant 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 television the resistance the weekly challenge thirsty meeples thriller tin soldier torg toys trailers travel type 26 type 31 type 45 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