loading...

Weekly Challenge 084

simongreennet profile image Simon Green ・2 min read

Challenge 084

TASK #1 › Reverse Integer

Let's start with the easy bit. To reverse the digits, I split the string into a sign and numbers. I then use the reverse function to generate the new number.

I couldn't think of an obvious way to figure out if the value was definitively able to be stored as a 32-bit integer, especially since I have a 64-bit OS. For this task, it also doesn't really make sense, since at no time do I actually do arithmetic on that number.

In any case, I check that the new number is within the bounds >= -( 2**31 ) and <= 2**31 - 1, and will return '0' if it is not.

Examples

» ./ch-1.pl 1234
4321

» ./ch-1.pl -1234
-4321

» ./ch-1.pl 1231230512
0
Enter fullscreen mode Exit fullscreen mode

TASK #2 › Find Square

This is very similar to the Lonely X task from Challenge 077, but we use '0' and '1', rather than 'X' and 'O'. In fact, I even found a bug in code for that task.

The first step is to slurp the input into an array of arrays with zeros and ones. To make life easy I remove anything in the string that isn't one of these characters. I check that there is at least two rows and two columns, and that the column size is equal for all rows.

I then go through each row and column, except for the last one, as the top left corner of a square cannot be on the right column or bottom row.

If that cell is '0', then we move on to the next row/column as no square would be found. Otherwise I work out the maximum size (the minimum of the remaining rows or columns). For each size, I add to the @squares array if all three remaining corners are true (i.e. '1').

Finally, I display the output as well as the matching squares found (if any).

Examples

» ./ch-2.pl "0 1 0 1" "0 0 1 0" "1 1 0 1" "1 0 0 1"
Output: 1 (row 1 col 2 size 3)

» ./ch-2.pl "1 1 0 1" "1 1 0 0" "0 1 1 1" "1 0 1 1"
Output: 4 (row 1 col 1 size 2; row 1 col 1 size 4; row 1 col 2 size 3; row 3 col 3 size 2)

» ./ch-2.pl "0 1 0 1" "1 0 1 0" "0 1 0 0" "1 0 0 1"
Output: 0


Enter fullscreen mode Exit fullscreen mode

Discussion

pic
Editor guide
Collapse
simongreennet profile image
Simon Green Author

Time to fess up that I missed one really important use case in task 1. My code would print '0001' if the input was '1000', where it of course should have printed '1'. HT to Cheok-Yin Fung for putting it in her solution

github.com/E7-87-83/perlweeklychal...