# RogerBW's Blog

 Perl Weekly Challenge 122: Basketball Stream 22 July 2021 I’ve been doing the Weekly Challenges. The latest involved a progressive average and additive compositions. (Note that this is open until 25 July 2021.) TASK #1 › Average of Stream You are given a stream of numbers, `@N`. Write a script to print the average of the stream at every point. Bearing in mind that I'm answering these as functions, rather than use a stream, I take an array and return another array with the sets of averages (arithmetic means, to be specific). Fairly straightforward: ``````sub aos(@m) { my \$n=0; my \$t=0; my @o; for @m -> \$i { \$t+=\$i; \$n++; push @o,floor(\$t/\$n); } return @o; } `````` PostScript, using a bunch of variables rather than constant stack furkling. (I haven't used arrays in PostScript before. How quaint, having to declare a maximum capacity.) ``````/aos { /n 0 def /t 0 def dup length array /o exch def { n add /n exch def o t /t t 1 add def n t div cvi put } forall o } def `````` Extending my very basic test harness to compare the arrays was harder work… TASK #2 › Basketball Points You are given a score `\$S`. You can win basketball points e.g. 1 point, 2 points and 3 points. Write a script to find out the different ways you can score `\$S`. Seems like a call for my standard loop-search pattern. Which meant I couldn't be bothered to do it in Raku. Here's the Rust. ``````fn bp (n: u32) -> Vec> { let mut o=vec![]; `````` Some of the other languages make it unreasonably difficult to declare a list with an empty list as its only member. Not Rust. `````` let mut p=vec![vec![]]; while p.len() > 0 { let s=p.pop().unwrap(); `````` Also its `sum()` defaults to 0. (Yeah, I could store the sum along with the list of values that make that sum and it would run a bit faster, but that would be more work.) `````` let t: u32=s.iter().sum(); if t==n { o.push(s); } else { let mut mx=n-t; if mx > 3 { mx=3; } for i in 1..=mx { let mut q=s.clone(); q.push(i); p.push(q); } } } `````` This produces a set of outputs sorted by highest first number, then highest second number, etc.; so we reverse it to match the input example. `````` o.reverse(); return o; } `````` The number of entries for increasing `\$S` matches the Tribonacci numbers, where each number is the sum of the three previous values. This might suggest alternative ways of finding the results; this problem reminded me somewhat of the "Find Possible Paths" of challenge #117. Full code on github. Posted by RogerBW at 06:44pm on 26 July 2021 There aren't many different ways to do part 1, though if one operates on a stream from `STDIN` some languages provide a line counter. Some bloggers didn't bother to avoid repeated summing. Raku of course can potentially generate an infinite stream with `supply`… Most people tackled part 2 recursively. I suppose I really ought to use recursion again some time; my experiences with it have mostly been that it doesn't gain you much if any performance (all that stack winding/unwinding of the whole calling context rather than just one variable) and it's a pain to debug. Still, most of that experience was with different languages… Another approach was to build cross-products of (1..3) at all lengths up to `\$S`, then see which ones produced the right score. 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 10 per page 20 per page 50 per page 100 per page 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 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