SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Ähnlich wie 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
 

Ähnlich wie 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
 

Mehr von 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
 

Mehr von 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
 

Kürzlich hochgeladen

Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesVijayaLaxmi84
 
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxCLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxAnupam32727
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...DhatriParmar
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxSayali Powar
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdfMr Bounab Samir
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17Celine George
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxkarenfajardo43
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...DhatriParmar
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Developmentchesterberbo7
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
CHEST Proprioceptive neuromuscular facilitation.pptx
CHEST Proprioceptive neuromuscular facilitation.pptxCHEST Proprioceptive neuromuscular facilitation.pptx
CHEST Proprioceptive neuromuscular facilitation.pptxAneriPatwari
 

Kürzlich hochgeladen (20)

prashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Professionprashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Profession
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their uses
 
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxCLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdf
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Development
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
CHEST Proprioceptive neuromuscular facilitation.pptx
CHEST Proprioceptive neuromuscular facilitation.pptxCHEST Proprioceptive neuromuscular facilitation.pptx
CHEST Proprioceptive neuromuscular facilitation.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()