diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-11-10 21:56:33 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-11-10 21:56:33 +0000 |
| commit | 817f2b4f4623d09ccfa390873c06ee32596a29ab (patch) | |
| tree | 744e2fa86473353d50c52d1749531b82a0995cd2 /challenge-033 | |
| parent | dfa55de90683c75623e8d09407404d1b3a68bd6e (diff) | |
| parent | 65825f943b1eb406fcc4bff1dc9762e1a88c6366 (diff) | |
| download | perlweeklychallenge-club-817f2b4f4623d09ccfa390873c06ee32596a29ab.tar.gz perlweeklychallenge-club-817f2b4f4623d09ccfa390873c06ee32596a29ab.tar.bz2 perlweeklychallenge-club-817f2b4f4623d09ccfa390873c06ee32596a29ab.zip | |
Merge pull request #923 from dcw803/master
imported my (dcw803) solutions to challenge 33, pretty easy this week
Diffstat (limited to 'challenge-033')
| -rw-r--r-- | challenge-033/duncan-c-white/README | 98 | ||||
| -rwxr-xr-x | challenge-033/duncan-c-white/perl5/ch-1.pl | 62 | ||||
| -rwxr-xr-x | challenge-033/duncan-c-white/perl5/ch-2.pl | 43 |
3 files changed, 162 insertions, 41 deletions
diff --git a/challenge-033/duncan-c-white/README b/challenge-033/duncan-c-white/README index a1e1d686c5..382b358255 100644 --- a/challenge-033/duncan-c-white/README +++ b/challenge-033/duncan-c-white/README @@ -1,46 +1,62 @@ -Challenge 1: "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. +Challenge 1: "Count Letters (A..Z) -So with the following input in file example.txt +Create a script that accepts one or more files specified on the +command-line and count the number of times letters appeared in the files. -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 - -My notes: Straightforward use of a hash. +So with the following input file sample.txt: +The quick brown fox jumps over the lazy dog. -Challenge 2: "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. +the script would display something like: -My notes: easy, especially using a histogram module I wrote recently. +a: 1 +b: 1 +c: 1 +d: 1 +e: 3 +f: 1 +g: 1 +h: 2 +i: 1 +j: 1 +k: 1 +l: 1 +m: 1 +n: 1 +o: 4 +p: 1 +q: 1 +r: 2 +s: 1 +t: 2 +u: 2 +v: 1 +w: 1 +x: 1 +y: 1 +z: 1 +" + +My notes: trivial. + + +Challenge 2: "Formatted Multiplication Table + +Write a script to print 11x11 multiplication table, only the top half triangle. + + x| 1 2 3 4 5 6 7 8 9 10 11 +---+-------------------------------------------- + 1| 1 2 3 4 5 6 7 8 9 10 11 + 2| 4 6 8 10 12 14 16 18 20 22 + 3| 9 12 15 18 21 24 27 30 33 + 4| 16 20 24 28 32 36 40 44 + 5| 25 30 35 40 45 50 55 + 6| 36 42 48 54 60 66 + 7| 49 56 63 70 77 + 8| 64 72 80 88 + 9| 81 90 99 + 10| 100 110 + 11| 121 +" + +My notes: trivial. diff --git a/challenge-033/duncan-c-white/perl5/ch-1.pl b/challenge-033/duncan-c-white/perl5/ch-1.pl new file mode 100755 index 0000000000..3d9438649d --- /dev/null +++ b/challenge-033/duncan-c-white/perl5/ch-1.pl @@ -0,0 +1,62 @@ +#!/usr/bin/perl +# +# Challenge 1: "Count Letters (A..Z) +# +# Create a script that accepts one or more files specified on the +# command-line and count the number of times letters appeared in the files. +# +# So with the following input file sample.txt: +# +# The quick brown fox jumps over the lazy dog. +# +# the script would display something like: +# +# a: 1 +# b: 1 +# c: 1 +# d: 1 +# e: 3 +# f: 1 +# g: 1 +# h: 2 +# i: 1 +# j: 1 +# k: 1 +# l: 1 +# m: 1 +# n: 1 +# o: 4 +# p: 1 +# q: 1 +# r: 2 +# s: 1 +# t: 2 +# u: 2 +# v: 1 +# w: 1 +# x: 1 +# y: 1 +# z: 1 +# " +# +# My notes: trivial. +# + +use v5.10; # to get "say" +use strict; +use warnings; + +#die "Usage: ch-1.pl < data | ch-1.pl filename+\n"; + +my %freq; +while( <> ) +{ + chomp; + $freq{$1}++ while s/([a-z])//; +} + +foreach my $str (sort { $a cmp $b } keys %freq) +{ + my $v = $freq{$str}; + say "$str $v"; +} diff --git a/challenge-033/duncan-c-white/perl5/ch-2.pl b/challenge-033/duncan-c-white/perl5/ch-2.pl new file mode 100755 index 0000000000..17ce5d2d7b --- /dev/null +++ b/challenge-033/duncan-c-white/perl5/ch-2.pl @@ -0,0 +1,43 @@ +#!/usr/bin/perl +# +# Challenge 2: "Formatted Multiplication Table +# +# Write a script to print 11x11 multiplication table, the top half triangle. +# +# x| 1 2 3 4 5 6 7 8 9 10 11 +# ---+-------------------------------------------- +# 1| 1 2 3 4 5 6 7 8 9 10 11 +# 2| 4 6 8 10 12 14 16 18 20 22 +# 3| 9 12 15 18 21 24 27 30 33 +# 4| 16 20 24 28 32 36 40 44 +# 5| 25 30 35 40 45 50 55 +# 6| 36 42 48 54 60 66 +# 7| 49 56 63 70 77 +# 8| 64 72 80 88 +# 9| 81 90 99 +# 10| 100 110 +# 11| 121 +# " +# +# My notes: trivial. +# + +use v5.10; # for "say" +use strict; +use warnings; + +sub line +{ + my( $label, $gap, $scale ) = @_; + printf "%4s|", $label; + printf "%4s", '' for 1..$gap; + printf "%4d", $_*$scale for $gap+1..11; + print "\n"; +} + +line( "x", 0, 1 ); +say '-' x 49; +for( 1..11 ) +{ + line( $_, $_-1, $_ ); +} |
