-
Notifications
You must be signed in to change notification settings - Fork 11
home work #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
home work #16
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
file = open("C:/Users/tomer/PycharmProjects/HomeChallenge3/Readme.txt") | ||
words = open("C:/Users/tomer/PycharmProjects/HomeChallenge3/Readme.txt").read().split() | ||
|
||
print(file.read()) | ||
print(words) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this output is not needed |
||
|
||
|
||
def keywithmaxval(d): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use '_' between word - should looks like: key_with_max_val |
||
""" a) create a list of the dict's keys and values; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. docstring should contain:
|
||
b) return the key with the max value""" | ||
v = list(d.values()) | ||
k = list(d.keys()) | ||
return k[v.index(max(v))] | ||
|
||
|
||
def word_count(words): | ||
counts = dict() | ||
for word in words: | ||
if word in counts: | ||
counts[word] += 1 | ||
else: | ||
counts[word] = 1 | ||
return keywithmaxval(counts), max(counts.values()) | ||
|
||
|
||
print("max word is:", (word_count(words))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it is better using "main" function, that calling other functions |
||
print(file.read()) | ||
file.close() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. using context manager( with...open...) is much better way to handle files operations. in case program will exit on error, file might remain opened |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
hello hello tomer hello tomer tomer maya maya hello eran tomer tomer roni roni roni roni roni roni |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there is no need to open file twice. "file" variable declared adlready