diff options
Diffstat (limited to 'challenge-032')
| -rw-r--r-- | challenge-032/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-032/paulo-custodio/README | 1 | ||||
| -rw-r--r-- | challenge-032/paulo-custodio/example.txt | 6 | ||||
| -rw-r--r-- | challenge-032/paulo-custodio/perl/ch-1.pl | 50 | ||||
| -rw-r--r-- | challenge-032/paulo-custodio/perl/ch-2.pl | 42 | ||||
| -rw-r--r-- | challenge-032/paulo-custodio/python/ch-1.py | 51 | ||||
| -rw-r--r-- | challenge-032/paulo-custodio/python/ch-2.py | 35 | ||||
| -rw-r--r-- | challenge-032/paulo-custodio/t/test-1.yaml | 13 | ||||
| -rw-r--r-- | challenge-032/paulo-custodio/t/test-2.yaml | 8 |
9 files changed, 208 insertions, 0 deletions
diff --git a/challenge-032/paulo-custodio/Makefile b/challenge-032/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-032/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-032/paulo-custodio/README b/challenge-032/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-032/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-032/paulo-custodio/example.txt b/challenge-032/paulo-custodio/example.txt new file mode 100644 index 0000000000..00fe021a08 --- /dev/null +++ b/challenge-032/paulo-custodio/example.txt @@ -0,0 +1,6 @@ +apple +banana +apple +cherry +cherry +apple diff --git a/challenge-032/paulo-custodio/perl/ch-1.pl b/challenge-032/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..5770faa607 --- /dev/null +++ b/challenge-032/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,50 @@ +#!/usr/bin/perl + +# Challenge 032 +# +# Task #1 +# Contributed by Neil Bowers +# 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 +# banana,1 +# cherry,2 + +use Modern::Perl; + +# command line options +my $sep = "\t"; +if (@ARGV && $ARGV[0] eq '-csv') { + $sep = ","; + shift; +} + +# count instances +my %count; +while (<>) { + chomp; + $count{$_}++; +} + +# output +for my $key (sort keys %count) { + say $key, $sep, $count{$key}; +} diff --git a/challenge-032/paulo-custodio/perl/ch-2.pl b/challenge-032/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..883a9ae6c8 --- /dev/null +++ b/challenge-032/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,42 @@ +#!/usr/bin/perl + +# Challenge 032 +# +# Task #2 +# Contributed by Neil Bowers +# 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. + +use Modern::Perl; +use List::Util 'max'; + +my $data = { apple => 3, cherry => 2, banana => 1 }; + +sub chart { + my($data) = @_; + + # get size of keys + my $width = max(map {length($_)} keys %$data); + + # output data + for my $key (sort keys %$data) { + say sprintf("%*s | %s", $width, $key, "##" x $data->{$key}); + } +} + +chart($data); diff --git a/challenge-032/paulo-custodio/python/ch-1.py b/challenge-032/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..717d0f8ded --- /dev/null +++ b/challenge-032/paulo-custodio/python/ch-1.py @@ -0,0 +1,51 @@ +#!/usr/bin/python3 + +# Challenge 032 +# +# Task #1 +# Contributed by Neil Bowers +# 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 +# banana,1 +# cherry,2 + +import fileinput +import sys + +# command line options +sep = "\t" +if len(sys.argv)>1 and sys.argv[1]=="-csv": + sys.argv.pop(1) + sep = "," + +# count instances +count = {} +for line in fileinput.input(): + word = line.strip() + if word in count: + count[word] += 1 + else: + count[word] = 1 + +# output +for key in sorted(count): + print(f"{key}{sep}{count[key]}") diff --git a/challenge-032/paulo-custodio/python/ch-2.py b/challenge-032/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..6a0b7385c7 --- /dev/null +++ b/challenge-032/paulo-custodio/python/ch-2.py @@ -0,0 +1,35 @@ +#!/usr/bin/python3 + +# Challenge 032 +# +# Task #2 +# Contributed by Neil Bowers +# 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. + +data = { 'apple':3, 'cherry':2, 'banana':1 } + +def chart(data): + # get size of keys + width = max([len(x) for x in data]) + + # output data + for key in sorted(data): + print(("{:"+str(width)+"s} | {}").format(key, "##"*data[key])) + +chart(data) diff --git a/challenge-032/paulo-custodio/t/test-1.yaml b/challenge-032/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..1fc4015318 --- /dev/null +++ b/challenge-032/paulo-custodio/t/test-1.yaml @@ -0,0 +1,13 @@ +- setup: + cleanup: + args: example.txt + input: + output: "apple\t3\nbanana\t1\ncherry\t2\n" +- setup: + cleanup: + args: -csv example.txt + input: + output: | + apple,3 + banana,1 + cherry,2 diff --git a/challenge-032/paulo-custodio/t/test-2.yaml b/challenge-032/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..a52de03332 --- /dev/null +++ b/challenge-032/paulo-custodio/t/test-2.yaml @@ -0,0 +1,8 @@ +- setup: + cleanup: + args: + input: + output: | + | apple | ###### + |banana | ## + |cherry | #### |
