-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4ff2d5d
commit 75e2cae
Showing
9 changed files
with
60,544 additions
and
0 deletions.
There are no files selected for viewing
60,388 changes: 60,388 additions & 0 deletions
60,388
impractical-projects/2. Palindromes/Solutions/2of4brif.txt
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file added
BIN
+951 Bytes
impractical-projects/2. Palindromes/Solutions/__pycache__/load_dictionary.cpython-37.pyc
Binary file not shown.
4 changes: 4 additions & 0 deletions
4
impractical-projects/2. Palindromes/Solutions/cprofile_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
import cProfile | ||
import palingrams | ||
cProfile.run('palingrams.find_palingrams()') |
16 changes: 16 additions & 0 deletions
16
impractical-projects/2. Palindromes/Solutions/dictionary_cleanup_practice.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
"""Remove single-letter words from list if not 'a' or 'i'.""" | ||
word_list = ['a', 'nurses', 'i', 'stack', 'b', 'c', 'cat'] | ||
word_list_clean = [] | ||
|
||
permissible = ('a', 'i') | ||
|
||
# remove single-letter words if not "a" or "I" | ||
for word in word_list: | ||
if len(word) > 1: | ||
word_list_clean.append(word) | ||
elif len(word) == 1 and word in permissible: | ||
word_list_clean.append(word) | ||
else: | ||
continue | ||
|
||
print("{}".format(word_list_clean)) |
27 changes: 27 additions & 0 deletions
27
impractical-projects/2. Palindromes/Solutions/load_dictionary.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
"""Load a text file as a list. | ||
Arguments: | ||
-text file name | ||
Exceptions: | ||
-IOError if filename not found. | ||
Returns: | ||
-A list of all words in text file in lower case. | ||
Requires-import sys | ||
""" | ||
import sys | ||
|
||
def load(file): | ||
"""Open a text file & turn contents into a list of lowercase strings.""" | ||
try: | ||
with open(file) as in_file: | ||
loaded_txt = in_file.read().strip().split('\n') | ||
loaded_txt = [x.lower() for x in loaded_txt] | ||
return loaded_txt | ||
except IOError as e: | ||
print("{}\nError opening {}. Terminating program.".format(e, file), | ||
file=sys.stderr) | ||
sys.exit(1) |
16 changes: 16 additions & 0 deletions
16
impractical-projects/2. Palindromes/Solutions/palindromes.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
"""Find palindromes (letter palingrams) in a dictionary file.""" | ||
|
||
import load_dictionary | ||
|
||
word_list = load_dictionary.load('D:\Git\Impractical_Python_Projects\Chapter_2\2of4brif.txt') | ||
|
||
pali_list = [] | ||
|
||
for word in word_list: | ||
if len(word) > 1 and word == word[::-1]: | ||
pali_list.append(word) | ||
|
||
print("\nNumber of palindromes found = {}\n".format(len(pali_list))) | ||
|
||
# print in list format with no quotes or commas: | ||
print(*pali_list, sep='\n') |
29 changes: 29 additions & 0 deletions
29
impractical-projects/2. Palindromes/Solutions/palingrams.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
"""Find all word-pair palingrams in a dictionary file.""" | ||
import load_dictionary | ||
|
||
word_list = load_dictionary.load('2of4brif.txt') | ||
|
||
# find word-pair palingrams | ||
def find_palingrams(): | ||
"""Find dictionary palingrams.""" | ||
pali_list = [] | ||
for word in word_list: | ||
end = len(word) | ||
rev_word = word[::-1] | ||
if end > 1: | ||
for i in range(end): | ||
if word[i:] == rev_word[:end-i]and rev_word[end-i:]in word_list: | ||
pali_list.append((word, rev_word[end-i:])) | ||
if word[:i] == rev_word[end-i:]and rev_word[:end-i]in word_list: | ||
pali_list.append((rev_word[:end-i], word)) | ||
return pali_list | ||
|
||
palingrams = find_palingrams() | ||
|
||
#sort palingrams on first word | ||
palingrams_sorted = sorted(palingrams) | ||
|
||
#display list of palingrams | ||
print("\nNumber of palingrams = {}\n".format(len(palingrams_sorted))) | ||
for first, second in palingrams_sorted: | ||
print("{} {}".format(first, second)) |
30 changes: 30 additions & 0 deletions
30
impractical-projects/2. Palindromes/Solutions/palingrams_optimized.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
"""Find all word-pair palingrams in a dictionary file.""" | ||
import load_dictionary | ||
|
||
word_list = load_dictionary.load('2of4brif.txt') | ||
|
||
# find word-pair palingrams | ||
def find_palingrams(): | ||
"""Find dictionary palingrams.""" | ||
pali_list = [] | ||
words = set(word_list) | ||
for word in words: | ||
end = len(word) | ||
rev_word = word[::-1] | ||
if end > 1: | ||
for i in range(end): | ||
if word[i:] == rev_word[:end-i]and rev_word[end-i:]in words: | ||
pali_list.append((word, rev_word[end-i:])) | ||
if word[:i] == rev_word[end-i:]and rev_word[:end-i]in words: | ||
pali_list.append((rev_word[:end-i], word)) | ||
return pali_list | ||
|
||
palingrams = find_palingrams() | ||
|
||
#sort palingrams on first word | ||
palingrams_sorted = sorted(palingrams) | ||
|
||
#display list of palingrams | ||
print("\nNumber of palingrams = {}\n".format(len(palingrams_sorted))) | ||
for first, second in palingrams_sorted: | ||
print("{} {}".format(first, second)) |
34 changes: 34 additions & 0 deletions
34
impractical-projects/2. Palindromes/Solutions/palingrams_timed.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
"""palingrams program with timer added.""" | ||
import load_dictionary | ||
import time | ||
start_time = time.time() | ||
|
||
word_list = load_dictionary.load('2of4brif.txt') | ||
|
||
# find word-pair palingrams | ||
def find_palingrams(): | ||
"""Find dictionary palingrams.""" | ||
pali_list = [] | ||
for word in word_list: | ||
end = len(word) | ||
rev_word = word[::-1] | ||
if end > 1: | ||
for i in range(end): | ||
if word[i:] == rev_word[:end-i]and rev_word[end-i:]in word_list: | ||
pali_list.append((word, rev_word[end-i:])) | ||
if word[:i] == rev_word[end-i:]and rev_word[:end-i]in word_list: | ||
pali_list.append((rev_word[:end-i], word)) | ||
return pali_list | ||
|
||
palingrams = find_palingrams() | ||
|
||
#sort palingrams on first word | ||
palingrams_sorted = sorted(palingrams) | ||
|
||
#display list of palingrams | ||
print("Number of palingrams = {}\n\n".format(len(palingrams_sorted))) | ||
for first, second in palingrams_sorted: | ||
print("{} {}".format(first, second)) | ||
|
||
end_time = time.time() | ||
print("Runtime for this program was {} seconds.".format(end_time - start_time)) |