I am trying to write a program that will read a file of words and will print the length of the longest word and the words of that length.
I have found out that there are 3, 13 letter words in my file (including punctuation), but i need the program to find the longest word lengths on its own.
Here is my program:
def main():
filename = input("What is the filename?")
with open(filename) as f:
linenum = 1
for line in f:
words = line.split()
longest = ''
for word in words:
if len(longest) < len(word):
longest = word
print("Line", linenum, "has", longest, "as the longest word.")
linenum += 1
print(longest)
main()
My program returns:
What is the filename?test.txt
Line 1 has Working as the longest word.
Working
Line 2 has possibilities as the longest word.
possibilities
Line 3 has scrambled as the longest word.
scrambled
Line 4 has letters. as the longest word.
letters.
Line 5 has as the longest word.
Line 6 has difficulties as the longest word.
difficulties
Line 7 has permutations. as the longest word.
permutations.
Line 8 has signature as the longest word.
signature
Line 9 has permutations. as the longest word.
permutations.
Line 10 has unscrambled as the longest word.
unscrambled
Do i need to input a character function? How will the program find that the longest word is 13 characters.
With a list comprehension:
lorem.py
Lorem Ipsum is simply dummy text of the printing and typesetting
industry. Lorem Ipsum has been the industry's
standard dummy text ever since the 1500s, when an
unknown printer took a galley of type and scrambled it to make a
type specimen book. It has survived not only five centuries, but
also the leap into electronic typesetting, remaining essentially unchanged.
It was popularised in the 1960s with the release of
Letraset sheets containing Lorem Ipsum passages, and more
recently with desktop publishing software
like Aldus PageMaker including versions of Lorem Ipsum.
long.py
def main():
X=5
print "Printing top "+str(X)+" longest words:"
filename = "lorem.txt"
# Can't get this under 80 characters:
words = [(linenum, word) for linenum, line in enumerate(open(filename)) for word in line.split()]
words = sorted(words,key = lambda x: len(x[1]), reverse=True) #Sort by string length in reverse
for word in words[:X]:
print "The longest word '",word[1],"'is on line ",str(word[0])
if __name__ == "__main__":
main()
output
>>>
Printing top 5 longest words:
The longest word ' typesetting, 'is on line 5
The longest word ' popularised 'is on line 6
The longest word ' essentially 'is on line 5
The longest word ' typesetting 'is on line 0
You can retrieve the top X words by adjusting the variable.
This is much simpler if you don't need the line number:
long_no_linenum.py
def main():
X=5
print "Printing top " + str(X) + " longest words:"
filename = "lorem.txt"
words = (word for word in open(filename).read().split())
words = sorted(words, key=len, reverse=True)
print [word for word in words[:X]]
if __name__ == "__main__":
main()
output without line numbers
>>>
Printing top 5 longest words:
['typesetting,', 'typesetting', 'essentially', 'popularised', "industry's"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With