aboutsummaryrefslogtreecommitdiff
path: root/challenge-033/paulo-custodio/python
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-12-23 16:48:33 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2021-12-23 16:48:33 +0000
commit5ec1140a5d61db370bed897824b11b0534533ab3 (patch)
tree5215a7946fcb73651806e65f9a4d3684841b14be /challenge-033/paulo-custodio/python
parentce5bd2dd1a3159107febe4b9eb488a071bc601f5 (diff)
downloadperlweeklychallenge-club-5ec1140a5d61db370bed897824b11b0534533ab3.tar.gz
perlweeklychallenge-club-5ec1140a5d61db370bed897824b11b0534533ab3.tar.bz2
perlweeklychallenge-club-5ec1140a5d61db370bed897824b11b0534533ab3.zip
Add Perl and Python solutions to challenge 033
Diffstat (limited to 'challenge-033/paulo-custodio/python')
-rw-r--r--challenge-033/paulo-custodio/python/ch-1.py55
-rw-r--r--challenge-033/paulo-custodio/python/ch-2.py36
2 files changed, 91 insertions, 0 deletions
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("")