Skip to content

Commit

Permalink
A: Python - IPP #2 Solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
Gizmotronn committed Dec 13, 2019
1 parent 4ff2d5d commit 75e2cae
Show file tree
Hide file tree
Showing 9 changed files with 60,544 additions and 0 deletions.
60,388 changes: 60,388 additions & 0 deletions impractical-projects/2. Palindromes/Solutions/2of4brif.txt

Large diffs are not rendered by default.

Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

import cProfile
import palingrams
cProfile.run('palingrams.find_palingrams()')
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 impractical-projects/2. Palindromes/Solutions/load_dictionary.py
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 impractical-projects/2. Palindromes/Solutions/palindromes.py
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 impractical-projects/2. Palindromes/Solutions/palingrams.py
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))
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 impractical-projects/2. Palindromes/Solutions/palingrams_timed.py
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))

0 comments on commit 75e2cae

Please sign in to comment.