aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2022-04-20 17:08:45 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2022-04-20 17:08:45 +0100
commit7a60e87772f998c800ab2a22ef74fe088efb2c8a (patch)
treeb20ae30719d912b56f621b0aa0d903262c7eabef
parent190862678a9c8f26d213d656c1eb50b8f372653d (diff)
downloadperlweeklychallenge-club-7a60e87772f998c800ab2a22ef74fe088efb2c8a.tar.gz
perlweeklychallenge-club-7a60e87772f998c800ab2a22ef74fe088efb2c8a.tar.bz2
perlweeklychallenge-club-7a60e87772f998c800ab2a22ef74fe088efb2c8a.zip
Add Python solution to challenge 161
-rw-r--r--challenge-161/paulo-custodio/python/ch-1.py26
-rw-r--r--challenge-161/paulo-custodio/python/ch-2.py69
2 files changed, 95 insertions, 0 deletions
diff --git a/challenge-161/paulo-custodio/python/ch-1.py b/challenge-161/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..d2e0a178aa
--- /dev/null
+++ b/challenge-161/paulo-custodio/python/ch-1.py
@@ -0,0 +1,26 @@
+#!/usr/bin/env python3
+
+# Challenge 161
+#
+# Task 1: Abecedarian Words
+# Submitted by: Ryan J Thompson
+# An abecedarian word is a word whose letters are arranged in alphabetical
+# order. For example, "knotty" is an abecedarian word, but "knots" is not.
+# Output or return a list of all abecedarian words in the dictionary, sorted
+# in decreasing order of length.
+#
+# Optionally, using only abecedarian words, leave a short comment in your code
+# to make your reviewer smile.
+
+import sys
+
+out = set()
+with open(sys.argv[1]) as f:
+ for line in f.readlines():
+ word = line.rstrip()
+ if len(word)>=3:
+ abcd_word = "".join(sorted(word)).lower()
+ if word==abcd_word:
+ out.add(word)
+for word in sorted(sorted(out), key=len, reverse=True):
+ print(word)
diff --git a/challenge-161/paulo-custodio/python/ch-2.py b/challenge-161/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..095946e9e0
--- /dev/null
+++ b/challenge-161/paulo-custodio/python/ch-2.py
@@ -0,0 +1,69 @@
+#!/usr/bin/env python3
+
+# Challenge 161
+#
+# Task 2: Pangrams
+# Submitted by: Ryan J Thompson
+# A pangram is a sentence or phrase that uses every letter in the English
+# alphabet at least once. For example, perhaps the most well known pangram is:
+#
+# the quick brown fox jumps over the lazy dog
+# Using the provided dictionary, so that you don't need to include individual
+# copy, generate at least one pangram.
+#
+# Your pangram does not have to be a syntactically valid English sentence (doing
+# so would require far more work, and a dictionary of nouns, verbs, adjectives,
+# adverbs, and conjunctions). Also note that repeated letters, and even repeated
+# words, are permitted.
+#
+# BONUS: Constrain or optimize for something interesting (completely up to you),
+# such as:
+# Shortest possible pangram (difficult)
+# Pangram which contains only abecedarian words (see challenge 1)
+# Pangram such that each word "solves" exactly one new letter. For example, such
+# a pangram might begin with (newly solved letters in bold):
+# a ah hi hid die ice tea ...
+# What is the longest possible anagram generated with this method? (All
+# solutions will contain 26 words, so focus on the letter count.)
+# Pangrams that have the weirdest (PG-13) Google image search results
+# Anything interesting goes!
+
+import sys
+import re
+
+# read dictionary
+dict = []
+with open(sys.argv[1]) as f:
+ for line in f.readlines():
+ word = line.rstrip()
+ if re.match(r"^[a-z]+$", word):
+ dict.append(word)
+
+# define set of not yet seen letters
+pending = set()
+for c in range(ord('a'), ord('z')+1):
+ pending.add(chr(c))
+
+def next_word():
+ max_word = ""
+ max_letters = 0
+ for word in dict:
+ # get new letters
+ letters = set()
+ for c in word:
+ if c in pending:
+ letters.add(c)
+ # save max letters
+ num_letters = len(letters)
+ if num_letters>max_letters:
+ max_word, max_letters = word, num_letters
+ return max_word
+
+# compose words to match most of pending letters
+words = []
+while pending:
+ word = next_word()
+ words.append(word)
+ for c in word:
+ pending.discard(c)
+print(" ".join(words))