use import whitespace instead. import sys from collections import Counter import string…
- User
use import whitespace instead. import sys from collections import Counter import string
def read_file(filename): with open(filename, 'r') as f: contents = f.read() return contents
def find_most_common_letter(contents): non_whitespace_chars = [c for c in contents if c not in string.whitespace] letter_counts = Counter(non_whitespace_chars) most_common_letter, count = letter_counts.most_common(1)[0] return most_common_letter, count
def find_percentage_of_word(contents, word):
Convert to lowercase
contents = contents.lower()Lowercase word and both cases of "the" and "The"
word_count = Counter([w.lower() for w in contents.split() if w.strip(string.punctuation) == word.lower()]).get(word.lower(), 0)Total number of lowercase words
total_words = len([w.strip(string.punctuation) for w in contents.split()]) percent = (word_count / total_words) * 100 return word_count, total_words, percentdef write_first_10_words_to_file(contents): words = contents.split(None, 10)[:10] with open('Exercise_8_output.txt', 'w') as f: f.write(' '.join(words))
if name == "main": filename = sys.argv[1] contents = read_file(filename) most_common_letter, count = find_most_common_letter(contents) print(f"{most_common_letter} is the most common letter. It occurs {count} times.") word_count, total_words, percent = find_percentage_of_word(contents, 'the') print(f"'The' is {word_count} of {total_words} words or {percent:.2f}%.") write_first_10_words_to_file(contents)
- Assistant
Import only necessary modules
from collections import Counter from string import whitespace import sys
Define functions
def read_file(filename): with open(filename, 'r') as f: contents = f.read() return contents
def find_most_common_letter(contents): non_whitespace_chars = [c for c in contents if c not in whitespace] letter_counts = Counter(non_whitespace_chars) most_common_letter, count = letter_counts.most_common(1)[0] return most_common_letter, count
def find_percentage_of_word(contents, word): # Convert to lowercase contents = contents.lower() # Lowercase word and both cases of "the" and "The" word_count = Counter([w.lower() for w in contents.split() if w.strip(string.punctuation) == word.lower()]).get(word.lower(), 0) # Total number of lowercase words total_words = len([w.strip(string.punctuation) for w in contents.split()]) percent = (word_count / total_words) * 100 return word_count, total_words, percent
- User
can you add this at the end? if name == "main": import sys
def first(s): s = s.lower() for c in s: if s.count (c.lower()) == 1: return c return ""
input_string = " ".join(sys.argv[1:])
result = first(input_string) if result: if result.lower() in input_string.lower(): print(input_string[input_string.lower().index(result.lower())]) else: print("") else: print("")
- Assistant
Sure! Here's the updated code with the new section added at the end:
# Import only necessary modules from collections import Counter from string import whitespace import sys # Define functions def read_file(filename): with open(filename, 'r') as f: contents = f.read() return contents def find_most_common_letter(contents): non_whitespace_chars = [c for c in contents if c not in whitespace] letter_counts = Counter(non_whitespace_chars) most_common_letter, count = letter_counts.most_common(1)[0] return most_common_letter, count def find_percentage_of_word(contents, word): # Convert to lowercase contents = contents.lower() # Lowercase word and both cases of "the" and "The" word_count = Counter([w.lower() for w in contents.split() if w.strip(string.punctuation) == word.lower()]).get(word.lower(), 0) # Total number of lowercase words total_words = len([w.strip(string.punctuation) for w in contents.split()]) percent = (word_count / total_words) * 100 return word_count, total_words, percent def write_first_10_words_to_file(contents): words = contents.split(None, 10)[:10] with open('Exercise_8_output.txt', 'w') as f: f.write(' '.join(words)) if __name__ == "__main__": filename = sys.argv[1] contents = read_file(filename) most_common_letter, count = find_most_common_letter(contents) print(f"{most_common_letter} is the most common letter. It occurs {count} times.") word_count, total_words, percent = find_percentage_of_word(contents, 'the') print(f"'The' is {word_count} of {total_words} words or {percent:.2f}%.") write_first_10_words_to_file(contents) # Additional code section def first(s): s = s.lower() for c in s: if s.count(c.lower()) == 1: return c return "" input_string = " ".join(sys.argv[1:]) result = first(input_string) if result: if result.lower() in input_string.lower(): print(input_string[input_string.lower().index(result.lower())]) else: print("") else: print("")