Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem 6.b
#1
Hi everyone,

I feel like I'm close to an answer for this one.. but not quite. Would groupby be the way to go here?


Source: https://courses.cs.washington.edu/course...part1.html

Problem 6.b: Longest Word

Write a function longest_word that takes a string file_name and returns the longest word in the file with which line it appears on. If there are ties for the longest word, it should return the one that appears first in the file. If the file is empty or there are no words in the file, the function should return None. You may assume that the file_name describes a file that exists.

Suppose we had a file called poem.txt with the contents:

she sells
sea
shells by
the sea shore
Then the following call would return:

longest_word('poem.txt') # '3: shells'
Because the longest word is "shell"s and appears on line 3.


Reply
#2
What I have so far:

from itertools import groupby

def longest_line_length(file):
    file = open(file)
    
    file = "{}".format(file.read())
    print(file)
    if file is None:
        return None
    else:
        file = file.split()
        print(file)
        
        count = ["{}".format(len(x)) for x in file]
        print(count)
       # for x in file:
            
           # count = "{}".format(len(x))
        
        return(max(count))

    #I'm able to return the max count of "shells" in integer form.   **think** 
        
Input: longest_line_length("poem.txt")


Output:
she sells sea shells by the sea shore ['she', 'sells', 'sea', 'shells', 'by', 'the', 'sea', 'shore'] ['3', '5', '3', '6', '2', '3', '3', '5']
Reply
#3
I would
- use "with" when opening the file. Saves problems when you forget to close the file (like you did)
- read the file line by line. Split it. If one of the words has a length greater than the current max, save both the word and its length
Reply
#4
I've finally solved this problem. Please take a look and let me know what you think and where I can improve :) Many thanks!


def longest_line_length(file):
    
    
    with open(file, 'r') as file:
        
        if file is None:
            return None
        else:
            lines = file.readlines()
            count = []
            words = []
            
            for i, element in enumerate(lines):
                
                word = element.split()
                words.append(word)
                
                
                counting = ["{}".format(len(x)) for x in word]
                count.append(counting)
            
            b = count.index(max(count))
            
            longest_string = max(words[b], key=len)
            
            return(b+1, longest_string)
            
Reply
#5
def get_longest_line(file):
    """
    Returns the longest line number and the line itself
    file should be a file like object or a list with str.
    """
    line_iter = enumerate(file, 1)
    return max(line_iter, key=lambda x: len(x[1]))


text = """
she sells
sea
shells by
the sea shore
""".lstrip().splitlines()

print(get_longest_line(text))
Output:
(4, 'the sea shore')
Implementation without use of max:
def get_longest_line(file):
    iterator = enumerate(file, 1)
    longest_line_number = longest_line = None
    for line_number, current_line in iterator:
        if longest_line_number is None:
            longest_line_number = line_number
            longest_line = current_line
        if len(longest_line) < len(current_line):
            longest_line = current_line
            longest_line_number = line_number
    return longest_line_number, longest_line
Output:
(4, 'the sea shore')
Both implementations doesn't open the file oject, instead they are iterating over the object.
The input could be a list with str or a file like object or a real file object.
If it's a text-block, you can use the splitlines method to create a list of lines.
If it's a file, then you should use the context-manager which closes the file after leaving the with-block.

with open("some-file.txt") as fd:
    line_number, line = get_longest_line(fd)
And if you want to make Exception handling:


file = "/root/test.txt"
try:
    with open(file) as fd:
        line_number, line = get_longest_line(fd)
except PermissionError:
    print("Don't have the permission to read", file)
except FileNotFoundError:
    print(file, "not found")
except UnicodeDecodeError:
    print("Illegal unicode in text")
The last error comes indirectly from open. By default, a file is opened in textmode with the default system encoding. On Linux, it's UTF-8. If during decoding a error occurs, it will throw a UnicodeDecodeError exception.

You should read this:
https://docs.python.org/3/library/functions.html#open
encoding and errors.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020