-
Notifications
You must be signed in to change notification settings - Fork 11
Python-Home-Challenges-Maor Weiss #10
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?
Changes from 2 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,3 @@ | ||
| cd /home/$USER/ | ||
| echo "please do not do what ever you want to do" > file.txt | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # HW03 - Open a text file and returns the most recurring word in that file. | ||
|
|
||
| fp = open('/file.txt', 'r') | ||
| # Open file # | ||
| f = fp.readline() | ||
| # Read file # | ||
| split_words_from_file = f.split() | ||
|
|
||
| the_most_recurring_word = '' | ||
| biggest_counting = 0 | ||
|
|
||
| copy_list = split_words_from_file | ||
| for list_arg in copy_list: | ||
| for word in split_words_from_file: | ||
| count = 0 | ||
| if list_arg == word: | ||
| count += 1 | ||
| if count > biggest_counting: | ||
| the_most_recurring_word = list_arg | ||
| biggest_counting = count | ||
|
|
||
|
|
||
| print 'The word "{}" which has appeared {} times is the most recurring word in the file.'.format(the_most_recurring_word, biggest_counting) | ||
|
Contributor
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. syntax error, missing '(' |
||
| fp.close() | ||
|
Contributor
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 close is important, but in case of error or exception, your script wont get to that point, and file will remain open. best practice is is to use "with...open..." for files operations
Author
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. Working on it. But why my script won't get to that point?
Author
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.
Contributor
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. in case of exception in one of previous lines, like syntax error, type error etc. |
||
| # Close file # | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| 1. Open the "Creating_file.sh" file | ||
| 2. execute the "Python-home-challenge#01-MaorWeiss.py" file | ||
|
|
||
| Have a nice day! |
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.
no reason to declare new variable here
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.
Do you mean new variables? or just count variable..? @AviadP
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 reason just to assign "split_words_from_file" into "copy_list"