# RogerBW's Blog

 The Weekly Challenge 204: Reshape the Monotony 19 February 2023 I’ve been doing the Weekly Challenges. The latest involved list testing and matrix reconstruction. (Note that this is open until 19 February 2023.) Task 1: Monotonic Array You are given an array of integers. Write a script to find out if the given array is Monotonic. Print 1 if it is otherwise 0. An array is Monotonic if it is either monotone increasing or decreasing. This isn't my usual understanding of the term, in which all differences should be equal – i.e. `1, 3, 5` would qualify but ```1, 3, 4``` would not. But here in effect we're asked to verify that the direction is consistent. In other words, all the differences are positive-or-zero, or they're all negative-or-zero. And therefore if I multiply the lowest difference by the highest, the result will be positive or zero. So that's what I do. The most compact code is in Raku: ``````sub monotonicarray(@l) { my @d = @l.rotor(2 => -1).map({\$_[1] - \$_[0]}); return @d.min * @d.max >= 0; } `````` but it's not so very much longer in a lanuage such as Lua that doesn't have these convenience functions: ``````function monotonicarray(l) local d = {} for i = 1, #l - 1 do table.insert(d, l[i + 1] - l[i]) end return math.min(table.unpack(d)) * math.max(table.unpack(d)) >= 0 end `````` Task 2: Reshape Matrix You are given a matrix (m x n) and two integers (r) and (c). Write a script to reshape the given matrix in form (r x c) with the original value in the given matrix. If you can’t reshape print 0. I do this in three steps: flatten the input array of arrays (I've never had a use for flattening in real life); check whether we have the right number of entries for the desired output size; drop those numbers into an output list, one new row's worth at a time. JavaScript: ``````function reshapematrix(m, r, c) { let mv = m.flat(); if (r * c != mv.length) { return [[0]]; } let out = []; for (let i = 0; i < r; i++) { out.push(mv.slice(i * c, (i + 1) * c)); } return out; } `````` and PostScript isn't wildly different except for using fewer variables. ``````/reshapematrix { 3 dict begin /c exch def /r exch def /mx exch [ exch { { } forall } forall ] def r c mul mx length eq { [ 0 1 r 1 sub { mx exch c mul c getinterval } for ] } { [ [ 0 ] ] } ifelse end } bind def `````` 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 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 cthulhu eternal cycling dead of winter doctor who documentary drama driving drone ecchi economics espionage essen 2015 essen 2016 essen 2017 essen 2018 essen 2019 essen 2022 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 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