aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-07-20 12:50:34 +0100
committerGitHub <noreply@github.com>2021-07-20 12:50:34 +0100
commit037c36d199a7426f57a06822bd08e1c66af8dd33 (patch)
tree391082bd6b7fbda0fa1e22cd22e03a8afb9d5c29
parent9ff2f5368866e9e637f8f27f3488178cf6ee8501 (diff)
parent6c801276b98823d0db0b217c597fa22d6874d6b3 (diff)
downloadperlweeklychallenge-club-037c36d199a7426f57a06822bd08e1c66af8dd33.tar.gz
perlweeklychallenge-club-037c36d199a7426f57a06822bd08e1c66af8dd33.tar.bz2
perlweeklychallenge-club-037c36d199a7426f57a06822bd08e1c66af8dd33.zip
Merge pull request #4558 from E7-87-83/newt
ch-1 in wk 122
-rw-r--r--challenge-122/cheok-yin-fung/perl/ch-1.pl21
-rw-r--r--challenge-122/cheok-yin-fung/perl/ch-2.pl83
-rw-r--r--logo-design/cheok-yin-fung/README.md1
-rw-r--r--logo-design/cheok-yin-fung/logo17
4 files changed, 122 insertions, 0 deletions
diff --git a/challenge-122/cheok-yin-fung/perl/ch-1.pl b/challenge-122/cheok-yin-fung/perl/ch-1.pl
new file mode 100644
index 0000000000..c05fd47caf
--- /dev/null
+++ b/challenge-122/cheok-yin-fung/perl/ch-1.pl
@@ -0,0 +1,21 @@
+#!/usr/bin/perl
+# The Weekly Challenge - 122
+# Task 1 Average of Stream
+use strict;
+use warnings;
+use v5.10.0;
+say "Keep entering a number and the average of the stream will be printed.";
+my $num = <STDIN>;
+my $numeric = qr/^[+-]?\d+.?\d*$/;
+exit if $num !~ /$numeric/;
+my $n = 1;
+my $sum = $num;
+printf "Average of the first number is %.3f\n", $num;
+$num = <STDIN>;
+while ($num =~ m/$numeric/ ){
+ $sum += $num;
+ $n++;
+ printf "Average of the first $n numbers is %.3f\n", $sum/$n;
+ $num = <STDIN>;
+}
+
diff --git a/challenge-122/cheok-yin-fung/perl/ch-2.pl b/challenge-122/cheok-yin-fung/perl/ch-2.pl
new file mode 100644
index 0000000000..d6196d6851
--- /dev/null
+++ b/challenge-122/cheok-yin-fung/perl/ch-2.pl
@@ -0,0 +1,83 @@
+#!/usr/bin/perl
+# The Weekly Challenge 122
+# Task 2 Basketball Points
+use strict;
+use warnings;
+use v5.10.0;
+use Algorithm::Combinatorics qw /combinations/;
+
+my $S = $ARGV[0] || 5;
+my @ans;
+
+if ($S == 1) {
+ @ans = ("1");
+}
+elsif ($S == 2) {
+ @ans = ("11","2");
+}
+else {
+ push @ans, reverse oneortwo_str($S)->@*;
+ for my $k (1..int $S/3) {
+ my @ans_three333;
+ my $iter = combinations([0..$S-1-2*$k], $k);
+ while (my $c = $iter->next) {
+ my $str_t = "*" x $S;
+ my $len = $S-2*$k;
+ substr($str_t, $c->[0], 3) = "3";
+ if ($k > 1) {
+ for my $t (reverse 1..$k-1) {
+ substr($str_t, 2*($t-1)+$c->[$t], 3) = "3";
+ }
+ }
+ push @ans_three333, $str_t;
+ }
+ push @ans, @{expand($_)} for (reverse @ans_three333);
+ }
+}
+
+say "Number of ways: " , scalar @ans;
+say "";
+say join "\n", @ans;
+
+sub expand {
+ my $str = $_[0];
+ my @expand_arr;
+ if ($str =~ /\*/) {
+ my $pos = index($str,"*");
+ my $next_3 = index($str, "3", $pos+1);
+ my $len_of_stars =
+ $next_3 == -1 ?
+ (length $str) - $pos :
+ $next_3-$pos;
+
+ for my $onetwo (oneortwo_str($len_of_stars)->@*) {
+ my $temp_str = $str;
+ substr($temp_str, $pos, $len_of_stars) = $onetwo;
+ push @expand_arr, @{expand($temp_str)};
+ }
+ }
+ else {
+ push @expand_arr, $str;
+ }
+ return [@expand_arr];
+}
+
+
+# codes from challenge 112
+sub oneortwo_str {
+ my $n = $_[0];
+ my @one_or_two;
+ return [] if $n == 0;
+ return ["1"] if $n == 1;
+ for my $i ($n%2+$n/2 .. $n-1) {
+ my $iter = combinations([0..$i-1] , ($n-$i) );
+ my $str = "1" x $i;
+ while (my $c = $iter->next) {
+ my $str_clone = $str;
+ substr($str_clone, $_, 1) = "2" for (@{$c});
+ push @one_or_two, $str_clone;
+ }
+ }
+ push @one_or_two , "1" x $n;
+ return [@one_or_two];
+}
diff --git a/logo-design/cheok-yin-fung/README.md b/logo-design/cheok-yin-fung/README.md
new file mode 100644
index 0000000000..892ba2bcd1
--- /dev/null
+++ b/logo-design/cheok-yin-fung/README.md
@@ -0,0 +1 @@
+This is not a serious attempt. :)
diff --git a/logo-design/cheok-yin-fung/logo b/logo-design/cheok-yin-fung/logo
new file mode 100644
index 0000000000..c1919407e0
--- /dev/null
+++ b/logo-design/cheok-yin-fung/logo
@@ -0,0 +1,17 @@
+#!/usr/bin/perl
+#!/usr/bin/env raku
+
+#
+#
+# ***** * ** ** * * **** * * *
+# * * * * * * * * * * *
+# * **** *** * * * *** *** * * * * * * *** *** * * *** *** *** ***
+# * * * ** * * * ** ** ** * * * * * * * * * * ** * * * * **
+# * * * *** ***** *** *** * * * * **** * * **** * * *** * * *** ***
+# * *
+# * ***
+
+
+say 'https://theweeklychallenge.org/';
+
+