Skip to content
Open
Changes from 1 commit
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
22 changes: 22 additions & 0 deletions Python-Home-Challenges/recurring word.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

where_file = input('Enter the file route: ')
my_file = open(where_file,"r")
print("Your file text is:")
for x in my_file:
print(x)
print("---------------")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this output is not required

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that was just for my debugging, will be deleted.

words_in_text = dict()
my_file = open(where_file,"r")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you dont need to open file again, also, file must be closed after ward. best practice is to use "open...with" when working with files.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

roger that!
will be fixed.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you dont need to open file again

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix.
strange, i was sure that i removed it.

for line in my_file:
line = line.strip()
words = line.split(" ")
for word in words:
if word in words_in_text:
words_in_text[word] = words_in_text[word] + 1
else:
words_in_text[word] = 1
for key in list(words_in_text.keys()):
print(key, ":", words_in_text[key])
print("---------------")
print (max(words_in_text.values()))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check you code again, output is wrong

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now its working with nice output
for highest in list(words_in_text.keys()): if max1 < words_in_text[highest]: max1 = words_in_text[highest] key1 = highest print ("The most recurring word is: ", key1," with ",max1, " times")