aboutsummaryrefslogtreecommitdiff
path: root/challenge-166
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2024-09-30 16:51:52 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2024-09-30 16:51:52 +0100
commit13ac4b279c2937fc35bb2094095fa9203a7f5e30 (patch)
tree2e2d6e8a876842678c1263db34b13eded7aee14c /challenge-166
parentb0d5d893606d0309a92f521b103eca08c3eb6386 (diff)
downloadperlweeklychallenge-club-13ac4b279c2937fc35bb2094095fa9203a7f5e30.tar.gz
perlweeklychallenge-club-13ac4b279c2937fc35bb2094095fa9203a7f5e30.tar.bz2
perlweeklychallenge-club-13ac4b279c2937fc35bb2094095fa9203a7f5e30.zip
Add Python solution to challenge 166
Diffstat (limited to 'challenge-166')
-rw-r--r--challenge-166/paulo-custodio/perl/ch-1.pl10
-rw-r--r--challenge-166/paulo-custodio/perl/ch-2.pl2
-rw-r--r--challenge-166/paulo-custodio/python/ch-1.py45
-rw-r--r--challenge-166/paulo-custodio/python/ch-2.py83
-rw-r--r--challenge-166/paulo-custodio/t/dir_a/Old_Fonts/.keep0
5 files changed, 134 insertions, 6 deletions
diff --git a/challenge-166/paulo-custodio/perl/ch-1.pl b/challenge-166/paulo-custodio/perl/ch-1.pl
index 8e6f78001a..0764002300 100644
--- a/challenge-166/paulo-custodio/perl/ch-1.pl
+++ b/challenge-166/paulo-custodio/perl/ch-1.pl
@@ -1,4 +1,4 @@
-#!/usr/bin/perl
+#!/usr/bin/env perl
# Challenge 166
#
@@ -10,22 +10,22 @@
# 0xC0dedBad. I want more!
#
# Write a program that will read from a dictionary and find 2- to 8-letter
-# words that can be “spelled” in hexadecimal, with the addition of the following
+# words that can be "spelled" in hexadecimal, with the addition of the following
# letter substitutions:
#
-# o -> 0 (e.g., 0xf00d = “food”)
+# o -> 0 (e.g., 0xf00d = "food")
# l -> 1
# i -> 1
# s -> 5
# t -> 7
#
# You can use your own dictionary or you can simply open
-# ../../../data/dictionary.txt (relative to your script’s location in our GitHub
+# ../../../data/dictionary.txt (relative to your script's location in our GitHub
# repository) to access the dictionary of common words from Week #161.
#
# Optional Extras (for an 0xAddedFee, of course!)
#
-# Limit the number of “special” letter substitutions in any one result to
+# Limit the number of "special" letter substitutions in any one result to
# keep that result at least somewhat comprehensible. (0x51105010 is an
# actual example from my sample solution you may wish to avoid!)
#
diff --git a/challenge-166/paulo-custodio/perl/ch-2.pl b/challenge-166/paulo-custodio/perl/ch-2.pl
index f65656bcab..cb92177a74 100644
--- a/challenge-166/paulo-custodio/perl/ch-2.pl
+++ b/challenge-166/paulo-custodio/perl/ch-2.pl
@@ -1,4 +1,4 @@
-#!/usr/bin/perl
+#!/usr/bin/env perl
# Challenge 166
#
diff --git a/challenge-166/paulo-custodio/python/ch-1.py b/challenge-166/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..b64b0f1337
--- /dev/null
+++ b/challenge-166/paulo-custodio/python/ch-1.py
@@ -0,0 +1,45 @@
+#!/usr/bin/env python3
+
+# Challenge 166
+#
+# Task 1: Hexadecimal Words
+# Submitted by: Ryan J Thompson
+#
+# As an old systems programmer, whenever I needed to come up with a 32-bit
+# number, I would reach for the tired old examples like 0xDeadBeef and
+# 0xC0dedBad. I want more!
+#
+# Write a program that will read from a dictionary and find 2- to 8-letter
+# words that can be "spelled" in hexadecimal, with the addition of the following
+# letter substitutions:
+#
+# o -> 0 (e.g., 0xf00d = "food")
+# l -> 1
+# i -> 1
+# s -> 5
+# t -> 7
+#
+# You can use your own dictionary or you can simply open
+# ../../../data/dictionary.txt (relative to your script's location in our GitHub
+# repository) to access the dictionary of common words from Week #161.
+#
+# Optional Extras (for an 0xAddedFee, of course!)
+#
+# Limit the number of "special" letter substitutions in any one result to
+# keep that result at least somewhat comprehensible. (0x51105010 is an
+# actual example from my sample solution you may wish to avoid!)
+#
+# Find phrases of words that total 8 characters in length
+# (e.g., 0xFee1Face), rather than just individual words.
+
+import sys
+
+if len(sys.argv) != 2:
+ sys.exit("usage: ch-1.py words.txt")
+
+f = open(sys.argv[1], "r")
+for line in f.readlines():
+ word = line.strip()
+ if len(word) >= 2 and len(word) <= 8 and all(c in 'abcdef0list' for c in word.lower()):
+ word = word.translate(str.maketrans('olist', '01157'))
+ print("0x" + word.upper())
diff --git a/challenge-166/paulo-custodio/python/ch-2.py b/challenge-166/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..a47b99586a
--- /dev/null
+++ b/challenge-166/paulo-custodio/python/ch-2.py
@@ -0,0 +1,83 @@
+#!/usr/bin/env python3
+
+# Challenge 166
+#
+# Task 2: K-Directory Diff
+# Submitted by: Ryan J Thompson
+#
+# Given a few (three or more) directories (non-recursively), display a
+# side-by-side difference of files that are missing from at least one of the
+# directories. Do not display files that exist in every directory.
+#
+# Since the task is non-recursive, if you encounter a subdirectory, append a /,
+# but otherwise treat it the same as a regular file.
+# Example
+#
+# Given the following directory structure:
+#
+# dir_a:
+# Arial.ttf Comic_Sans.ttf Georgia.ttf Helvetica.ttf Impact.otf Verdana.ttf
+# Old_Fonts/
+#
+# dir_b:
+# Arial.ttf Comic_Sans.ttf Courier_New.ttf Helvetica.ttf Impact.otf
+# Tahoma.ttf Verdana.ttf
+#
+# dir_c:
+# Arial.ttf Courier_New.ttf Helvetica.ttf Impact.otf Monaco.ttf Verdana.ttf
+#
+# The output should look similar to the following:
+#
+# dir_a | dir_b | dir_c
+# -------------- | --------------- | ---------------
+# Comic_Sans.ttf | Comic_Sans.ttf |
+# | Courier_New.ttf | Courier_New.ttf
+# Georgia.ttf | |
+# | | Monaco.ttf
+# Old_Fonts/ | |
+# | Tahoma.ttf |
+
+import os
+import sys
+
+WIDTH = 16
+
+def read_dir(dir):
+ try:
+ with os.scandir(dir) as entries:
+ return sorted((entry.name + '/' if entry.is_dir() else entry.name) for entry in entries if entry.name not in {'.', '..'})
+ except OSError as e:
+ print(f"opendir {dir}: {e}", file=sys.stderr)
+ sys.exit(1)
+
+def read_dirs(*dirs):
+ return [read_dir(dir) for dir in dirs]
+
+def print_line(*cells):
+ for i in range(len(cells)):
+ print(f"{cells[i]:<{WIDTH}}", end=" | " if i != len(cells) - 1 else "")
+ print()
+
+def print_diff(dirs, contents):
+ # print header
+ print_line(*dirs)
+ print_line(*(["-" * WIDTH] * len(dirs)))
+
+ # collect files
+ files = {}
+ files_dir = {}
+ for i, dir in enumerate(dirs):
+ for file in contents[i]:
+ files[file] = True
+ if file not in files_dir:
+ files_dir[file] = {}
+ files_dir[file][dir] = True
+
+ # print rows
+ for file in sorted(files.keys()):
+ row = [file if dir in files_dir.get(file, {}) else "" for dir in dirs]
+ if any(cell == "" for cell in row):
+ print_line(*row)
+
+if len(sys.argv) > 2:
+ print_diff(sys.argv[1:], read_dirs(*sys.argv[1:]))
diff --git a/challenge-166/paulo-custodio/t/dir_a/Old_Fonts/.keep b/challenge-166/paulo-custodio/t/dir_a/Old_Fonts/.keep
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/challenge-166/paulo-custodio/t/dir_a/Old_Fonts/.keep