diff options
| author | Noud Aldenhoven <noud.aldenhoven@gmail.com> | 2019-11-03 13:28:30 +0100 |
|---|---|---|
| committer | Noud Aldenhoven <noud.aldenhoven@gmail.com> | 2019-11-03 13:28:30 +0100 |
| commit | 900b2ac1c872f49de2a61c96053b262e6e33cba8 (patch) | |
| tree | 7f4b7c9add7c2fa9480ec8a8402ef11b2c10ae93 | |
| parent | fcf19d481aab81c548656d73f0e4c704535f7fd3 (diff) | |
| download | perlweeklychallenge-club-900b2ac1c872f49de2a61c96053b262e6e33cba8.tar.gz perlweeklychallenge-club-900b2ac1c872f49de2a61c96053b262e6e33cba8.tar.bz2 perlweeklychallenge-club-900b2ac1c872f49de2a61c96053b262e6e33cba8.zip | |
Solutions to challenge 32 problem 1 and 2 in Perl 6 by Noud
| -rw-r--r-- | challenge-032/noud/perl6/ch1.p6 | 40 | ||||
| -rw-r--r-- | challenge-032/noud/perl6/ch2.p6 | 48 |
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"); |
