diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-12-23 19:02:12 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-12-23 19:02:12 +0000 |
| commit | 66708f6ceee6a32096961f5172b5bb204e98db21 (patch) | |
| tree | fd75bf3a20ba4f12a48c5c3de7042b4a0395ae94 | |
| parent | 771547dd9c46b0bf9f3faaf5fa268a2c36fe6210 (diff) | |
| parent | c0dff1b486144f2bab92d842273b7841e787b1bd (diff) | |
| download | perlweeklychallenge-club-66708f6ceee6a32096961f5172b5bb204e98db21.tar.gz perlweeklychallenge-club-66708f6ceee6a32096961f5172b5bb204e98db21.tar.bz2 perlweeklychallenge-club-66708f6ceee6a32096961f5172b5bb204e98db21.zip | |
Merge pull request #1065 from adamcrussell/challenge-039
Challenge 039
| -rw-r--r-- | challenge-038/adam-russell/perl/ch-1.pl | 50 | ||||
| -rw-r--r-- | challenge-038/adam-russell/perl/ch-2.pl | 79 | ||||
| -rw-r--r-- | challenge-039/adam-russell/perl/Rpn.yp | 2 | ||||
| -rw-r--r-- | challenge-039/adam-russell/perl/ch-2.pl | 4 |
4 files changed, 4 insertions, 131 deletions
diff --git a/challenge-038/adam-russell/perl/ch-1.pl b/challenge-038/adam-russell/perl/ch-1.pl deleted file mode 100644 index 5baaad941b..0000000000 --- a/challenge-038/adam-russell/perl/ch-1.pl +++ /dev/null @@ -1,50 +0,0 @@ -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/perl/ch-2.pl b/challenge-038/adam-russell/perl/ch-2.pl deleted file mode 100644 index f6f1615d5c..0000000000 --- a/challenge-038/adam-russell/perl/ch-2.pl +++ /dev/null @@ -1,79 +0,0 @@ -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"; -} diff --git a/challenge-039/adam-russell/perl/Rpn.yp b/challenge-039/adam-russell/perl/Rpn.yp index d27d0b7bcd..d61d952b40 100644 --- a/challenge-039/adam-russell/perl/Rpn.yp +++ b/challenge-039/adam-russell/perl/Rpn.yp @@ -5,7 +5,7 @@ line: '\n' | rpn '\n' {print $_[1] . "\n"} ; rpn: NUMBER - | rpn rpn '+' { $_[1] + $_[2]} + | rpn rpn '+' {$_[1] + $_[2]} | rpn rpn '-' {$_[1] - $_[2]} | rpn rpn '*' {$_[1] * $_[2]} | rpn rpn '/' {$_[1] / $_[2]} diff --git a/challenge-039/adam-russell/perl/ch-2.pl b/challenge-039/adam-russell/perl/ch-2.pl index 52d9a91e10..f1c333496e 100644 --- a/challenge-039/adam-russell/perl/ch-2.pl +++ b/challenge-039/adam-russell/perl/ch-2.pl @@ -8,11 +8,13 @@ use constant RPN_ADD => "10 8 + \n"; use constant RPN_SUBTRACT => "18 66 - \n"; use constant RPN_MULTIPLY => "10 8 * \n"; use constant RPN_DIVIDE => "52 2 / \n"; +use constant RPN => "2 2 + 3 3 + + 1 7 - + \n"; MAIN:{ my $parser = new Rpn(); $parser->parse(RPN_ADD); $parser->parse(RPN_SUBTRACT); $parser->parse(RPN_MULTIPLY); - $parser->parse(RPN_DIVIDE); + $parser->parse(RPN_DIVIDE); + $parser->parse(RPN); } |
