Simple Python Program #2 of 5 I have 4 other programs posted as well, they are a
ID: 3582232 • Letter: S
Question
Simple Python Program #2 of 5
I have 4 other programs posted as well, they are all simple, but I don't have time to do them. Due in a few hours.
Generating All Strings
Earlier we considered the fraction of n-letter strings that are words, and we determined that the total number of n-letter strings formed from an alphabet of c letters is given by nc. In this exercise, you are going to create a file containing all the string combinations, and count them.
(a) We’ll start by constructing all possible two-letter strings from an alphabet of 26 characters. We did this earlier, by hand, with alphabets of three or four characters, but now we have a computer. We know that there are 26 × 26 = 676 possibilities. The way to do this is with two for-loops, one nested inside the other. For each letter of the 26 letters of the alphabet (taken in turn), you have to concatenate (add) each letter of the alphabet.
It goes like this AA, AB, AC, …, AZ, BA, BB, BC, …, BZ, …, …, …, ZA, ZB, ZC, …, ZZ. You can do a warm-up exercise where you write one string per line.
Don’t make a list or a big string and then write that: just write them one at a time, as they are created. Use a count variable to keep track of how many words there are, and print a message to Idle saying how many strings you printed altogether. In your final program, I want you to write the strings in a format where all the strings starting with A are on a single line, separated by spaces, then there is a blank line, then all the strings starting with B are on the next line, separated by spaces, and so on.
Also count them and print out (to Idle) a message telling the number of strings you have created. Remember to close the file when you are finished writing, in order to insure that the program actually finishes the job. (For those who want to know, Python uses buffering when it writes to a file, and closing the file flushes out anything that might be remaining in the buffer.)
Hint: when you are “looping over the alphabet” you can do a for-loop over a string just as easily as you can over a list. In this case, the string is “ABCDEFGHIJKLMNOPQRSTUVWXYZ”. Call your python program Problem-2-all-2-letter-strings.py and your output file Problem-2-all-2-letter-strings.txt.
(b) There are 26 × 26 × 26 = 17, 576 different strings of length three from a alphabet of 26 letters. That might sound like a lot, and it certainly would be too many to write down by hand. But Python can take care of it in the blink of an eye. For this part, print all three letter strings to a file, one string per line. While you’re writing them (one by one), count them, and print to Idle a message telling the number of string you generated.
Call the program Problem-2-all-3-letter-strings.py and the output file Problem-2-all-3-letter-strings.txt.
Final question: what’s the limit on the length of string that you can handle in this way on your computer?
Explanation / Answer
answer (a):
output:
output:
answer final question:
max limit vary from computer to computer based on the ram
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.