Question

Q1- say a letter in a word is missing, can you find the missing letter? For...

Q1- say a letter in a word is missing, can you find the missing letter? For example, let the word be 'bas', then the original word could have been 'bias', 'bass', or even some other words. Use the same word list as assignment 7 (https://www.mit.edu/~ecprice/wordlist.10000 (Links to an external site.)) to find the original words. Same code to generate the word list in Python.

import pandas
a = pandas.read_fwf('https://www.mit.edu/~ecprice/wordlist.10000',header=None,names=['w'])
wordlist = a['w'].tolist()

Q (a) -- Suppose we know the index where a letter is missing, write a function findOrigWords(word, index) to return a list of all possible original words. If there doesn't exist such a word, or if the index is invalid, then return the empty list. For example, findOrigWords('bas', 3) searches for a word 'bas_', and will return ['base', 'bass']. Similarly, findOrigWords('bas', 0) searches for '_bas', findOrigWords('bas', 1) searches for 'b_as', findOrigWords('bas', 2) searches for 'ba_s', and findOrigWords('bas', -1) or findOrigWords('bas',4) will return empty list [].

Q(b) - Write a function countOrigWords(word) to return a dictionary, where keys are all possible indices to insert the missing letter, and values are the counts of the completed words. For example, countOrigWords('bas') will return {1:2, 2:3, 3:2}, which means in the word list there are no words in format '_bas', 2 words in format 'b_as', 3 words in format 'ba_s', and 2 words in format 'bas_'. Note: all the values should be positive integers. If a count is 0, then the corresponding index should be removed out of the dictionary.

(python)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.

#code

import pandas

# from the url, loading the words to a list
a = pandas.read_fwf('https://www.mit.edu/~ecprice/wordlist.10000', header=None, names=['w'])
wordlist = a['w'].tolist()


'''required method to generate a list of possible words where a letter is added at
   specified index.
'''
def findOrigWords(word, index):
    # if index is invalid, returning empty list
    if index < 0 or index > len(word):
        return []
    # converting word to lower to minimize comparisons
    word = word.lower()
    # getting string before index
    pre = word[:index]
    # getting string after index
    post = word[index:]
    # creating an empty list
    result = []
    # looping through each possible alphabet, once
    for c in 'abcdefghijklmnopqrstuvwxyz':
        # adding c between pre and post
        combined = pre + c + post
        # checking if this word is in wordlist
        if combined in wordlist:
            # appending combined to result
            result.append(combined)
    # returning result
    return result



''' method to return a dictionary, where keys are all possible indices to insert the 
    missing letter, and values are the counts of the completed words 
'''
def countOrigWords(word):
    #creating a dict
    result = dict()
    #looping from 0 to len(word)
    for i in range(len(word) + 1):
        #finding possible words when a character is inserted at index i
        possible_words = findOrigWords(word, i)
        #if returned list is not empty, adding to result dict with key being i and 
        #value being the length of possible_words list
        if len(possible_words) > 0:
            result[i] = len(possible_words)
    return result


#testing
print(findOrigWords('bas', 0)) # []
print(findOrigWords('bas', 1)) # ['bias', 'bras']
print(findOrigWords('bas', 2)) # ['bags', 'bars', 'bass']
print(findOrigWords('bas', 3)) # ['base', 'bass']
print(findOrigWords('bas', 4)) # []
print(countOrigWords('bas')) # {1: 2, 2: 3, 3: 2}

Add a comment
Know the answer?
Add Answer to:
Q1- say a letter in a word is missing, can you find the missing letter? For...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Solve it in Python 4. An alternade is a word in which its letters, taken alternatively...

    Solve it in Python 4. An alternade is a word in which its letters, taken alternatively in a strict sequence, and used in the same order as the original word, make up at least two other words. All letters must be used, but the smaller words are not necessarily of the same length. For example, a word with seven letters where every second letter is used will produce a four-letter word and a three-letter word. Here are two examples: "board":...

  • Question 2: Finding the best Scrabble word with Recursion using java Scrabble is a game in...

    Question 2: Finding the best Scrabble word with Recursion using java Scrabble is a game in which players construct words from random letters, building on words already played. Each letter has an associated point value and the aim is to collect more points than your opponent. Please see https: //en.wikipedia.org/wiki/Scrabble for an overview if you are unfamiliar with the game. You will write a program that allows a user to enter 7 letters (representing the letter tiles they hold), plus...

  • For this week's lab, you will use two of the classes in the Java Collection Framework:...

    For this week's lab, you will use two of the classes in the Java Collection Framework: HashSet and TreeSet. You will use these classes to implement a spell checker. Set Methods For this lab, you will need to use some of the methods that are defined in the Set interface. Recall that if set is a Set, then the following methods are defined: set.size() -- Returns the number of items in the set. set.add(item) -- Adds the item to the...

  • For this week's lab, you will use two of the classes in the Java Collection Framework:...

    For this week's lab, you will use two of the classes in the Java Collection Framework: HashSet and TreeSet. You will use these classes to implement a spell checker. Set Methods For this lab, you will need to use some of the methods that are defined in the Set interface. Recall that if set is a Set, then the following methods are defined: set.size() -- Returns the number of items in the set. set.add(item) -- Adds the item to the...

  • python List1=[ ["a", 1], ["b", 3], ["c", 3], ["d", 2], ["e", 1], ["f", 4], ["g", 2]]...

    python List1=[ ["a", 1], ["b", 3], ["c", 3], ["d", 2], ["e", 1], ["f", 4], ["g", 2]] 1 def function(Letter,List),example, function("c",List) return 3 2 def function(word,List),example, function("a",List) return 1, function("be",List) return 4, function("bee",[b,5],[e,20]) return 45 Dictionary = ["a","bee","ad","ae"] 3 ["a","b","y","e"] return[["a",1],["ae",2],["bee",5]] 4 input a list, find largest value words can spell, return words and value dont using loop, using recursion for those code

  • In problem 3, you will write a function, findvertically (), which will determine whether or not...

    In problem 3, you will write a function, findvertically (), which will determine whether or not a given word exists in a word search puzzle vertically. In word search puzzles, words can be written upwards or downwards. For example, "BEAK" appears at [1] [3] written downwards, while "BET" appears at [2] [2] written upwards: [["C", "A", "T", "X", "R"], ["D", "T", "E", "B", "L"], ["A" "R" "B", "E", "Z"], ["X", "O", "E", "A", "U"], ["J", "S", "O", "K", "W"]] Your...

  • write the following code in python 3.2+. Recursive function prefered. Thank you! Define a function named...

    write the following code in python 3.2+. Recursive function prefered. Thank you! Define a function named q3() that accepts a List of characters as a parameter and returns a Dictionary. The Dictionary values will be determined by counting the unique 3 letter combinations created by combining the values at index 0, 1, 2 in the List, then the values at index 1, 2, 3 and so on. The q3() function should continue analyzing each sequential 3 letter combination and using...

  • # DISCUSSION SECTION WORK: # # 1. STUDENTS: download this file, ds4.py, and wordsMany.txt, from #...

    # DISCUSSION SECTION WORK: # # 1. STUDENTS: download this file, ds4.py, and wordsMany.txt, from # http://www.cs.uiowa.edu/~cremer/courses/cs1210/etc/ds4/ # Save both in the same folder. # # 2. TA (aloud) and STUDENTS: Read the comments from START HERE! (just after these instructions) # to definition of anagramInfo function. Discuss any questions about what the functions should do. # # 3. TA demonstrate running anagramInfo("wordsMany.txt") on this unchanged file, to # see that it behaves reasonably despite having incomplete anagram-testing functions. #...

  • PLEASE DO IN JAVA You can use any source for the dictionary or just a few...

    PLEASE DO IN JAVA You can use any source for the dictionary or just a few words are fine! description The purpose of this assignment is to practice with ArrayLists (and hopefully, you'll have some fun). As far as the user knows, play is exactly as it would be for a normal game of hangman, but behind the scenes, the computer cheats by delaying settling on a mystery word for as long as possible, which forces the user to use...

  • In python Count the frequency of each word in a text file. Let the user choose...

    In python Count the frequency of each word in a text file. Let the user choose a filename to read. 1. The program will count the frequency with which each word appears in the text. 2. Words which are the spelled the same but differ by case will be combined. 3. Punctuation should be removed 4. If the file does not exist, use a ‘try-execption’ block to handle the error 5. Output will list the words alphabetically, with the word...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT