CY's Take on PWC#109

If you want to challenge yourself on programming, especially on Perl and/or Raku, go to https://perlweeklychallenge.org, code the latest challenges, submit codes on-time (by GitHub or email).

ch2_wk109.png

Task 1: Chowla Numbers

#!/usr/bin/perl
# The Weekly Challenge - 109 
Task 1 Chowla Numbers
use strict;
use warnings;
my $F = $ARGV[0] || 20;
my @chowla_seq = (0,0,0);
for my $n (4..$F) {
    my $s = 0;
    for my $k (2..$n-1) {
        $s += $k unless $n % $k;
    }
    push @chowla_seq$s;
}
print join ""@chowla_seq;
print "\n";
Employ no special tricks here except the use of unless and (roughly speaking?)the boolean value of 0 is equivalent to undef, while both mean false.

Task 2: Four Squares Puzzle

There are many improvisations we can do with it. The task is "N Boxes Puzzle" on my mind.
JavaFX
On a very personal aspect I am learning Java. I have been thinking of trying out something with JavaFX for a while and this task is a chance. After some efforts, the rectangles did not overlap when I just made their height and width scaled. Finally I put additional terms on their width to force them overlapped ‐ this make the setting not so extensible facing different parameters.
Instead of Hash Map, an array of BitMaps is chosen for noting which points are inside a specific rectangle.
For the Java source codes, the most consuming part for me had been generating the permutations because almostly every variable is a reference (not quite sure the technical terms again).
Perl: Text-Interface
The permutations are handled by the CPAN module Algorithm::Combinatorics.
My most ambitious plan was user providing the command-line graphics as input. This requires locating the "coordinates" of the alphabets and boxes. Okay, I did the second part programmatically, the first part... by hand.
(the graph in task statement)
              (1)                    (3)
        ╔══════════════╗      ╔══════════════╗
        ║              ║      ║              ║
        ║      a       ║      ║      e       ║
        ║              ║ (2)  ║              ║  (4)
        ║          ┌───╫──────╫───┐      ┌───╫─────────┐
        ║          │   ║      ║   │      │   ║         │
        ║          │ b ║      ║ d │      │ f ║         │
        ║          │   ║      ║   │      │   ║         │
        ║          │   ║      ║   │      │   ║         │
        ╚══════════╪═══╝      ╚═══╪══════╪═══╝         │
                   │       c      │      │      g      │
                   │              │      │             │
                   │              │      │             │
                   └──────────────┘      └─────────────┘
my @boxes = (
     Box->new( Point->new(9,6), Point->new(24,15) ), 
     Box->new( Point->new(20,10), Point->new(35,19) ), 
     Box->new( Point->new(31,6), Point->new(46,15) ), 
     Box->new( Point->new(42,10), Point->new(56,19) ), 
);

my @var = (
    Point->new(16,8),  #a
    Point->new(22,12), #b
    Point->new(28,16), #c
    Point->new(33,12), #d
    Point->new(38,8),  #e
    Point->new(44,12), #f
    Point->new(49,16), #g
);
An array of (references of) arrays is chosen to determine which points are inside the boxes.
my $M = scalar @possible_val;
my $N = scalar @boxes;

my @container;

for my $i_b (0..$N-1) {
    my $b = $boxes[$i_b];
    $container[$i_b] = [];
    for my $v_ind (0..$M-1) {
        if (   $var[$v_ind]->x < $b->br->x  
           && $var[$v_ind]->x > $b->tl->x  
           && $var[$v_ind]->$b->tl->y  
           && $var[$v_ind]->$b->br->y) {
            push @{$container[$i_b]}, $v_ind;
        }
    }
}
$ perl ch-2.pl
Number of solutions: 8
  a  b  c  d  e  f  g
  3  7  2  1  5  4  6
  4  5  3  1  6  2  7
  4  7  1  3  2  6  5
  5  6  2  3  1  7  4
  6  4  1  5  2  3  7
  6  4  5  1  2  7  3
  7  2  6  1  3  5  4
  7  3  2  5  1  4  6

One of the possible solution(s):
                                                        
                                                        
                                                        
                                                        
 
        ****************      ****************          
        *              *      *              *          
        *      5       *      *      1       *          
        *              *      *              *          
        *          ****************      ***************
        *          *   *      *   *      *   *         *
        *          * 6 *      * 3 *      * 7 *         *
        *          *   *      *   *      *   *         *
        *          *   *      *   *      *   *         *
        ****************      ****************         *
                   *       2      *      *      4      *
                   *              *      *             *
                   *              *      *             *
                   ****************      ***************
When possible values for the alphabets are all digits, a command-line graphics output is provided. I just use stars to represent the edges, not as nice as the box-drawing characters in the problem statement.


Note that duplicate values as input parameters are not properly handled in my solutions.

Stay alert and healthy! □


Leave a comment

About C.-Y. Fung

user-pic a beginner in programming; passionate in mathematics, distance running and gender issues; from Hong Kong. https://twitter.com/e7_87