SlideShare a Scribd company logo
1 of 2
Part B: Programming with Dictionaries and Sets Consider the file named “lab11b.py”. That file
contains the skeleton of a Python program to display information about the words in a document.
Function “main” is complete. It handles the interaction with the user and calls other functions to
perform the appropriate tasks. Function “print_word_index” is complete. It receives a dictionary,
where each element is a word and aset of line numbers where that word appears in a document. It
displays all of the words (in alphabeticorder), along with the lines numbers for each word (in
ascending order). Function “build_word_index” is incomplete. It receives an input file and builds
a dictionary containing the unique words which appear in the input file, along with the line
numbers where each word appears. The first line of the input file should be considered to be line
1. a. Revise function “build_word_index” to accomplish the specified work. You may wish to
review function “build_word_set” (above) for ideas about how to handle upper and lower case
letters, as well as punctuation. b. Test the revised program using the sample documents.
============================= You can see the whole part of question at
(http://www.cse.msu.edu/~cse231/Labs/Lab11/lab11.pdf) and I only need the answer of Part B.
You can see more detail at (http://www.cse.msu.edu/~cse231/Labs/Lab11/)
Solution
import string def build_word_index( input_file ): word_map = {} line_no = 0 for i, line in
enumerate(input_file): line_no+=1 word_lst = line.strip().split() word_lst =
[w.lower().strip(string.punctuation) for w in word_lst] for word in word_lst: if word in
word_map: word_map[word].add(line_no) elif word!="": word_map[word]={line_no} return
word_map def print_word_index( word_map ): index_lst = sorted(list(word_map.items())) for
word, line_set in index_lst: line_lst = sorted(list(line_set)) line_str = str( line_lst[0] ) for line_no
in line_lst[1:]: line_str += ", {}".format( line_no ) print("{:14s}:".format(word), line_str ) ##
Alternative way to create the line_str ## line_str = ",".join([str(i) for i in line_lst]) def main():
filename = input( "Name of file to be processed: " ) try: file = open( filename, "r" ) index =
build_word_index( file ) print_word_index( index ) file.close() except IOError: print( "Halting -
- unable to open", filename ) main()
Part B- Programming with Dictionaries and Sets Consider the file named.docx

More Related Content

Similar to Part B- Programming with Dictionaries and Sets Consider the file named.docx

C programming languag for cse students
C programming languag for cse studentsC programming languag for cse students
C programming languag for cse studentsAbdur Rahim
 
Workshop presentation hands on r programming
Workshop presentation hands on r programmingWorkshop presentation hands on r programming
Workshop presentation hands on r programmingNimrita Koul
 
15 functional programming
15 functional programming15 functional programming
15 functional programmingjigeno
 
15 functional programming
15 functional programming15 functional programming
15 functional programmingjigeno
 
Bca3020– data base management system(dbms)
Bca3020– data base management system(dbms)Bca3020– data base management system(dbms)
Bca3020– data base management system(dbms)smumbahelp
 
Data Analysis with R (combined slides)
Data Analysis with R (combined slides)Data Analysis with R (combined slides)
Data Analysis with R (combined slides)Guy Lebanon
 
Functions in Python Syntax and working .
Functions in Python Syntax and working .Functions in Python Syntax and working .
Functions in Python Syntax and working .tarunsharmaug23
 
Functional Programming with JavaScript
Functional Programming with JavaScriptFunctional Programming with JavaScript
Functional Programming with JavaScriptWebF
 
PyCon US 2012 - State of WSGI 2
PyCon US 2012 - State of WSGI 2PyCon US 2012 - State of WSGI 2
PyCon US 2012 - State of WSGI 2Graham Dumpleton
 
Basics of Functional Programming
Basics of Functional ProgrammingBasics of Functional Programming
Basics of Functional ProgrammingSartaj Singh
 
Financial modeling sameh aljabli lecture 6
Financial modeling   sameh aljabli   lecture 6Financial modeling   sameh aljabli   lecture 6
Financial modeling sameh aljabli lecture 6Sameh Algabli
 

Similar to Part B- Programming with Dictionaries and Sets Consider the file named.docx (20)

C programming languag for cse students
C programming languag for cse studentsC programming languag for cse students
C programming languag for cse students
 
Workshop presentation hands on r programming
Workshop presentation hands on r programmingWorkshop presentation hands on r programming
Workshop presentation hands on r programming
 
15 functional programming
15 functional programming15 functional programming
15 functional programming
 
15 functional programming
15 functional programming15 functional programming
15 functional programming
 
Bca3020– data base management system(dbms)
Bca3020– data base management system(dbms)Bca3020– data base management system(dbms)
Bca3020– data base management system(dbms)
 
Bcsl 031 solve assignment
Bcsl 031 solve assignmentBcsl 031 solve assignment
Bcsl 031 solve assignment
 
Data Analysis with R (combined slides)
Data Analysis with R (combined slides)Data Analysis with R (combined slides)
Data Analysis with R (combined slides)
 
Functions in Python Syntax and working .
Functions in Python Syntax and working .Functions in Python Syntax and working .
Functions in Python Syntax and working .
 
User defined functions in matlab
User defined functions in  matlabUser defined functions in  matlab
User defined functions in matlab
 
Functional Programming with JavaScript
Functional Programming with JavaScriptFunctional Programming with JavaScript
Functional Programming with JavaScript
 
PyCon US 2012 - State of WSGI 2
PyCon US 2012 - State of WSGI 2PyCon US 2012 - State of WSGI 2
PyCon US 2012 - State of WSGI 2
 
Basics of Functional Programming
Basics of Functional ProgrammingBasics of Functional Programming
Basics of Functional Programming
 
Financial modeling sameh aljabli lecture 6
Financial modeling   sameh aljabli   lecture 6Financial modeling   sameh aljabli   lecture 6
Financial modeling sameh aljabli lecture 6
 
Better graphics in R
Better graphics in RBetter graphics in R
Better graphics in R
 
Matlab OOP
Matlab OOPMatlab OOP
Matlab OOP
 
Python advance
Python advancePython advance
Python advance
 
Introduction to R for beginners
Introduction to R for beginnersIntroduction to R for beginners
Introduction to R for beginners
 
Python - Lecture 12
Python - Lecture 12Python - Lecture 12
Python - Lecture 12
 
lab4_php
lab4_phplab4_php
lab4_php
 
lab4_php
lab4_phplab4_php
lab4_php
 

More from rtodd19

Please identify hardware device(s) (peripherals) and describe how a NO.docx
Please identify hardware device(s) (peripherals) and describe how a NO.docxPlease identify hardware device(s) (peripherals) and describe how a NO.docx
Please identify hardware device(s) (peripherals) and describe how a NO.docxrtodd19
 
please list two of the devices provided by the text (or other devices-.docx
please list two of the devices provided by the text (or other devices-.docxplease list two of the devices provided by the text (or other devices-.docx
please list two of the devices provided by the text (or other devices-.docxrtodd19
 
Please I need this answered with refrences - Thanks In a database- to.docx
Please I need this answered with refrences - Thanks In a database- to.docxPlease I need this answered with refrences - Thanks In a database- to.docx
Please I need this answered with refrences - Thanks In a database- to.docxrtodd19
 
Please help- thanks- Many government agencies and other organizations.docx
Please help- thanks- Many government agencies and other organizations.docxPlease help- thanks- Many government agencies and other organizations.docx
Please help- thanks- Many government agencies and other organizations.docxrtodd19
 
Please explain Advanced Persistent Threat!SolutionAnswer- An advanced.docx
Please explain Advanced Persistent Threat!SolutionAnswer- An advanced.docxPlease explain Advanced Persistent Threat!SolutionAnswer- An advanced.docx
Please explain Advanced Persistent Threat!SolutionAnswer- An advanced.docxrtodd19
 
Please explain--- a) why SO2 (g) and H2O (l) formed instead of H2 (g)-.docx
Please explain--- a) why SO2 (g) and H2O (l) formed instead of H2 (g)-.docxPlease explain--- a) why SO2 (g) and H2O (l) formed instead of H2 (g)-.docx
Please explain--- a) why SO2 (g) and H2O (l) formed instead of H2 (g)-.docxrtodd19
 
Please don-'t copy from an outside source Briefly discuss the distribu.docx
Please don-'t copy from an outside source Briefly discuss the distribu.docxPlease don-'t copy from an outside source Briefly discuss the distribu.docx
Please don-'t copy from an outside source Briefly discuss the distribu.docxrtodd19
 
Please distinguish between a malware and a virus- How are they same an.docx
Please distinguish between a malware and a virus- How are they same an.docxPlease distinguish between a malware and a virus- How are they same an.docx
Please distinguish between a malware and a virus- How are they same an.docxrtodd19
 
Please answer part c of the question- Thank you- The International Ato.docx
Please answer part c of the question- Thank you- The International Ato.docxPlease answer part c of the question- Thank you- The International Ato.docx
Please answer part c of the question- Thank you- The International Ato.docxrtodd19
 
Pictorialize a human being as a computer system-SolutionThe human nerv.docx
Pictorialize a human being as a computer system-SolutionThe human nerv.docxPictorialize a human being as a computer system-SolutionThe human nerv.docx
Pictorialize a human being as a computer system-SolutionThe human nerv.docxrtodd19
 
Personal Trainer- Inc- owns and operates fitness centers in a dozen Mi.docx
Personal Trainer- Inc- owns and operates fitness centers in a dozen Mi.docxPersonal Trainer- Inc- owns and operates fitness centers in a dozen Mi.docx
Personal Trainer- Inc- owns and operates fitness centers in a dozen Mi.docxrtodd19
 
Peruse the readings by Bennis and Nanus- Schein- and Ulrich and Lake-.docx
Peruse the readings by Bennis and Nanus- Schein- and Ulrich and Lake-.docxPeruse the readings by Bennis and Nanus- Schein- and Ulrich and Lake-.docx
Peruse the readings by Bennis and Nanus- Schein- and Ulrich and Lake-.docxrtodd19
 
Why do some authors view investment cash flow sensitivity as a symptom.docx
Why do some authors view investment cash flow sensitivity as a symptom.docxWhy do some authors view investment cash flow sensitivity as a symptom.docx
Why do some authors view investment cash flow sensitivity as a symptom.docxrtodd19
 
Why do some converging technologies raise special ethical and social i.docx
Why do some converging technologies raise special ethical and social i.docxWhy do some converging technologies raise special ethical and social i.docx
Why do some converging technologies raise special ethical and social i.docxrtodd19
 
Why do the outer stars in our galaxy not slow down- as Keplers laws of.docx
Why do the outer stars in our galaxy not slow down- as Keplers laws of.docxWhy do the outer stars in our galaxy not slow down- as Keplers laws of.docx
Why do the outer stars in our galaxy not slow down- as Keplers laws of.docxrtodd19
 
Why are concentrated interests and diffuse costs important in predicti.docx
Why are concentrated interests and diffuse costs important in predicti.docxWhy are concentrated interests and diffuse costs important in predicti.docx
Why are concentrated interests and diffuse costs important in predicti.docxrtodd19
 
Who are organizational stakeholders and what types of responsibilities.docx
Who are organizational stakeholders and what types of responsibilities.docxWho are organizational stakeholders and what types of responsibilities.docx
Who are organizational stakeholders and what types of responsibilities.docxrtodd19
 
Who are the principle stake holders of the company- Did Dole treat the.docx
Who are the principle stake holders of the company- Did Dole treat the.docxWho are the principle stake holders of the company- Did Dole treat the.docx
Who are the principle stake holders of the company- Did Dole treat the.docxrtodd19
 
Which type of network covers a contiguous geographic area- such as an.docx
Which type of network covers a contiguous geographic area- such as an.docxWhich type of network covers a contiguous geographic area- such as an.docx
Which type of network covers a contiguous geographic area- such as an.docxrtodd19
 
Which space is the most abundant in 1-0 M of wak acid HCN solution- As.docx
Which space is the most abundant in 1-0 M of wak acid HCN solution- As.docxWhich space is the most abundant in 1-0 M of wak acid HCN solution- As.docx
Which space is the most abundant in 1-0 M of wak acid HCN solution- As.docxrtodd19
 

More from rtodd19 (20)

Please identify hardware device(s) (peripherals) and describe how a NO.docx
Please identify hardware device(s) (peripherals) and describe how a NO.docxPlease identify hardware device(s) (peripherals) and describe how a NO.docx
Please identify hardware device(s) (peripherals) and describe how a NO.docx
 
please list two of the devices provided by the text (or other devices-.docx
please list two of the devices provided by the text (or other devices-.docxplease list two of the devices provided by the text (or other devices-.docx
please list two of the devices provided by the text (or other devices-.docx
 
Please I need this answered with refrences - Thanks In a database- to.docx
Please I need this answered with refrences - Thanks In a database- to.docxPlease I need this answered with refrences - Thanks In a database- to.docx
Please I need this answered with refrences - Thanks In a database- to.docx
 
Please help- thanks- Many government agencies and other organizations.docx
Please help- thanks- Many government agencies and other organizations.docxPlease help- thanks- Many government agencies and other organizations.docx
Please help- thanks- Many government agencies and other organizations.docx
 
Please explain Advanced Persistent Threat!SolutionAnswer- An advanced.docx
Please explain Advanced Persistent Threat!SolutionAnswer- An advanced.docxPlease explain Advanced Persistent Threat!SolutionAnswer- An advanced.docx
Please explain Advanced Persistent Threat!SolutionAnswer- An advanced.docx
 
Please explain--- a) why SO2 (g) and H2O (l) formed instead of H2 (g)-.docx
Please explain--- a) why SO2 (g) and H2O (l) formed instead of H2 (g)-.docxPlease explain--- a) why SO2 (g) and H2O (l) formed instead of H2 (g)-.docx
Please explain--- a) why SO2 (g) and H2O (l) formed instead of H2 (g)-.docx
 
Please don-'t copy from an outside source Briefly discuss the distribu.docx
Please don-'t copy from an outside source Briefly discuss the distribu.docxPlease don-'t copy from an outside source Briefly discuss the distribu.docx
Please don-'t copy from an outside source Briefly discuss the distribu.docx
 
Please distinguish between a malware and a virus- How are they same an.docx
Please distinguish between a malware and a virus- How are they same an.docxPlease distinguish between a malware and a virus- How are they same an.docx
Please distinguish between a malware and a virus- How are they same an.docx
 
Please answer part c of the question- Thank you- The International Ato.docx
Please answer part c of the question- Thank you- The International Ato.docxPlease answer part c of the question- Thank you- The International Ato.docx
Please answer part c of the question- Thank you- The International Ato.docx
 
Pictorialize a human being as a computer system-SolutionThe human nerv.docx
Pictorialize a human being as a computer system-SolutionThe human nerv.docxPictorialize a human being as a computer system-SolutionThe human nerv.docx
Pictorialize a human being as a computer system-SolutionThe human nerv.docx
 
Personal Trainer- Inc- owns and operates fitness centers in a dozen Mi.docx
Personal Trainer- Inc- owns and operates fitness centers in a dozen Mi.docxPersonal Trainer- Inc- owns and operates fitness centers in a dozen Mi.docx
Personal Trainer- Inc- owns and operates fitness centers in a dozen Mi.docx
 
Peruse the readings by Bennis and Nanus- Schein- and Ulrich and Lake-.docx
Peruse the readings by Bennis and Nanus- Schein- and Ulrich and Lake-.docxPeruse the readings by Bennis and Nanus- Schein- and Ulrich and Lake-.docx
Peruse the readings by Bennis and Nanus- Schein- and Ulrich and Lake-.docx
 
Why do some authors view investment cash flow sensitivity as a symptom.docx
Why do some authors view investment cash flow sensitivity as a symptom.docxWhy do some authors view investment cash flow sensitivity as a symptom.docx
Why do some authors view investment cash flow sensitivity as a symptom.docx
 
Why do some converging technologies raise special ethical and social i.docx
Why do some converging technologies raise special ethical and social i.docxWhy do some converging technologies raise special ethical and social i.docx
Why do some converging technologies raise special ethical and social i.docx
 
Why do the outer stars in our galaxy not slow down- as Keplers laws of.docx
Why do the outer stars in our galaxy not slow down- as Keplers laws of.docxWhy do the outer stars in our galaxy not slow down- as Keplers laws of.docx
Why do the outer stars in our galaxy not slow down- as Keplers laws of.docx
 
Why are concentrated interests and diffuse costs important in predicti.docx
Why are concentrated interests and diffuse costs important in predicti.docxWhy are concentrated interests and diffuse costs important in predicti.docx
Why are concentrated interests and diffuse costs important in predicti.docx
 
Who are organizational stakeholders and what types of responsibilities.docx
Who are organizational stakeholders and what types of responsibilities.docxWho are organizational stakeholders and what types of responsibilities.docx
Who are organizational stakeholders and what types of responsibilities.docx
 
Who are the principle stake holders of the company- Did Dole treat the.docx
Who are the principle stake holders of the company- Did Dole treat the.docxWho are the principle stake holders of the company- Did Dole treat the.docx
Who are the principle stake holders of the company- Did Dole treat the.docx
 
Which type of network covers a contiguous geographic area- such as an.docx
Which type of network covers a contiguous geographic area- such as an.docxWhich type of network covers a contiguous geographic area- such as an.docx
Which type of network covers a contiguous geographic area- such as an.docx
 
Which space is the most abundant in 1-0 M of wak acid HCN solution- As.docx
Which space is the most abundant in 1-0 M of wak acid HCN solution- As.docxWhich space is the most abundant in 1-0 M of wak acid HCN solution- As.docx
Which space is the most abundant in 1-0 M of wak acid HCN solution- As.docx
 

Recently uploaded

Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 

Recently uploaded (20)

Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 

Part B- Programming with Dictionaries and Sets Consider the file named.docx

  • 1. Part B: Programming with Dictionaries and Sets Consider the file named “lab11b.py”. That file contains the skeleton of a Python program to display information about the words in a document. Function “main” is complete. It handles the interaction with the user and calls other functions to perform the appropriate tasks. Function “print_word_index” is complete. It receives a dictionary, where each element is a word and aset of line numbers where that word appears in a document. It displays all of the words (in alphabeticorder), along with the lines numbers for each word (in ascending order). Function “build_word_index” is incomplete. It receives an input file and builds a dictionary containing the unique words which appear in the input file, along with the line numbers where each word appears. The first line of the input file should be considered to be line 1. a. Revise function “build_word_index” to accomplish the specified work. You may wish to review function “build_word_set” (above) for ideas about how to handle upper and lower case letters, as well as punctuation. b. Test the revised program using the sample documents. ============================= You can see the whole part of question at (http://www.cse.msu.edu/~cse231/Labs/Lab11/lab11.pdf) and I only need the answer of Part B. You can see more detail at (http://www.cse.msu.edu/~cse231/Labs/Lab11/) Solution import string def build_word_index( input_file ): word_map = {} line_no = 0 for i, line in enumerate(input_file): line_no+=1 word_lst = line.strip().split() word_lst = [w.lower().strip(string.punctuation) for w in word_lst] for word in word_lst: if word in word_map: word_map[word].add(line_no) elif word!="": word_map[word]={line_no} return word_map def print_word_index( word_map ): index_lst = sorted(list(word_map.items())) for word, line_set in index_lst: line_lst = sorted(list(line_set)) line_str = str( line_lst[0] ) for line_no in line_lst[1:]: line_str += ", {}".format( line_no ) print("{:14s}:".format(word), line_str ) ## Alternative way to create the line_str ## line_str = ",".join([str(i) for i in line_lst]) def main(): filename = input( "Name of file to be processed: " ) try: file = open( filename, "r" ) index = build_word_index( file ) print_word_index( index ) file.close() except IOError: print( "Halting - - unable to open", filename ) main()