aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-11-03 14:54:18 +0000
committerGitHub <noreply@github.com>2019-11-03 14:54:18 +0000
commitb72956c9a4fcf7de3049d4ca1cf61afca49dc82a (patch)
tree558cd93f9fbe58fa5965d3df3697ac408d3e1042
parentb9f5bb88a4dc66e00946af30e95d9c189cdb954f (diff)
parent900b2ac1c872f49de2a61c96053b262e6e33cba8 (diff)
downloadperlweeklychallenge-club-b72956c9a4fcf7de3049d4ca1cf61afca49dc82a.tar.gz
perlweeklychallenge-club-b72956c9a4fcf7de3049d4ca1cf61afca49dc82a.tar.bz2
perlweeklychallenge-club-b72956c9a4fcf7de3049d4ca1cf61afca49dc82a.zip
Merge pull request #879 from noudald/challenge-032-noud
Solutions to challenge 32 problem 1 and 2 in Perl 6 by Noud
-rw-r--r--challenge-032/noud/perl6/ch1.p640
-rw-r--r--challenge-032/noud/perl6/ch2.p648
2 files changed, 88 insertions, 0 deletions
diff --git a/challenge-032/noud/perl6/ch1.p6 b/challenge-032/noud/perl6/ch1.p6
new file mode 100644
index 0000000000..a71e1598b4
--- /dev/null
+++ b/challenge-032/noud/perl6/ch1.p6
@@ -0,0 +1,40 @@
+# Count instances
+#
+# Create a script that either reads standard input or one or more files
+# specified on the command-line. Count the number of times and then print a
+# summary, sorted by the count of each entry.
+#
+# So with the following input in file example.txt
+#
+# apple
+# banana
+# apple
+# cherry
+# cherry
+# apple
+#
+# the script would display something like:
+#
+# apple 3
+# cherry 2
+# banana 1
+#
+# For extra credit, add a -csv option to your script, which would generate:
+#
+# apple,3
+# cherry,2
+# banana,1
+
+sub MAIN(*@files, Bool :$csv) {
+ my %word_count;
+ for @files -> $file {
+ for $file.IO.words -> $word {
+ %word_count{$word}++;
+ }
+ }
+
+ my $infix = gather { if $csv { take "," } else { take ":\t" } };
+ for %word_count.sort(-*.value)>>.kv -> ($word, $count) {
+ say "$word$infix$count";
+ }
+}
diff --git a/challenge-032/noud/perl6/ch2.p6 b/challenge-032/noud/perl6/ch2.p6
new file mode 100644
index 0000000000..63ce2274d1
--- /dev/null
+++ b/challenge-032/noud/perl6/ch2.p6
@@ -0,0 +1,48 @@
+# ASCII bar chart
+#
+# Write a function that takes a hashref where the keys are labels and the
+# values are integer or floating point values. Generate a bar graph of the data
+# and display it to stdout.
+#
+# The input could be something like:
+#
+# $data = { apple => 3, cherry => 2, banana => 1 };
+# generate_bar_graph($data);
+#
+# And would then generate something like this:
+#
+# apple | ############
+# cherry | ########
+# banana | ####
+#
+# If you fancy then please try this as well: (a) the function could let you
+# specify whether the chart should be ordered by (1) the labels, or (2) the
+# values.
+
+sub generate_bar_graph(%data, $sort-on="value") {
+ my $vmin = %data.values.min - 1;
+ my $vmax = %data.values.max;
+ constant $width = 79;
+
+ my @count_array;
+ if ($sort-on === "value") {
+ @count_array = %data.sort(-*.value)>>.kv;
+ } elsif ($sort-on === "key") {
+ @count_array = %data.sort(*.key)>>.kv;
+ } else {
+ die "Unknown sorting argument: $sort-on";
+ }
+
+ for @count_array -> ($word, $count) {
+ my $times = Int(($count - $vmin) / ($vmax - $vmin) * $width);
+ say "$word:\t" ~ "#" x $times;
+ }
+}
+
+my $data = { 'apple' => 3, 'cherry' => 2, 'banana' => 1 };
+
+say "Sort on value";
+generate_bar_graph($data, "value");
+
+say "\nSort on key";
+generate_bar_graph($data, "key");