aboutsummaryrefslogtreecommitdiff
path: root/challenge-005/paulo-custodio/python
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-11-12 18:05:36 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2021-11-12 18:05:36 +0000
commitaa9e5bb73ffd0ab973e66a00611a7f86b1a9eb4a (patch)
tree12a3f8e02467f22997b5272cd562c79522b401a2 /challenge-005/paulo-custodio/python
parente2ee87081513efe219682ada6b7fb01de87c0335 (diff)
downloadperlweeklychallenge-club-aa9e5bb73ffd0ab973e66a00611a7f86b1a9eb4a.tar.gz
perlweeklychallenge-club-aa9e5bb73ffd0ab973e66a00611a7f86b1a9eb4a.tar.bz2
perlweeklychallenge-club-aa9e5bb73ffd0ab973e66a00611a7f86b1a9eb4a.zip
Add Python solution to challenge 5
Diffstat (limited to 'challenge-005/paulo-custodio/python')
-rw-r--r--challenge-005/paulo-custodio/python/ch-1.py36
-rw-r--r--challenge-005/paulo-custodio/python/ch-2.py46
2 files changed, 82 insertions, 0 deletions
diff --git a/challenge-005/paulo-custodio/python/ch-1.py b/challenge-005/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..dfb7dac235
--- /dev/null
+++ b/challenge-005/paulo-custodio/python/ch-1.py
@@ -0,0 +1,36 @@
+#!/usr/bin/env python3
+
+# Challenge 005
+#
+# Challenge #1
+# Write a program which prints out all anagrams for a given word. For more
+# information about Anagram, please check this wikipedia page.
+# create a hash of all words in dictionary where key is sorted list of letters
+# therefore two anagrams have the same key
+
+import sys
+import re
+
+def read_file(filename):
+ with open(filename) as f:
+ return f.readlines()
+
+def read_words(lines):
+ words = []
+ for line in lines:
+ word = line.strip()
+ if not re.search(r"\W", word):
+ words.append(word)
+ return words
+
+def get_word_key(word):
+ letters = sorted([x for x in word.lower()])
+ return "".join(letters)
+
+def print_anagrams(myword):
+ myword_key = get_word_key(myword)
+ for word in read_words(read_file("words.txt")):
+ if get_word_key(word)==myword_key:
+ print(word.lower())
+
+print_anagrams(sys.argv[1])
diff --git a/challenge-005/paulo-custodio/python/ch-2.py b/challenge-005/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..366717d95f
--- /dev/null
+++ b/challenge-005/paulo-custodio/python/ch-2.py
@@ -0,0 +1,46 @@
+#!/usr/bin/env python3
+
+# Challenge 005
+#
+# Challenge #2
+# Write a program to find the sequence of characters that has the most anagrams.
+#
+# create a hash of all words in dictionary where key is sorted list of letters
+# therefore two anagrams have the same key
+
+import sys
+import re
+
+def read_file(filename):
+ with open(filename) as f:
+ return f.readlines()
+
+def read_words(lines):
+ words = []
+ for line in lines:
+ word = line.strip()
+ if not re.search(r"\W", word):
+ words.append(word)
+ return words
+
+def get_word_key(word):
+ letters = sorted([x for x in word.lower()])
+ return "".join(letters)
+
+def print_largest_anagram():
+ anagrams = {}
+ max_anagrams = 0
+ for word in read_words(read_file("words.txt")):
+ if len(word) >= 2:
+ key = get_word_key(word)
+ if key in anagrams:
+ anagrams[key] += 1
+ else:
+ anagrams[key] = 1
+ max_anagrams = max(max_anagrams, anagrams[key])
+ print(f"Maximum of {max_anagrams} anagrams")
+ for key in sorted(anagrams):
+ if anagrams[key]==max_anagrams:
+ print(key)
+
+print_largest_anagram()