View the step-by-step solution to:

I am struggle to with my python program that:

I am struggle to with my python program that:


* asks the user a set of letters

* prints out all of the combinations of letters that make valid words __in 

 alphabetical order__

  * by using a dictionary file of words

  * and an algorithm specified in the instructions below

* see example output at end


I downloaded a dictionary of words

def find_words(user_inp, wordsfile):

  validwords=[]

  wfile=open("dictionary.txt", 'r')

  words=wfile.read()

  cleanwords=words.split('n')


  sortuser=''.join(sorted(user_inp))

  validwords.append(sortuser)

  print(validwords)


But when I create find_words

  * it needs two parameters (user_inp,wordsfile)

    * (a string representing the set of letters to build words from and a string that's the name of a dictionary file that contains all possible words)

I am stuck! The program needs to:

  * open the dictionary file and find valid words that can be formed from the set of letters

  * it should return a list of valid words


* then... I created another function, called main (to use in future if statement)

I wrote:

def main():

  letter=input('Enter a set of letters. A minimum of one, but not more than seven: n>')

  while letter.isalpha()== False or len(letter)<1 or len(letter)>7:

    letter=input('Try again. Enter a set of letters. A minimum of one, but not more than seven: n>')

  try:

    numberword=int(input('What is the max number of words to display?'))

  except:

    numberword=0


  * it is supposed to have no parameters and no return value

  * it will ask the user for a set of letters that are between 1 and 7 

   characters long (inclusive) but if the input is not all letters or if the input isn't at least one letter or is more than 7 letters it should be asking for input again

  * it will then ask the user for the maximum number of words to display

    * use try / except to deal with non-numeric input

    * if the user enter's non numeric input, just show all of the results


I then have to use the find_words function on the user input to get a list of valid

   words print out the result in sorted alphabetically that gives only at most, the max number of words entered by the user


and then I need to assert the function:


if __name__ == "__main__":

  main()


* use this algorithm to determine the valid words that can be formed from a 

 set of letters:

  * assertion is with a list of found words (there shouldn't be any words there yet!)

  * go through every word in your dictionary (that is, every line):

    * get rid of any leading or trailing white space with strip

    * do so with a temporary list out of the characters in the set of letters

     passed in to the function (this is to allow "removal" of characters)

      * create the temp list by using the built in function: list

      * for example: list("hey") --> ['h', 'e', 'y']

      * (split doesn't work with an empty separator)

    * for every character in the dictionary word...

      * if the character isn't in the temp list, that means that the

       dictionary word can't be formed by the letters in the temp 

       list... so stop, and don't add the word to your list of found

       words

      * if the character is in your temp list of characters, remove

       it from the temp list (so that it can't be "used" again)

      * once you've gone through all of the dictionary word's 

       characters, and they all existed (and have been removed) from 

       the temp list... you know the dictionary word can be formed by 

       the characters in your temp list! ...so add the word to your 

       list of found words

     * example:

      * set of letters: "tbe", current dictionary word: "be"

        * "b" from "be" is in ['t', 'b', 'e'] --> ['t', 'e']

        * "e" from "be" is in ['t', 'e'] --> ['t']

        * done... "be" can be formed from "tbe"

      * set of letters: "tbe", current dictionary word: "bee"

        * "b" from "bee" is in ['t', 'b', 'e'] --> ['t', 'e']

        * first "e" from "bee" is in ['t', 'e'] --> ['t', 'e']

        * second "e" from "bee" is NOT in ['t'] !!! 

        * done... "bee" CAN'T be formed from "tbe"


Example Interaction : max results are 3

-----

Please enter some letters... at least 1, but no more than 7

> tdri

What is the maximum number of words to display?

> 3

Showing max 3 results:

dirt

dit

id


Example Interaction: a non-numeric value is typed in, show all results

-----

Please enter some letters... at least 1, but no more than 7

> tdri

What is the maximum number of words to display?

> I don't want to say!

Showing all results:

dirt

dit

id

it

rid

ti

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question