RogerBW's Blog

Perl Weekly Challenge 105: Name Root 23 March 2021

I’ve been doing the Perl Weekly Challenges. The latest involved fractional exponentiation and sweating with the oldies. (Note that this is open until 28 March 2021.)

TASK #1 › Nth root

You are given positive numbers $N and $k.

Write a script to find out the $Nth root of $k. For more information, please take a look at the wiki page.

Well, all of my chosen languages have some sort of fractional exponentiation capability so return $k^(1/$N) would have been a valid answer. But this is meant to be at least some sort of challenge, so I implemented Newtonian iteration. In Raku:

sub nroot($n,$a) {
  my $xk=2;
  while (1) {
    my $xk1=(($n-1)*$xk+$a/($xk ** ($n-1)))/$n;
    if ($xk1==$xk) {
      last;
    }
    $xk=$xk1;
  }
  return $xk;
}

and the others look basically the same. The only fiddliness was in Rust, where there's no exponentiation operator, but rather library function calls.

        let xk1=((n-1.0)*xk+a/(xk.powf(n-1.0)))/n;

TASK #2 › The Name Game

You are given a $name.

Write a script to display the lyrics to the Shirley Ellis song The Name Game. Please checkout the wiki page for more information.

I admit I didn't bother with trying to work out where syllable stresses might lie; instead, I make a "tail" part that's the name with any initial consonants removed. (I assume that an initial Y will be a vowel sound, favouring Yves over Yanni; I suppose the next level of sophistication would treat Y as a consonant if and only if a vowel follows it.) If there was no initial consonant, drop the tail into lower case.

Then it's a matter of interpolating name and tail into the template. In Perl:

sub ng {
  my $name=shift;
  (my $tail=$name) =~ s/^[bcdfghjklmnpqrstvwxz]*//i;
  if ($tail eq $name) {
    $tail=lc($tail);
  }
  return "$name, $name, bo-b$tail\nBonana-fanna fo-f$tail\nFee fi mo-m$tail\n$name!";
}

Python has grown string interpolation in recent versions, so I didn't have to use its special formatting language.

    return f"{name}, {name}, bo-b{tail}\nBonana-fanna fo-f{tail}\nFee fi mo-m{tail}\n{name}!"

Rust is above all that sort of thing (though I do think that having to separate the variable list from the template is prone to producing errors, just like good old sprintf; there is a system of named parameters which might help but is rather verbose).

    return format!("{}, {}, bo-b{}\nBonana-fanna fo-f{}\nFee fi mo-m{}\n{}!",name,name,tail,tail,tail,name);

And of course hope that none of your users is called "Buck".

Full code on github.


  1. Posted by John P at 08:53pm on 23 March 2021

    Have you seen these?

    https://azgaar.github.io/Fantasy-Map-Generator https://watabou.itch.io/medieval-fantasy-city-generator

    I'm impressed by the results they get. Somebody has been doing a lot of work.

  2. Posted by RogerBW at 05:14pm on 29 March 2021

    Very nifty stuff – one of these days I'll have to take one and expand it to do e.g. interstellar political boundaries.

    Looking at other people's answers to these challenges:

    (Task 1) A surprising number of people simply used the built-in exponentiation, returning $k ** (1/$N) or something like it. On the one hand, yes, that's obviously faster to implement and run than my version. On the other, well, what's the point? It's in the language already. Of the people who shared my approach, several of them used a fixed number of iterations rather than checking for a convergence.

    (Task 2) Nobody had much to say about this one. Stem, interpolate. One solution allowed Y to be configured as a consonant.

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 aviation 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 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 2022 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