aboutsummaryrefslogtreecommitdiff
path: root/challenge-038
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2019-12-15 18:54:55 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2019-12-15 18:54:55 +0000
commita382243c8d3456544bbbf7de88a19e792a4b41a2 (patch)
tree2967f8735cd99849752c66b6b0d0201bb32c4629 /challenge-038
parent8661bf1f0f8f74e653ee80595e876880de0ee76c (diff)
downloadperlweeklychallenge-club-a382243c8d3456544bbbf7de88a19e792a4b41a2.tar.gz
perlweeklychallenge-club-a382243c8d3456544bbbf7de88a19e792a4b41a2.tar.bz2
perlweeklychallenge-club-a382243c8d3456544bbbf7de88a19e792a4b41a2.zip
- Added solutions by Adam Russell.
Diffstat (limited to 'challenge-038')
-rw-r--r--challenge-038/adam-russell/perl5/ch-1.pl50
-rw-r--r--challenge-038/adam-russell/perl5/ch-2.pl79
2 files changed, 129 insertions, 0 deletions
diff --git a/challenge-038/adam-russell/perl5/ch-1.pl b/challenge-038/adam-russell/perl5/ch-1.pl
new file mode 100644
index 0000000000..5baaad941b
--- /dev/null
+++ b/challenge-038/adam-russell/perl5/ch-1.pl
@@ -0,0 +1,50 @@
+use strict;
+use warnings;
+##
+# Create a script to accept a 7 digits number, where
+# the first number can only be 1 or 2. The second and
+# third digits can be anything 0-9. The fourth and
+# fifth digits corresponds to the month i.e. 01,02,03,...,11,12.
+# And the last 2 digits respresents the days in the
+# month i.e. 01,02,03,...,29,30,31. Your script should validate
+# if the given number is valid as per the rule and then
+# convert into human readable format date.
+# RULES
+# 1) If 1st digit is 1, then prepend 20 otherwise 19 to the
+# 2nd and 3rd digits to make it 4-digits year.
+# 2) The 4th and 5th digits together should be a valid month.
+# 3) The 6th and 7th digits together should be a valid day
+# for the above month.
+##
+use boolean;
+sub is_valid{
+ my($x) = @_;
+ $x =~ m/([1-2]{1})([0-9]{2})(0[1-9]{1}|1[012]{1})(0[1-9]{1}|1[012]{1}|2[0-9]{1})/;
+ return $x, $1, $2, $3, $4;
+}
+
+sub transform{
+ my($a, $b, $c, $d) = @_;
+ if($a == 1){
+ $a = "20$a";
+ }
+ else{
+ $a = "19$b";
+ }
+ return "$a-$c-$d";
+}
+
+MAIN:{
+ while(my $x = <DATA>){
+ my ($valid, @fields) = is_valid($x);
+ unless(!$valid){
+ print transform(@fields) . "\n";
+ }
+ else{
+ print "invalid: $x\n";
+ }
+ }
+}
+
+__DATA__
+2230120
diff --git a/challenge-038/adam-russell/perl5/ch-2.pl b/challenge-038/adam-russell/perl5/ch-2.pl
new file mode 100644
index 0000000000..f6f1615d5c
--- /dev/null
+++ b/challenge-038/adam-russell/perl5/ch-2.pl
@@ -0,0 +1,79 @@
+use strict;
+use warnings;
+##
+# Lets assume we have tiles with an alphabet (A..Z) printed on them.
+# Each tile has a value, e.g. A (1 point), B (4 points) etc. You are
+# allowed to draw 7 tiles from the lot randomly. Then try to form a
+# word using the 7 tiles with maximum points altogether. You don’t
+# have to use all the 7 tiles to make a word. You should try to use
+# as many tiles as possible to get the maximum points.
+##
+use constant DICTIONARY => "/usr/share/dict/words";
+use Readonly;
+Readonly::Hash my %CHAR_VALUES => (
+ A => 1, B => 4, C => 5, D => 3, E => 2,
+ F => 3, G => 1, H => 5, I => 1, J => 2,
+ K => 10, L => 2, M => 5, N => 4, O => 5,
+ P => 3, Q => 10, R => 2, S => 1, T => 5,
+ U => 1, V => 2, W => 3, X => 1, Y => 2,
+ Z => 1
+);
+
+Readonly::Hash my %CHAR_FREQUENCIES => (
+ A => 8, B => 5, C => 4, D => 3, E => 9,
+ F => 3, G => 3, H => 3, I => 5, J => 3,
+ K => 2, L => 3, M => 4, N => 4, O => 3,
+ P => 5, Q => 2, R => 3, S => 7, T => 5,
+ U => 5, V => 3, W => 5, X => 2, Y => 5,
+ Z => 5
+);
+
+sub draw7{
+ my @letters;
+ my @seven;
+ for my $k (keys %CHAR_FREQUENCIES){
+ push @letters, ($k) x $CHAR_FREQUENCIES{$k};
+ }
+ for(0..6){
+ my $r = int(rand(@letters));
+ push @seven, splice @letters, $r, 1;
+ }
+ return @seven;
+}
+
+sub find_max_score{
+ my @seven = @_;
+ my $max_score = 0;
+ my $max_word;
+ my $max_letters;
+ open(WORDS, DICTIONARY);
+ while(<WORDS>){
+ chomp($_);
+ my $word = uc($_);
+ my $temp = $word;
+ my @letters;
+ for my $c (@seven){
+ $word =~ s/(\Q$c\E)//;
+ push @letters, $1 if $1;
+ }
+ if(!$word){
+ my $sum = 0;
+ map {$sum += $_} map{$CHAR_VALUES{$_}} @letters;
+ if($max_score < $sum){
+ $max_word = $temp;
+ $max_letters = join(", " , @letters);
+ $max_score = $sum;
+ }
+ }
+ }
+ close(WORDS);
+ return $max_word, $max_letters, $max_score;
+}
+
+MAIN:{
+ my @letters = draw7();
+ print "LETTERS: " . join(", ", @letters) . "\n";
+ my ($word, $letters, $score) = find_max_score(@letters);
+ print "$word has a score of $score.\n";
+ print "These letters were used: $letters.\n";
+}