Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions Basics/Exercise/13_read_write_files/exercise_2_stocks.py
Original file line number Diff line number Diff line change
@@ -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}")