How would I write a code in Python 3.5 to solve this, assuming the dictionary.tx
ID: 3667159 • Letter: H
Question
How would I write a code in Python 3.5 to solve this, assuming the dictionary.txt file is in the same directory?
This lab exercise provides practice with file processing You will work with a partner on this exercise during your lab session. Two people should work at one computer. Occasionally switch the person who is typing. Talk to each other about what you are doing and why so that both of you understand each stejp Part A: Word Puzzle #1 Will Shortz is a noted puzzlemaster for the New York Times and National Public Radio who frequently posts challenging word puzzles. Many word puzzles can be solved by iterating through a list of words while checking for characteristics specified by the puzzle. Many word lists exist online and we provide one with about 250,000 words, one word per line, named dictionary.txt. Find a word that contains the vowels a, e, i, o and u, one each. in that order. How to solve this? Beginning programmers often overlook the easiest approach which is called "brute force." In this case, that means to read through the file of words one word at a time (remember there is one word per line) and check each one to see if it meets the requirements of the puzzle. When you find one that fits, print it. (I found 16 words that fit in this word list.) Hint: use for to iterate through the file Demonstrate your completed program to your TA. On-line students should submit the completed program (named "lab05 A.py") for grading via the CSE handin system Part B: Word Puzzle #2 Find an uncapitalized, seven-letter word, that doesn't have an "s" anywhere in it and contains just a single vowel. In this case include "y" as a vowel so your vowels are a, e, i, o, u and y, . Again use the dictionary.txt word file and brute force. You may find the continue statement to be useful: if the word doesn't match one of the requirements, use continue to jump to the next word in the file (that is, go to the beginning of your loop). I found that checking all the requirements to be easy except for "contains just a single vowel." I found it useful to have a Boolean I named has_one_vowel which I set to be False before I checked a word. I found that it helped me keep track of whether I had already found a vowel. (I found 3 words that fit in this word list.)Explanation / Answer
Part A:
with open('dictionary.txt', 'r') as f:
for line in f:
for word in line.split():
word = word[::-1]
if 'u' in word and 'o' in word and 'i' in word and 'e' in word and 'a' in word:
if word.index('u') < word.index('o') and word.index('o') < word.index('i') and word.index('i') < word.index('e') and word.index('e') < word.index('a'):
print(word[::-1])
Part B:
with open('dictionary.txt', 'r') as f:
for line in f:
for word in line.split():
vowelCount = 0
if 's' in word and len(word) == 7:
for i in range(0, len(word)):
if word[i] in 'aeiou':
vowelCount = vowelCount + 1
if vowelCount == 1:
print(word)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.