aboutsummaryrefslogtreecommitdiff
path: root/challenge-060
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-05-17 20:41:05 +0100
committerGitHub <noreply@github.com>2020-05-17 20:41:05 +0100
commit29a0f19b311412f73349aa84d7e1117c713d6911 (patch)
treee820e3f6617ceea32a0b73fdb97d3c3b2f02aeb1 /challenge-060
parent39b08bd942cf379f103f31ef0200a1d89768ab45 (diff)
parentdfce7317784e571ee8fd5e15170863aa910ceff2 (diff)
downloadperlweeklychallenge-club-29a0f19b311412f73349aa84d7e1117c713d6911.tar.gz
perlweeklychallenge-club-29a0f19b311412f73349aa84d7e1117c713d6911.tar.bz2
perlweeklychallenge-club-29a0f19b311412f73349aa84d7e1117c713d6911.zip
Merge pull request #1727 from E7-87-83/master
Cheok Yin's Submission for PWC#060
Diffstat (limited to 'challenge-060')
-rw-r--r--challenge-060/cheok-yin-fung/BLOG.txt1
-rw-r--r--challenge-060/cheok-yin-fung/perl/ch-1.pl84
-rw-r--r--challenge-060/cheok-yin-fung/perl/ch-2.pl50
3 files changed, 135 insertions, 0 deletions
diff --git a/challenge-060/cheok-yin-fung/BLOG.txt b/challenge-060/cheok-yin-fung/BLOG.txt
new file mode 100644
index 0000000000..074ee83c4d
--- /dev/null
+++ b/challenge-060/cheok-yin-fung/BLOG.txt
@@ -0,0 +1 @@
+http://blogs.perl.org/users/c_y_fung/2020/05/cys-unfinished-post-on-pwc060-numbers-with-headache.html
diff --git a/challenge-060/cheok-yin-fung/perl/ch-1.pl b/challenge-060/cheok-yin-fung/perl/ch-1.pl
new file mode 100644
index 0000000000..68df81f057
--- /dev/null
+++ b/challenge-060/cheok-yin-fung/perl/ch-1.pl
@@ -0,0 +1,84 @@
+#!/usr/bin/perl
+use strict;
+
+#usage:
+#Input: ch-1.pl -e NUMBER Output: corresponding Excel Column
+#Input: ch-1.pl -n ALPHABETAS Output: corresponding number
+
+my $MAX = 16384;
+
+
+if ($ARGV[0] eq '-e') {
+ if ($ARGV[1]<=$MAX and 1<=$ARGV[1]) {
+ print numtoExcelCol($ARGV[1])," \n";
+ } else {die "invalid argument";}
+}
+if ($ARGV[0] eq '-n') {
+ my $p = $ARGV[1];
+ if (($p ge "A" and $p le "Z") or ($p ge "AA" and $p le "ZZ")
+ or ($p ge "AAA" and $p le "ZZZ")) {
+ print excelcoltoNum($p), "\n";
+ } else {die "invalid argument";}
+}
+
+
+if ($ARGV[0] eq '-t') {
+ for (1..$MAX) {
+ print($_,"\n") unless $_ == excelcoltoNum(numtoExcelCol($_));
+ #For Testing
+ }
+}
+
+
+sub AtoOne {
+ my $c = $_[0];
+ return (1+ord($c)-ord('A'));
+}
+
+sub AtoZero {
+ my $c = $_[0];
+ return (ord($c)-ord('A'));
+}
+
+sub OnetoA {
+ my $f = $_[0];
+ return chr($f+ord('A')-1);
+}
+
+sub numtoExcelCol {
+ my $number = $_[0];
+ if ($number <= 26) {
+ return OnetoA($number);
+ }
+ if ($number > 26 and $number <= 26+676) {
+ my $temp = $number % 26 ;
+ $temp = 26 if $temp == 0;
+ return (OnetoA(($number-1)/26).OnetoA($temp));
+ }
+ if ($number > 26+676) {
+ my $r = int (($number - 1 - 26) /676);
+ my $s = $number - ($r * 676);
+ return (OnetoA($r).numtoExcelCol($s));
+ }
+}
+
+
+
+sub excelcoltoNum {
+ my $col = $_[0];
+ if (length($col) == 1) {
+ return AtoOne($col);
+ }
+ if (length($col) == 2) {
+ my @s = split //, $col;
+ return (AtoZero($s[0])*26+AtoOne($s[1])+26);
+ }
+ if (length($col) == 3) {
+ my @s = split //, $col;
+ return (AtoZero($s[0])*676
+ + AtoZero($s[1])*26
+ + AtoZero($s[2])
+ + 1 + 26 + 676);
+ }
+
+}
diff --git a/challenge-060/cheok-yin-fung/perl/ch-2.pl b/challenge-060/cheok-yin-fung/perl/ch-2.pl
new file mode 100644
index 0000000000..5aa2c1a88d
--- /dev/null
+++ b/challenge-060/cheok-yin-fung/perl/ch-2.pl
@@ -0,0 +1,50 @@
+#!/usr/bin/perl
+use strict;
+use List::MoreUtils qw(uniq);
+use Math::Combinatorics;
+
+my @L = (0, 1, 2, 5);
+my $X = 2;
+my $Y = 21;
+
+my @freq;
+my @modifiedL = uniq @L;
+for (0..$#L) {
+ if ($_ != 0) {
+ $freq[$_] = $X/length($modifiedL[$_]) ;
+ }
+ else {
+ $freq[$_] = $X;
+ }
+
+}
+
+
+my @combo = ();
+my @unique_n = ();
+
+for (1..$X) {
+ my $A = Math::Combinatorics->new(
+ count=>$_,
+ data => [@modifiedL],
+ frequency => [@freq] );
+ while (my @candidates = $A->next_multiset) {
+ my $candidate = join "", @candidates;
+ my $C = Math::Combinatorics->new(
+ count=>length($candidate),
+ data => [@candidates]);
+ while (my @Bl = $C->next_permutation) {
+ my $B = join "", @Bl;
+ $B = $B+0;
+ if (length($B)==$X) {
+ if ($B < $Y and 0 < $B) {
+ push @combo, $B;
+ }
+ }
+ }
+ }
+}
+
+@unique_n = uniq @combo;
+print join " ," , sort @unique_n;
+print "\n";