Hello everyone! I\'m having a hard time understanding this homework and I can\'t
ID: 3833129 • Letter: H
Question
Hello everyone! I'm having a hard time understanding this homework and I can't contact my teacher right now. He said that we should use this program as a base to create a "Shoe inventory program" that should save to, and edit a text file called inventory.txt that should look something like this
Again, I'm not 100% certain on the details, but I think the main idea is that it is an editable inventory for products. Thank you for any help you can give!
##
# This program builds the index of a book from terms and page numbers.
#
def main() :
# Create an empty dictionary.
indexEntries = {}
# Extract the data from the text file.
infile = open("indexdata.txt", "r")
fields = extractRecord(infile)
while len(fields) > 0 :
addWord(indexEntries, fields[1], fields[0])
fields = extractRecord(infile)
infile.close()
# Print the index listing.
printIndex(indexEntries)
## Extract a single record from the input file.
# @param infile the input file object
# @return a list containing the page number and term or an empty list if
# the end of file was reached
#
def extractRecord(infile) :
line = infile.readline()
if line != "" :
fields = line.split(":")
page = int(fields[0])
term = fields[1].rstrip()
return [page, term]
else :
return []
## Add a word and its page number to the index.
# @param entries the dictionary of index entries
# @param term the term to be added to the index
# @param page the page number for this occurrence of the term
#
def addWord(entries, term, page) :
# If the term is already in the dictionary, add the page to the set.
if term in entries :
pageSet = entries[term]
pageSet.add(page)
# Otherwise, create a new set that contains the page and add an entry.
else :
pageSet = set([page])
entries[term] = pageSet
return entries
## Print the index listing.
# @param entries a dictionary containing the entries of the index
#
def printIndex(entries) :
for key in sorted(entries) :
print(key, end=" ")
pageSet = entries[key]
first = True
for page in sorted(pageSet) :
if first :
print(page, end="")
first = False
else :
print(",", page, end="")
print()
# Start the program.
main()
##>>>
##example 7, 10
##index 7
##program 7, 11
##set 20
##type 6, 8
##>>>
Explanation / Answer
def main() :
# Create an empty dictionary.
indexEntries = {}
# Extract the data from the text file.
infile = open("indexdata.txt", "r")
fields = extractRecord(infile)
while len(fields) > 0 :
addWord(indexEntries, fields[1], fields[0])
fields = extractRecord(infile)
infile.close()
# Print the index listing.
printIndex(indexEntries)
## Extract a single record from the input file.
# @param infile the input file object
# @return a list containing the page number and term or an empty list if
# the end of file was reached
#
def extractRecord(infile) :
line = infile.readline()
if line != "" :
fields = line.split(":")
page = int(fields[0])
term = fields[1].rstrip()
return [page, term]
else :
return []
def printIndex(entries) :
for key in sorted(entries) :
print(key, end=" ")
pageSet = entries[key]
first = True
for page in sorted(pageSet) :
if first :
print(page, end="")
first = False
else :
print(",", page, end="")
print()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.