IDLE.
def open_file_read(filename):
None
def word_set_from_file(filename):
Four score and seven years ago our fathers brought forth on this continent a new nation conceived in Liberty and dedicated to the ...
create an empty set
for each line in the file:
create the list of words by running split() on line
for each word in the word list:
make the word lowercase and add it to the set
return the set
def ordered_word_set_print(set):
def word_count(filename):
create a file object for reading
set count equal to 0
for each line in the file:
create list of the words in the file using split()
add the length of the list to count
return count
def set_difference(set_1, set_2):
filename_1 = "gettysburg.txt"
set_1 = word_set_from_file(filename_1)
ordered_word_set_print(set_1)
print()
print("Words in " + filename_1 + ":" + str(word_count(filename_1)))
filename_2 = "gettysburg_hay.txt"
set_2 = word_set_from_file(filename_2)
print()
print("Words in " + filename_1 + " not in " + filename_2)
ordered_word_set_print(set_difference(set_1, set_2))
print()
print("Words in " + filename_2 + " not in " + filename_1)
ordered_word_set_print(set_difference(set_2, set_1))
a above add advanced ... will work world years Words in gettysburg.txt:272 Words in gettysburg.txt not in gettysburg_hay.txt advanced battle field fought god under Words in gettysburg_hay.txt not in gettysburg.txt battlefield carried upon
pass. pass statement in open_file_read
with the body of the code from your
hw4.py script. pass statement in
word_set_from_file with a statement that creates the
empty set words_set.open_file_read using the
filename parameter. for loop with the loop variable
line for loop get rid of the linefeed at the end of the
line by assign line to the result of running strip on line. Four score and seven years ago our fathers brought forth on this continent a new nation conceived in Liberty and dedicated to the proposition that all men are created equal ...Fix any errors you find.
print statement. split() method on line. ['Four', 'score', 'and', 'seven', 'years', 'ago', 'our', 'fathers', 'brought', 'forth', 'on', 'this'] ['continent', 'a', 'new', 'nation', 'conceived', 'in', 'Liberty', 'and', 'dedicated', 'to', 'the'] ...Fix any errors you find.
print statement. for loop using the loop variable
word. Four score and ...Fix any errors you find.
print statement. lower. for loops print words_set.
{'government', 'proposition', 'for', 'a', 'struggled' ...
Fix any errors you find.
print statement from
word_set_from_file. pass statement from
ordered_word_set_print.
{'a', 'fathers', 'men', 'forth', 'we', 'created' ...
Fix any errors you find.
print statement from
ordered_word_set_print. for loop that prints each word in the set. a above add advancedFix any errors you find.
pass statement from word_count. open_file_read. for loop that prints each line in the file. print statement in the for loop. split() on the line. for loop print count. 272 Words in gettysburg.txt:NoneFix any errors you find.
print statement. Words in gettysburg.txt:272Fix any errors you find.
pass statement in set_difference. a above add advanced ... will work world years Words in gettysburg.txt:272 Words in gettysburg.txt not in gettysburg_hay.txt advanced battle field fought god under Words in gettysburg_hay.txt not in gettysburg.txt battlefield carried upon
cd it117/hw/hw5
hw5.py
a above add advanced ... will work world years Words in gettysburg.txt:272 Words in gettysburg.txt not in gettysburg_hay.txt advanced battle field fought god under Words in gettysburg_hay.txt not in gettysburg.txt battlefield carried upon
Copyright © 2021 Glenn Hoffman. All rights reserved. May not be reproduced without permission.