Help
cancel
Showing results for
Did you mean:
Highlighted
Pilot Lvl 1
Message 1 of 6

## Letters in decreasing order of frequency

Solved! Go to Solution.

Hey everone, so I had this exercise :

"Write a program that reads a file and prints the letters
in decreasing order of frequency. Your program should convert all the
input to lower case and only count the letters a-z. Your program should
not count spaces, digits, punctuation, or anything other than the letters
a-z."

And I managed to solve it, and here's a part of my code :

```fname = input("Enter a file name: ")
if len(fname) < 1 : fname = "romeo.txt"
try :
fhand = open(fname)
except :
print("Enter a proper file name",fname )
quit()

#read the file into a big string

#lower the string
words = words.lower()

letters = list()
for i in words :
if i.isalpha() == True :
letters.append(i)

```

And it works just fine. However, I was looking up for other solutions online and I found one in the answer section of the book(Python for everybody), it's pretty much like mine but it has an extra line :

```words = fhand.read()

#lower the string, don't worry about digits, punctuation etc.
words = words.lower()

#change the string into single letters
twords = tuple(words)

letters = list()
for i in twords :
#we only choose the alphabet letters
if i.isalpha() == True :
letters.append(i)```

I was wondering why the following line is added ? Because the code just works fine without it

`twords = tuple(words)`

5 Replies
Commander Lvl 1
Message 2 of 6

## Re: Letters in decreasing order of frequency

Without being able to absolutely confirm this, I believe this is a historical oddity. The behavior of strings changed between Python 2 and Python 3; the tuple() line is likely necessary for Python 2 support with Unicode?

Please follow-up to let us know how you made out. For good karma, mark a reply as the answer if it helped!

Pilot Lvl 1
Message 3 of 6

## Re: Letters in decreasing order of frequency

Thanks!

I'm not sure but maybe it's what you're saying because in some of the book slides,there are references to Python 2.

Solution
Commander Lvl 2
Message 4 of 6

## Re: Letters in decreasing order of frequency

I dont think there is a real reason. I think the only one reason that i have in mind is maybe to split the tasks of words and twords. The output of letters with twords and words are the same (I didnt tried it but I'm sure of that). To test the output of words and twords you can do something like this:

```fname = input("Enter a file name: ")
if len(fname) < 1 : fname = "romeo.txt"
try :
fhand = open(fname)
except :
print("Enter a proper file name",fname )
quit()

#read the file into a big string

#lower the string
words = words.lower()

letters = list()
for i in words :
if i.isalpha() == True :
letters.append(i)

twords = tuple(words)

letters2 = list()
for i in twords :
if i.isalpha() == True :
letters2.append(i)

print(letters==letters2)```

and if the output is True than the output are the same (I'm sure it will).

Hope to solve your doubts

-Gabriele-

Pilot Lvl 1
Message 5 of 6

## Re: Letters in decreasing order of frequency

Yeah I think it's used maybe to explain a little more about what's going on. And yes the output is exactly the same

Thanks !

Commander Lvl 2
Message 6 of 6

## Re: Letters in decreasing order of frequency

Dont forget to set it as solution 🙂

New solutions