diff --git a/Basics/Exercise/13_read_write_files/exercise_2_stocks.py b/Basics/Exercise/13_read_write_files/exercise_2_stocks.py index f71442ba..278e9406 100644 --- a/Basics/Exercise/13_read_write_files/exercise_2_stocks.py +++ b/Basics/Exercise/13_read_write_files/exercise_2_stocks.py @@ -1,12 +1,15 @@ -with open("stocks.csv", "r") as f, open("output.csv", "w") as out: - out.write("Company Name,PE Ratio, PB Ratio\n") - next(f) # This will skip first line in the file which is a header - for line in f: - tokens = line.split(",") - stock = tokens[0] - price = float(tokens[1]) - eps = float(tokens[2]) - book = float(tokens[3]) - pe = round(price / eps, 2) - pb = round(price / book, 2) - out.write(f"{stock},{pe},{pb}\n") +from collections import Counter + +with open("poem.txt", "r") as file: + text = file.read().lower() + +words = text.split() + +word_count = Counter(words) + +max_count = max(word_count.values()) + +most_frequent_words = [word for word, count in word_count.items() if count == max_count] + +print(f"Most frequent words: {most_frequent_words}") +print(f"Occurrences: {max_count}")