aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-05-24 17:49:52 +0100
committerGitHub <noreply@github.com>2019-05-24 17:49:52 +0100
commitd29c5d3a339f8da08859699198029bff66385632 (patch)
tree5160695f7884c70cfd676894eb6bcefdfcdef3aa
parenta797dd9e2e57f7e13a3979fd97bcbf1ab4150678 (diff)
parent5ce43388c421c75bcf3d2bf29761386af2b38856 (diff)
downloadperlweeklychallenge-club-d29c5d3a339f8da08859699198029bff66385632.tar.gz
perlweeklychallenge-club-d29c5d3a339f8da08859699198029bff66385632.tar.bz2
perlweeklychallenge-club-d29c5d3a339f8da08859699198029bff66385632.zip
Merge pull request #171 from jmaslak/joelle-9-3-1
Solutions for week 9, challenge 2 in P5 and P6
-rwxr-xr-xchallenge-009/joelle-maslak/perl5/ch-2.pl69
-rw-r--r--challenge-009/joelle-maslak/perl5/ch-2.txt6
-rwxr-xr-xchallenge-009/joelle-maslak/perl6/ch-2.p659
3 files changed, 134 insertions, 0 deletions
diff --git a/challenge-009/joelle-maslak/perl5/ch-2.pl b/challenge-009/joelle-maslak/perl5/ch-2.pl
new file mode 100755
index 0000000000..b026ffe86e
--- /dev/null
+++ b/challenge-009/joelle-maslak/perl5/ch-2.pl
@@ -0,0 +1,69 @@
+#!/usr/bin/env perl
+use v5.24;
+use strict;
+use warnings;
+use autodie;
+
+#
+# Copyright (C) 2019 Joelle Maslak
+# All Rights Reserved - See License
+#
+
+#
+# Source file contains lines in the following format:
+#
+# <score><tab><description>
+#
+# So, for instance:
+# 32 Foo
+# 40 Bar
+# 40 Baz
+# 66 Quux
+# 67 Quuux
+# 67 Quuuux
+#
+# There should be no tabs other than the single tab seperating score
+# from description.
+#
+# Scores are assumed to be integers that can be printed with %5d.
+
+# Turn on method signatures
+use feature 'signatures';
+no warnings 'experimental::signatures';
+
+die("Only one argument may be supplied - file to read") if scalar(@ARGV) >= 2;
+my $srcfile = $ARGV[0] // 'ch-2.txt';
+
+my ( @lines, %bag );
+open my $fh, '<', $srcfile;
+while ( my $line = <$fh> ) {
+ chomp $line;
+
+ my $pair = [ split /\t/, $line ];
+ push @lines, $pair;
+ $bag{ $pair->[0] }++;
+}
+
+@lines = reverse
+ sort { ( $a->[0] <=> $b->[0] ) or ( $b->[1] cmp $a->[1] ) } @lines;
+
+say " STD MOD DENSE SCORE DESCRIPTION";
+
+my $standard = 0;
+my $modified = 0;
+my $dense = 0;
+my $lastscore;
+for my $pair (@lines) {
+ my $score = $pair->[0];
+ my $desc = $pair->[1];
+
+ if ( ( !defined($lastscore) ) or ( $lastscore != $score ) ) {
+ $standard += defined($lastscore) ? $bag{$lastscore} : 1;
+ $modified += $bag{$score};
+ $dense++;
+ $lastscore = $score;
+ }
+
+ printf( "%5d %5d %5d %5d %s\n", $standard, $modified, $dense, $score, $desc );
+}
+
diff --git a/challenge-009/joelle-maslak/perl5/ch-2.txt b/challenge-009/joelle-maslak/perl5/ch-2.txt
new file mode 100644
index 0000000000..d4c6bb4f03
--- /dev/null
+++ b/challenge-009/joelle-maslak/perl5/ch-2.txt
@@ -0,0 +1,6 @@
+32 Foo
+40 Bar
+40 Baz
+66 Quux
+67 Quuux
+67 Quuuux
diff --git a/challenge-009/joelle-maslak/perl6/ch-2.p6 b/challenge-009/joelle-maslak/perl6/ch-2.p6
new file mode 100755
index 0000000000..ac9b6fc589
--- /dev/null
+++ b/challenge-009/joelle-maslak/perl6/ch-2.p6
@@ -0,0 +1,59 @@
+#!/usr/bin/env perl6
+use v6;
+
+#
+# Copyright © 2019 Joelle Maslak
+# All Rights Reserved - See License
+#
+
+#
+# Source file contains lines in the following format:
+#
+# <score><tab><description>
+#
+# So, for instance:
+# 32 Foo
+# 40 Bar
+# 40 Baz
+# 66 Quux
+# 67 Quuux
+# 67 Quuuux
+#
+# There should be no tabs other than the single tab seperating score
+# from description.
+#
+# Scores are assumed to be integers that can be printed with %5d.
+
+sub MAIN(
+ Str:D :$src-file? = "ch-2.txt";
+) {
+ my @lines = $src-file.IO.lines».split("\t").map( { Pair.new($_[0], $_[1]) } );
+ @lines = @lines.sort( { ($^a.key <=> $^b.key) or ($^b.value cmp $^a.value) } ).reverse;
+
+ my $bag = bag @lines.map( { $^a.key } );
+
+ say " STD MOD DENSE SCORE DESCRIPTION";
+
+ my $standard = 0;
+ my $modified = 0;
+ my $dense = 0;
+ my $last-score;
+ for @lines -> $pair {
+ my $score = $pair.key;
+ my $desc = $pair.value;
+
+ if (! $last-score.defined) or $last-score ≠ $score {
+ $standard += $last-score.defined ?? $bag{$last-score} !! 1;
+ $modified += $bag{$score};
+ $dense++;
+ $last-score = $score;
+ }
+
+ print $standard.fmt("%5d") ~ " ";
+ print $modified.fmt("%5d") ~ " ";
+ print $dense\ .fmt("%5d") ~ " ";
+ print $score\ .fmt("%5d") ~ " ";
+ say $desc;
+ }
+}
+