Lab 5
Study a recursive algorithm that produces letter unscramblings
Submit after the end of the lab session
Overview
For this lab, you will study a recursive function that runs through all letter permutations (unscramblings) for a given string. In addition to counting the number of checked permutations, you will modify it so that it includes attempts that use any subset of the letters.
Instructions, questions, and code writing
- Download the zip folder for the lab.
- Run unscramble with strings of various lengths. At what string length does the function take too long for your patience?
- In your own words, describe what happens to the source string and target string at each level.
- How many permutations does a 3 letter string produce? How aboue 4 letters? Write down your answers! How do you know?
- Uncomment the dictionary check (if target in ds) and indent the print so that it only prints actual words. Run it several times on different combinations of letters to see how it works.
- Using the Counter class, add a counter that keeps track of how many dictionary checks occur:
- Create a counter object in unscramble before the recurrence function is called. Give it a meaningful variable name.
- Add the name of your counter object as a fifth argument/parameter to all recurrence calls and its definition.
- Just before the dictionary check in the recurrence function, call the count method on your Counter object.
- At the end of unscramble, print the counter object.
- Run unscramble with the counter. Does its output confirm your predictions?
- Create copies of unscramble and recurrence and call them unscramble2 and recurrence2. Make sure that your copies refer to the new name for all function calls. Modify recurrence2 so that it checks the character target even when there are still characters left in stock. This way, unscramble2 will report letter combinations that only use some of the characters from the original string. Test and check your new function.
- Question: the recurrence function is somewhat inefficient in that it explores possibilities that won't produce any words. How could this function be made more efficient? Required: a throughtful, refective answer. Optional (somewhat advanced): an implementation of your efficient modification.
Deliverable
Create a text (preferred) or pdf file that contains the following:
- A statement that summarizes your completion of the lab. As appropriate, the statement should
include the following:
- Who you worked with on the lab
- Any difficulties you encountered
- Answer to questions
- Pasted output showing your functions running
- A summary of the files included in the zip folder
Put your files in a folder, zip it and submit it under Lab 5 on D2L. Check that your submitted zip file is complete.
Grading
Your lab submission will be graded using the following rubric:
- + .5 --- Your submission is clearly formatted.
- + .5 --- Your submission includes a summary statement and includes how you collaborated.
- + .5 / 1.0 --- You submitted most of the lab (0.5) or you submitted all of the lab (1.0).
- + .5 --- Your lab submission is generally correct.