aboutsummaryrefslogtreecommitdiff
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
parente2ee87081513efe219682ada6b7fb01de87c0335 (diff)
downloadperlweeklychallenge-club-aa9e5bb73ffd0ab973e66a00611a7f86b1a9eb4a.tar.gz
perlweeklychallenge-club-aa9e5bb73ffd0ab973e66a00611a7f86b1a9eb4a.tar.bz2
perlweeklychallenge-club-aa9e5bb73ffd0ab973e66a00611a7f86b1a9eb4a.zip
Add Python solution to challenge 5
-rw-r--r--challenge-005/paulo-custodio/Makefile2
-rw-r--r--challenge-005/paulo-custodio/perl/ch-1.pl2
-rw-r--r--challenge-005/paulo-custodio/perl/ch-2.pl2
-rw-r--r--challenge-005/paulo-custodio/python/ch-1.py36
-rw-r--r--challenge-005/paulo-custodio/python/ch-2.py46
-rw-r--r--challenge-005/paulo-custodio/t/test-1.yaml1
-rw-r--r--challenge-005/paulo-custodio/t/test-2.yaml2
-rw-r--r--challenge-005/paulo-custodio/test.pl4
8 files changed, 86 insertions, 9 deletions
diff --git a/challenge-005/paulo-custodio/Makefile b/challenge-005/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-005/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-005/paulo-custodio/perl/ch-1.pl b/challenge-005/paulo-custodio/perl/ch-1.pl
index cd33db9b46..6db4ce2911 100644
--- a/challenge-005/paulo-custodio/perl/ch-1.pl
+++ b/challenge-005/paulo-custodio/perl/ch-1.pl
@@ -1,4 +1,4 @@
-#!/usr/bin/perl
+#!/usr/bin/env perl
# Challenge 005
#
diff --git a/challenge-005/paulo-custodio/perl/ch-2.pl b/challenge-005/paulo-custodio/perl/ch-2.pl
index 59ab0fb365..69dd53f3dd 100644
--- a/challenge-005/paulo-custodio/perl/ch-2.pl
+++ b/challenge-005/paulo-custodio/perl/ch-2.pl
@@ -1,4 +1,4 @@
-#!/usr/bin/perl
+#!/usr/bin/env perl
# Challenge 005
#
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()
diff --git a/challenge-005/paulo-custodio/t/test-1.yaml b/challenge-005/paulo-custodio/t/test-1.yaml
index 63708186b7..920baa4d67 100644
--- a/challenge-005/paulo-custodio/t/test-1.yaml
+++ b/challenge-005/paulo-custodio/t/test-1.yaml
@@ -27,4 +27,3 @@
|recaps
|scrape
|spacer
- |casper
diff --git a/challenge-005/paulo-custodio/t/test-2.yaml b/challenge-005/paulo-custodio/t/test-2.yaml
index f63585ea79..0fc57c3cc5 100644
--- a/challenge-005/paulo-custodio/t/test-2.yaml
+++ b/challenge-005/paulo-custodio/t/test-2.yaml
@@ -4,8 +4,6 @@
input:
output: |
|Maximum of 8 anagrams
- |aceprs
- |aels
|aelst
|aerst
|egor
diff --git a/challenge-005/paulo-custodio/test.pl b/challenge-005/paulo-custodio/test.pl
deleted file mode 100644
index ba6c37260b..0000000000
--- a/challenge-005/paulo-custodio/test.pl
+++ /dev/null
@@ -1,4 +0,0 @@
-#!/usr/bin/env perl
-use Modern::Perl;
-use Test::More;
-require '../../challenge-001/paulo-custodio/test.pl';