aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-12-24 01:03:26 +0000
committerGitHub <noreply@github.com>2021-12-24 01:03:26 +0000
commitf13cc76081c7af1d173ee01166c8e725d3843f3a (patch)
treee0e79b1bdc232483dbb5f97a650f38b88700583b
parent776396430ae1e9e0aa85d2e78bb92e38fd1556f7 (diff)
parentb2bda687c42cfc1229440dfb0d392e5fbad8f1d1 (diff)
downloadperlweeklychallenge-club-f13cc76081c7af1d173ee01166c8e725d3843f3a.tar.gz
perlweeklychallenge-club-f13cc76081c7af1d173ee01166c8e725d3843f3a.tar.bz2
perlweeklychallenge-club-f13cc76081c7af1d173ee01166c8e725d3843f3a.zip
Merge pull request #5407 from pauloscustodio/devel
Add Perl and Python solutions to challenge 032
-rw-r--r--challenge-032/paulo-custodio/Makefile2
-rw-r--r--challenge-032/paulo-custodio/README1
-rw-r--r--challenge-032/paulo-custodio/example.txt6
-rw-r--r--challenge-032/paulo-custodio/perl/ch-1.pl50
-rw-r--r--challenge-032/paulo-custodio/perl/ch-2.pl42
-rw-r--r--challenge-032/paulo-custodio/python/ch-1.py51
-rw-r--r--challenge-032/paulo-custodio/python/ch-2.py35
-rw-r--r--challenge-032/paulo-custodio/t/test-1.yaml13
-rw-r--r--challenge-032/paulo-custodio/t/test-2.yaml8
-rw-r--r--challenge-033/paulo-custodio/Makefile2
-rw-r--r--challenge-033/paulo-custodio/README1
-rw-r--r--challenge-033/paulo-custodio/perl/ch-1.pl57
-rw-r--r--challenge-033/paulo-custodio/perl/ch-2.pl37
-rw-r--r--challenge-033/paulo-custodio/python/ch-1.py55
-rw-r--r--challenge-033/paulo-custodio/python/ch-2.py36
-rw-r--r--challenge-033/paulo-custodio/sample.txt1
-rw-r--r--challenge-033/paulo-custodio/t/test-1.yaml31
-rw-r--r--challenge-033/paulo-custodio/t/test-2.yaml18
-rw-r--r--challenge-034/paulo-custodio/Makefile2
-rw-r--r--challenge-034/paulo-custodio/README1
-rw-r--r--challenge-034/paulo-custodio/perl/ch-1.pl13
-rw-r--r--challenge-034/paulo-custodio/perl/ch-2.pl26
-rw-r--r--challenge-034/paulo-custodio/python/ch-1.py11
-rw-r--r--challenge-034/paulo-custodio/python/ch-2.py45
-rw-r--r--challenge-034/paulo-custodio/t/test-1.yaml5
-rw-r--r--challenge-034/paulo-custodio/t/test-2.yaml20
26 files changed, 569 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 | ####
diff --git a/challenge-033/paulo-custodio/Makefile b/challenge-033/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-033/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-033/paulo-custodio/README b/challenge-033/paulo-custodio/README
new file mode 100644
index 0000000000..87dc0b2fbd
--- /dev/null
+++ b/challenge-033/paulo-custodio/README
@@ -0,0 +1 @@
+Solution by Paulo Custodio
diff --git a/challenge-033/paulo-custodio/perl/ch-1.pl b/challenge-033/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..8346b97cbf
--- /dev/null
+++ b/challenge-033/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,57 @@
+#!/usr/bin/perl
+
+# Challenge 033
+#
+# Task #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
+
+use Modern::Perl;
+
+# collect
+my %count;
+while (<>) {
+ for (split //, lc($_)) {
+ if (/[a-z]/) {
+ $count{$_}++;
+ }
+ }
+}
+
+# output
+for (sort keys %count) {
+ say "$_: $count{$_}";
+}
diff --git a/challenge-033/paulo-custodio/perl/ch-2.pl b/challenge-033/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..761f07455c
--- /dev/null
+++ b/challenge-033/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,37 @@
+#!/usr/bin/perl
+
+# Challenge 033
+#
+# Task #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
+
+use Modern::Perl;
+
+# header
+print " x|";
+printf "%4d", $_ for 1..11;
+print "\n";
+print "---+", "-"x(11*4), "\n";
+
+# print table
+for my $row (1..11) {
+ printf "%3d|", $row;
+ print " "x(4*($row-1));
+ printf "%4d", $row*$_ for $row..11;
+ print "\n";
+}
diff --git a/challenge-033/paulo-custodio/python/ch-1.py b/challenge-033/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..bee81c7d25
--- /dev/null
+++ b/challenge-033/paulo-custodio/python/ch-1.py
@@ -0,0 +1,55 @@
+#!/usr/bin/python3
+
+# Challenge 033
+#
+# Task #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
+
+import fileinput
+import sys
+from collections import defaultdict
+
+# collect
+count = defaultdict(lambda: 0)
+for line in fileinput.input():
+ for c in line:
+ if c.isalpha():
+ count[c.lower()] += 1
+
+# output
+for key in sorted(count):
+ print(f"{key}: {count[key]}")
diff --git a/challenge-033/paulo-custodio/python/ch-2.py b/challenge-033/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..c7ebd913b5
--- /dev/null
+++ b/challenge-033/paulo-custodio/python/ch-2.py
@@ -0,0 +1,36 @@
+#!/usr/bin/python3
+
+# Challenge 033
+#
+# Task #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
+
+# print header
+print(" x|", end="")
+for col in range(1, 12):
+ print(f"{col:4d}", end="")
+print("")
+print("---+", "-"*(11*4), sep="")
+
+# print table
+for row in range(1, 12):
+ print(f"{row:3d}|", end="")
+ print(" "*(4*(row-1)), end="")
+ for col in range(row, 12):
+ print(f"{row*col:4d}", end="")
+ print("")
diff --git a/challenge-033/paulo-custodio/sample.txt b/challenge-033/paulo-custodio/sample.txt
new file mode 100644
index 0000000000..2fe6575e76
--- /dev/null
+++ b/challenge-033/paulo-custodio/sample.txt
@@ -0,0 +1 @@
+The quick brown fox jumps over the lazy dog.
diff --git a/challenge-033/paulo-custodio/t/test-1.yaml b/challenge-033/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..894d074083
--- /dev/null
+++ b/challenge-033/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,31 @@
+- setup:
+ cleanup:
+ args: sample.txt
+ input:
+ output: |
+ |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
diff --git a/challenge-033/paulo-custodio/t/test-2.yaml b/challenge-033/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..6188568fb4
--- /dev/null
+++ b/challenge-033/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,18 @@
+- setup:
+ cleanup:
+ args:
+ input:
+ output: |
+ | 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
diff --git a/challenge-034/paulo-custodio/Makefile b/challenge-034/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-034/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-034/paulo-custodio/README b/challenge-034/paulo-custodio/README
new file mode 100644
index 0000000000..87dc0b2fbd
--- /dev/null
+++ b/challenge-034/paulo-custodio/README
@@ -0,0 +1 @@
+Solution by Paulo Custodio
diff --git a/challenge-034/paulo-custodio/perl/ch-1.pl b/challenge-034/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..dce4f757f9
--- /dev/null
+++ b/challenge-034/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,13 @@
+#!/usr/bin/perl
+
+# Challenge 034
+#
+# Task #1
+# Contributed by Dave Cross
+# Write a program that demonstrates using hash slices and/or array slices.
+
+use Modern::Perl;
+
+my %data = (oranges => 3, pears => 2, apples => 4);
+my @values = @data{qw(oranges apples)};
+say join(", ", @values);
diff --git a/challenge-034/paulo-custodio/perl/ch-2.pl b/challenge-034/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..7ce4f31fba
--- /dev/null
+++ b/challenge-034/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,26 @@
+#!/usr/bin/perl
+
+# Challenge 034
+#
+# Task #2
+# Contributed by Dave Cross
+# Write a program that demonstrates a dispatch table.
+
+use Modern::Perl;
+
+# simple rpn calculator
+my @stack;
+my %dispatch = (
+ '+' => sub { my $b = pop @stack; my $a = pop @stack; push @stack, $a+$b; },
+ '-' => sub { my $b = pop @stack; my $a = pop @stack; push @stack, $a-$b; },
+ '*' => sub { my $b = pop @stack; my $a = pop @stack; push @stack, $a*$b; },
+ '/' => sub { my $b = pop @stack; my $a = pop @stack; push @stack, $a/$b; },
+ '.' => sub { say pop @stack; },
+);
+
+for (split //, "@ARGV") {
+ if (/\s/) {}
+ elsif (/\d/) { push @stack, $_; }
+ elsif (exists $dispatch{$_}) { $dispatch{$_}->(); }
+ else { die "invalid operation: $_"; }
+}
diff --git a/challenge-034/paulo-custodio/python/ch-1.py b/challenge-034/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..d7855da4e7
--- /dev/null
+++ b/challenge-034/paulo-custodio/python/ch-1.py
@@ -0,0 +1,11 @@
+#!/usr/bin/python3
+
+# Challenge 034
+#
+# Task #1
+# Contributed by Dave Cross
+# Write a program that demonstrates using hash slices and/or array slices.
+
+data = {'oranges':3, 'pears':2, 'apples':4}
+values = [data[x] for x in 'oranges apples'.split()]
+print(*values, sep=", ")
diff --git a/challenge-034/paulo-custodio/python/ch-2.py b/challenge-034/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..87134c16dc
--- /dev/null
+++ b/challenge-034/paulo-custodio/python/ch-2.py
@@ -0,0 +1,45 @@
+#!/usr/bin/python3
+
+# Challenge 034
+#
+# Task #2
+# Contributed by Dave Cross
+# Write a program that demonstrates a dispatch table.
+
+import sys
+
+# simple rpn calculator
+stack = []
+def add():
+ b = stack.pop()
+ a = stack.pop()
+ stack.append(a+b)
+
+def sub():
+ b = stack.pop()
+ a = stack.pop()
+ stack.append(a-b)
+
+def mul():
+ b = stack.pop()
+ a = stack.pop()
+ stack.append(a*b)
+
+def div():
+ b = stack.pop()
+ a = stack.pop()
+ stack.append(a/b)
+
+def prt():
+ print(stack.pop())
+
+dispatch = {'+': add, '-':sub, '*':mul, '/':div, '.':prt}
+
+prog = "".join(sys.argv[1:])
+for c in prog:
+ if c.isspace():
+ pass
+ elif c.isdigit():
+ stack.append(int(c))
+ else:
+ dispatch[c]()
diff --git a/challenge-034/paulo-custodio/t/test-1.yaml b/challenge-034/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..764a7a9d39
--- /dev/null
+++ b/challenge-034/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,5 @@
+- setup:
+ cleanup:
+ args:
+ input:
+ output: 3, 4
diff --git a/challenge-034/paulo-custodio/t/test-2.yaml b/challenge-034/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..746bd8389b
--- /dev/null
+++ b/challenge-034/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,20 @@
+- setup:
+ cleanup:
+ args: 1 3 + .
+ input:
+ output: 4
+- setup:
+ cleanup:
+ args: 1 3 - .
+ input:
+ output: -2
+- setup:
+ cleanup:
+ args: '"3 2 * ."'
+ input:
+ output: 6
+- setup:
+ cleanup:
+ args: 3 2 / .
+ input:
+ output: 1.5