Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions Python-Home-Challenges/Creating_file.sh
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

25 changes: 25 additions & 0 deletions Python-Home-Challenges/Python-home-challenge#01-MaorWeiss.py
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
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.

no reason to declare new variable here

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.

Do you mean new variables? or just count variable..? @AviadP

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.

there is no reason just to assign "split_words_from_file" into "copy_list"

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)
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.

syntax error, missing '('
also, using f' string is considered as best practice for python 3

fp.close()
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.

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

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.

Working on it. But why my script won't get to that point?

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.

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.

in case of exception in one of previous lines, like syntax error, type error etc.

# Close file #
4 changes: 4 additions & 0 deletions Python-Home-Challenges/insructions_challenge#01.txt
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!