From 201c70e6d8d5374f4cecbc5712eec671266b3702 Mon Sep 17 00:00:00 2001 From: E7-87-83 Date: Sun, 25 Apr 2021 23:40:26 +0800 Subject: 2 Perl scripts ; Java application for Task 2 --- challenge-108/cheok-yin-fung/perl/ch-2.pl | 12 +- challenge-109/cheok-yin-fung/java/Box.java | 34 ++++ .../cheok-yin-fung/java/NBoxesJavaFX.java | 201 +++++++++++++++++++++ challenge-109/cheok-yin-fung/java/Point.java | 19 ++ challenge-109/cheok-yin-fung/perl/ch-1.pl | 16 ++ challenge-109/cheok-yin-fung/perl/ch-2.pl | 178 ++++++++++++++++++ 6 files changed, 457 insertions(+), 3 deletions(-) create mode 100644 challenge-109/cheok-yin-fung/java/Box.java create mode 100644 challenge-109/cheok-yin-fung/java/NBoxesJavaFX.java create mode 100644 challenge-109/cheok-yin-fung/java/Point.java create mode 100644 challenge-109/cheok-yin-fung/perl/ch-1.pl create mode 100644 challenge-109/cheok-yin-fung/perl/ch-2.pl diff --git a/challenge-108/cheok-yin-fung/perl/ch-2.pl b/challenge-108/cheok-yin-fung/perl/ch-2.pl index 8fa66de30f..d0208714e3 100644 --- a/challenge-108/cheok-yin-fung/perl/ch-2.pl +++ b/challenge-108/cheok-yin-fung/perl/ch-2.pl @@ -5,6 +5,12 @@ # Usage: $ ch-1.pl N # first N Bell numbers with the 0th case +# formula used in this script, essentially a rotten version of: +# > a(n) = Sum_{i=1..P(n)} (n!/(Product_{j=1..p(i)}p(i,j)!)) * (1/(Product_{j=1..d(i)} m(i,j)!)) +# > - Thomas Wieder, May 18 2005 +# For details, see http://oeis.org/A000110 + + use strict; use warnings; use Integer::Partition; @@ -13,10 +19,10 @@ use Integer::Partition; # n_C_r sub combin { my ($n, $r) = ($_[0], $_[1]); - my $a = 1; + my $ans = 1; $a *= $_ for ($n-$r+1..$n); $a /= $_ for (1..$r); - return $a; + return $ans; } sub factorial { @@ -59,7 +65,7 @@ sub dup { for (1..$#arr) { if ($preced != $arr[$_]) { $ans *= factorial($dup_term) if $dup_term != 1; - # the if clause, saves a of bit time + # the if clause, saves a bit of time $preced = $arr[$_]; $dup_term = 1; } diff --git a/challenge-109/cheok-yin-fung/java/Box.java b/challenge-109/cheok-yin-fung/java/Box.java new file mode 100644 index 0000000000..4f3b0cdcc6 --- /dev/null +++ b/challenge-109/cheok-yin-fung/java/Box.java @@ -0,0 +1,34 @@ +// The Weekly Challenge - 109 +// Task 2 Four Squares(Rectangles) Puzzle +// Supporting codes + +class Box +{ + private Point tl, br; + + Box(int x1, int y1, int x2, int y2) { + tl = new Point(x1, y1); + br = new Point(x2, y2); + } + + Box(Point p1, Point p2) { + this(p1.x(), p1.y(), p2.x(), p2.y()); + } + + Point topLeft() { + return tl; + } + + int height() { + return br.x()-tl.x(); + } + + int width() { + return br.y()-tl.y(); + } + + boolean isEnclosed(Point p) { + return p.x() > tl.x() && p.x() < br.x() && + p.y() > tl.y() && p.y() < br.y(); + } +} diff --git a/challenge-109/cheok-yin-fung/java/NBoxesJavaFX.java b/challenge-109/cheok-yin-fung/java/NBoxesJavaFX.java new file mode 100644 index 0000000000..ad507def6e --- /dev/null +++ b/challenge-109/cheok-yin-fung/java/NBoxesJavaFX.java @@ -0,0 +1,201 @@ +// The Weekly Challenge - 109 +// Task 2 Four Squares(Rectangles) Puzzle +// Run with Box.java and Point.java +// JavaFX Usage may refer to https://github.com/openjfx/samples/tree/master/HelloFX +// Coding References: +// https://www.tutorialspoint.com/how-to-create-a-rectangle-using-javafx +// https://www.javatpoint.com/javafx-text + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.BitSet; + + +// JavaFX /* + +import javafx.application.Application; +import javafx.scene.Group; +import javafx.scene.Scene; +import javafx.scene.paint.Color; +import javafx.stage.Stage; +import javafx.scene.shape.Rectangle; +import javafx.scene.text.*; + +// */ JavaFX + +public class NBoxesJavaFX extends Application +// public class NBoxesJavaFX // for non-JavaFX +{ + + static Box[] box = new Box[] { + new Box( new Point(9,6), new Point(24,15) ), + new Box( new Point(20,10), new Point(35,19) ), + new Box( new Point(31,6), new Point(46,15) ), + new Box( new Point(42,10), new Point(56,19) ) + }; + + static Point[] point = new Point[] { + new Point(16,8), //a + new Point(22,12), //b + new Point(28,16), //c + new Point(33,12), //d + new Point(38,8), //e + new Point(44,12), //f + new Point(49,16) //g + }; + + public static final int N = box.length; + public static final int M = point.length; + + public static int[] oneOfAns = new int[M]; + public static ArrayList finalAnswers = new ArrayList<>(); + +// ========== BEGIN: functions for permutations ================== + public static ArrayList copy(ArrayList val) + { + ArrayList a = new ArrayList<>(); + for (int i=0; i < val.size(); i++) { + a.add(Integer.valueOf(val.get(i))); + } + return a; + } + + public static ArrayList permutations(int[] values) + { + int m = values.length; + ArrayList> arr = new ArrayList<>(); + ArrayList temp = new ArrayList<>(); + temp.add(Integer.valueOf(values[0])); + arr.add(temp); + for (int i = 2; i <= m; i++) { + int cp = arr.size(); + for (int c=0; c < i-1; c++) + for (int d=0; d < cp; d++) { + arr.add( copy( arr.get(d) ) ); + } + int count = 0; + int parameter = arr.size()/i; + for (int k=0; k < i ; k++) { + for (int c = 0; c < parameter; c++) { + ArrayList item = arr.get(count); + item.add(k, Integer.valueOf(values[i-1])); + count++; + } + } + + } + + ArrayList ans = new ArrayList<>(); + Iterator> i_arr = arr.iterator(); + while (i_arr.hasNext()) { + ans.add(toIntArray( i_arr.next() )); + } + return ans; + } + + public static int[] toIntArray (ArrayList sth) { + int[] ans = new int[sth.size()]; + for (int i = 0; i < sth.size(); i++ ) + ans[i] = sth.get(i); + return ans; + } + +// =========== END: functions for permutations ================== + +// =========== BEGIN: functions for JavaFX ======================== +// /* + public void start(Stage stage) + { + Group group = new Group(); + + Rectangle[] shape = new Rectangle[N]; + for (int i=0; i p = permutations(new int[] {1,2,3,4,5,6,7}); + + + + Iterator i_p = p.iterator(); + while (i_p.hasNext()) { + int[] now = i_p.next(); + + boolean consistency = true; + int sum = 0; + for (int j=0; j $_[1], + _y => $_[2], + }, $class; + } + + sub x { $_[0]->{_x} } + sub y { $_[0]->{_y} } + +} + +{ + package Box; + use strict; + + sub new { + my ($class) = @_; + bless { + _tl => $_[1], #top left + _br => $_[2], #bottem right + }, $class; + } + + sub tl { $_[0]->{_tl} } + sub br { $_[0]->{_br} } + +} + + +# ================= BEGIN: Parameters input ============== + +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 +); + +my @possible_val = (1,2,3,4,5,6,7); + + +# ================= END: Parameters input ============== + +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]->y > $b->tl->y + && $var[$v_ind]->y < $b->br->y) { + push @{$container[$i_b]}, $v_ind; + } + } +} + +my $trial = permutations(\@possible_val); + +my @possible_soln; + +while (my $p = $trial->next) { + # attempt_soln ---> @{$p}; + my $invariant = 0; + for (@{$container[ 0 ]}) { + $invariant += $p->[$_]; } + my $consistency = 1; + for my $i_b (1..$N-1) { + my $this_sum = 0; + for (@{$container[ $i_b ]}) { + $this_sum += $p->[$_]; } + $consistency = ($this_sum == $invariant); + last if !$consistency; + } + push @possible_soln, $p if $consistency; +} + +my $NUM_ANS = scalar @possible_soln; +print "Number of solutions: ", $NUM_ANS, "\n"; + +if ($NUM_ANS != 0) { + print " ",chr(ord('a')+$_) for (0..$M-1); + print "\n"; + for my $temp (0..$#possible_soln) { + for my $number (@{$possible_soln[$temp]}) { + printf("%3d", $number); + } + print "\n"; + } + print "\n"; +} + + + +sub printing { + my @soln = @_; + my $max_x = 0; + my $max_y = 0; + for my $b (@boxes) { + $max_x = $max_x > $b->br->x ? $max_x : $b->br->x; + $max_y = $max_y > $b->br->y ? $max_y : $b->br->y; + } + my @row; + $row[$_] = [(' ') x $max_x] for (0..$max_y-1); + for my $i_b (0..$N-1) { + my $b = $boxes[$i_b]; + for my $i ($b->tl->x-1..$b->br->x-1) {$row[$b->tl->y-1][$i] = '*';} + for my $i ($b->tl->x-1..$b->br->x-1) {$row[$b->br->y-1][$i] = '*';} + for my $i ($b->tl->y-1..$b->br->y-1) {$row[$i][$b->tl->x-1] = '*';} + for my $i ($b->tl->y-1..$b->br->y-1) {$row[$i][$b->br->x-1] = '*';} + } + for my $i_v (0..$M-1) { + $row[$var[$i_v]->y-1]->[$var[$i_v]->x-1] = $soln[$i_v]; + } + + print "One of the possible solution(s):\n"; + + for my $j (0..$max_y-1) { + for my $i (0..$max_x-1) { + print $row[$j]->[$i]; + } + print "\n"; + } + + print "\n" x 3; +} + +printing(@{$possible_soln[ int(rand($NUM_ANS))]}) + if (scalar (map {length $_ == 1} @possible_val) == $M) && $NUM_ANS != 0; -- cgit From 16904d5e7e4ac16c0cfb6c961a4ee68422a9c8b1 Mon Sep 17 00:00:00 2001 From: E7-87-83 Date: Mon, 26 Apr 2021 01:13:11 +0800 Subject: 2 Perl scripts ; Java application for Task 2; blog done! --- challenge-109/cheok-yin-fung/blog.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-109/cheok-yin-fung/blog.txt diff --git a/challenge-109/cheok-yin-fung/blog.txt b/challenge-109/cheok-yin-fung/blog.txt new file mode 100644 index 0000000000..9266ddcd8c --- /dev/null +++ b/challenge-109/cheok-yin-fung/blog.txt @@ -0,0 +1 @@ +http://blogs.perl.org/users/c_y_fung/2021/04/cys-take-on-pwc109.html -- cgit