![]() |
Problem 6.b - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Problem 6.b (/thread-31222.html) |
Problem 6.b - nman52 - Nov-29-2020 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/courses/cse163/19sp/hw1/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. RE: Problem 6.b - nman52 - Nov-29-2020 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")
RE: Problem 6.b - jefsummers - Nov-29-2020 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 RE: Problem 6.b - nman52 - Dec-26-2020 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) RE: Problem 6.b - DeaD_EyE - Dec-26-2020 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)) 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 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. |