辅导python、辅导words程序、讲解python编程
- 首页 >> Python编程Files to submit: words.py
Time it took Matthew to Complete: 10 mins
Requirements
● Submit only the files requested
● Print all real numbers to 2 decimal points unless stated otherwise
Restrictions
● No global variables may be used
● The only code that may appear outside of a function are function definitions and function
calls
● NEW: You may not import any modules
Description
Write a program called words.py that when provided with a dictionary and a list of characters prints
all possible words that can be formed using those characters.
Problem Details
● Your program will be provided a file that contains all of the known words with one word per
line
○ All of the words in the file are lowercase
● Your program will be provided with a list of letters
○ The letters will all be on one line with a space between each letter
● Your program should output all possible words that be formed using those letters
○ These words should be displayed in alphabetical order
○ No duplicates should be displayed
○ Each letter can only be used a single time
■ For example if the user inputs a and p then the word app can NOT be formed
as it uses the same letter, p, twice.
■ If the user inputs a p p then the word app could be formed as you use each p
once
Input
● All input will be valid
● The name of the file containing all of the words
● The letters being used
○ All on one line with a space between each letter
Hints
● You'll want to use a set to hold the words from the file.
○ Sets model the mathematical concept of a set: a collection of unique things
○ Sets have very fast lookups (using the in keyword)
○ The methods on sets you'll probably need are
■ set.add(element)
■ Add element to the the set
■ element in set
■ Check if element is in set
Examples
User input has underlined to help you differentiate what is user input and what is program output.
You do not need to underline anything.
Example 1
Enter the name of the file containing all of the words: english.txt
Please enter your letters separated by a space: c a t b
a
ab
abc
abt
ac
act
at
bat
ca
cab
cat
ct
tab
Example 2
Enter the name of the file containing all of the words: english.txt
Please enter your letters separated by a space: u u b t c s
bus
bust
but
buts
cs
cst
ct
cts
cub
cubs
cut
cuts
sc
scut
st
stub
sub
tbs
tub
tubs
us
ut