Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fable's solution to Python Poetry #19

Open
wants to merge 2 commits into
base: gh-pages
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Windows image file caches
Thumbs.db
ehthumbs.db

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msm
*.msp

# =========================
# Operating System Files
# =========================

# OSX
# =========================

.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon


# Thumbnails
._*

# Files that might appear on external disk
.Spotlight-V100
.Trashes

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

# other
.idea
1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/puzzles.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/scopes/scope_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

394 changes: 394 additions & 0 deletions .idea/workspace.xml

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions poetry.html
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ <h2>Solutions</h2>
<li>Matt Page-Lieberman's solution is <a href="https://github.com/BostonPython/puzzles/blob/gh-pages/solutions/jotaemei/pythonpoetry.py">pythonpoetry.py</a></li>
<li>Aaron Hill's solution is <a href="https://github.com/BostonPython/puzzles/blob/gh-pages/solutions/Aaron1011/poetry.py">poetry.py</a></li>
<li>Josh McGrath's solution is <a href="https://github.com/BostonPython/puzzles/blob/gh-pages/solutions/joshmcgrath08/poetry.py">poetry.py</a></li>
<li>Fable Turas's solution is <a href="https://github.com/BostonPython/puzzles/blob/gh-pages/solutions/FableT/PyPoetry.py">PyPoetry.py</a></li>
</ul>

<p>If you have a solution you'd like to share see the <a href="solutions.html">Solutions page</a> for instructions.</p>
74 changes: 74 additions & 0 deletions solutions/FableT/PyPoetry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# load poem or phrase from file into variable
with open('poem.txt') as f:
poem = f.read()

# load unix words dictionary into words variable
with open('usr/share/dict/words') as w:
words = w.read().splitlines()


# pick out all letters with unique, non-zero counts
def unique_letters(key_phrase):
available_letters = set([c for c in key_phrase if c.isalpha()])
letter_counts = [key_phrase.count(c) for c in available_letters]
return [c for c in available_letters if letter_counts.count(key_phrase.count(c)) == 1]


# creates mapping with code as key for decoding
code_into_letter = {poem.count(c): c for c in unique_letters(poem)}

# creates mapping with letter as key for encoding
letter_into_code = {c: poem.count(c) for c in unique_letters(poem)}


# encode or decode the riddle
def riddle_me_this(riddle, mapping):
return [mapping[key] for key in riddle]


# returns all words that can be used in a door riddle
def riddle_words(all_words, available_letters):
return [riddle for riddle in all_words if set(riddle).issubset(available_letters)]


# returns all riddle codes for potential door riddles
def coded_riddles(all_words, mapping):
return [riddle_me_this(word, mapping) for word in all_words]


# print list items to file, one item per line
def print_to_file(file_name, list_to_print):
fo = open(file_name, "w")
for item in list_to_print:
fo.write(str(item) + "\n")
fo.close()


# print dictionary to file, one key/value pair per line
def print_dict(file_name, dict_to_print):
fo = open(file_name, "w")
for key, value in dict_to_print.items():
fo.write(str(key) + ": " + str(value) + "\n")
fo.close()


# relevant solutions saved to variables
all_letters = unique_letters(poem)
word_list = riddle_words(words, all_letters)
riddle_codes = coded_riddles(word_list, letter_into_code)
answer_key = {tuple(code): ''.join(riddle_me_this(code, code_into_letter)) for code in riddle_codes}
longest_word = max(word_list, key=len)

#print comprehensive lists to files
print_to_file("word_list.txt", word_list)
print_to_file("riddle_list.txt", riddle_codes)
print_dict("answer_key.txt", answer_key)

if __name__ == '__main__':
print("Available letters are: {}".format(', '.join(unique_letters(poem))))
print("Letter mapping is: {}".format(letter_into_code))

print("\nThe answer to the riddle: [56,38,44,56,29] is '{}'".format(''.join(riddle_me_this([56, 38, 44, 56, 29], code_into_letter))))
print("\nThe longest riddle is {}".format(riddle_me_this(longest_word, letter_into_code)))
print("Its solution is '{}'".format(longest_word))
print("\nSee the text files for a complete riddle list, a complete word list, or an answer key with riddles and their solutions")
29 changes: 29 additions & 0 deletions solutions/FableT/poem.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
a narrow fellow in the grass
occasionally rides;
you may have met him, did you not,
his notice sudden is.

the grass divides as with a comb,
a spotted shaft is seen;
and then it closes at your feet
and opens further on.

he likes a boggy acre,
a floor too cool for corn.
yet when a child, and barefoot,
i more than once, at morn,

have passed, i thought, a whip-lash
unbraiding in the sun,
when, stooping to secure it,
it wrinkled, and was gone.

several of nature's people
i know, and they know me;
i feel for them a transport
of cordiality;

but never met this fellow,
attended or alone,
without a tighter breathing,
and zero at the bone.