# RogerBW's Blog

 The Weekly Challenge 218: Maximum Matrix 28 May 2023 I’ve been doing the Weekly Challenges. The latest involved list manipulation and bit flipping. (Note that this is open until 28 May 2023.) Task 1: Maximum Product You are given a list of 3 or more integers. Write a script to find the 3 integers whose product is the maximum and return it. The simple version of this would just be to take the three highest. But that would be dull. Thus example 4, in which some of the integers are negative. I end up sorting the list and then taking four combined slices of it to get the products. Raku: ``````sub maximumproduct(@lst) { my @l = sort {\$^a <=> \$^b}, @lst; my \$b = @l.elems; my @t; `````` `i` is the number of integers to take from the left side (lowest values). `````` for (0..3) -> \$i { my \$p = 1; `````` Multiply by each of those: `````` if (\$i > 0) { for (0..\$i-1) -> \$j { \$p *= @l[\$j]; } } `````` Then by `3 - i` numbers from the right side (highest values). `````` if (\$i < 3) { for (\$b-3+\$i..\$b-1) -> \$j { \$p *= @l[\$j]; } } `````` Store that result. `````` @t.push(\$p); } `````` And return the highest one we got. `````` return max(@t); } `````` Task 2: Matrix Score You are given a m x n binary matrix i.e. having only 1 and 0. You are allowed to make as many moves as you want to get the highest score. A move can be either toggling each value in a row or column. To get the score, convert the each row binary to dec and return the sum. Like last week's problem, this doesn't need to be an exhaustive search. Given the choice in a sequence of `(1, ...)` or `(0, ...)`, the `(1, ...)` will always produce a larger value, whatever the state of any digits after it. Therefore every row must start with a 1; then for columns other than the first, the majority of digits should be 1. Rust: ``````fn matrixscore(matrix0: Vec>) -> u32 { let mut matrix = matrix0; `````` Check the first digit of each row. If it's a 0, flip the row. `````` for i in 0..matrix.len() { if matrix[i][0] == 0 { for j in 0..matrix[i].len() { matrix[i][j] = 1 - matrix[i][j]; } } } `````` Now for each column after the first, `````` let t = matrix.len() / 2; for i in 1..matrix[0].len() { `````` count the zeroes, `````` let mut c = 0; for j in 0..matrix.len() { if matrix[j][i] == 0 { c += 1; } } `````` and if they're more than half the entries, flip the column. `````` if c > t { for j in 0..matrix.len() { matrix[j][i] = 1 - matrix[j][i]; } } } `````` Then read out the results, converting each row to a number. `````` let mut tot = 0; for m in matrix { let mut p = 0; for n in m { p *= 2; if n == 1 { p += 1; } } tot += p; } tot } `````` Turns out `matrix` is a keyword in PostScript… 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 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