SlideShare a Scribd company logo
1 of 21
For any help regarding Python Homework Help
Visit : https://www.programminghomeworkhelp.com/, Email
: support@programminghomeworkhelp.com or
call us at - +1 678 648 4277
programminghomeworkhelp.com
Quick Reference
D = { } – creates an empty dictionary
D = {key1:value1, …} – creates a non-empty dictionary
D[key] – returns the value that’s mapped to by key. (What if there’s no such key?)
D[key] = newvalue – maps newvalue to key. Overwrites any previous value. del D[key]
– deletes the mapping with that key from D.
len(D) – returns the number of entries (mappings) in D. x in D, x not in D – checks
whether the key x is in the dictionary D.
D.items( ) – returns the entries as a list of (key, value) tuples. for k in D – iterates over
all of the keys in D.
Problem 1 – Inventory Finder
Download the inventory.py file. The file shows eight different items, each having a
name, a price and a count, like so:
HAMMER = “hammer”
HAMMER_PRICE = 10
HAMMER_COUNT = 100
Problems
programminghomeworkhelp.com
We’re going to consider that customers generally come in with an idea of how much
money they want to spend. So we’re going to think of items as either CHEAP (under
$20), MODERATE (between $20 and $100) or EXPENSIVE (over $100).
First, fill in the variable inventory so that all of the data for the eight items is inside
inventory. Make sure that you maintain the notion of CHEAP, MODERATE and
EXPENSIVE. Then, implement the function get_info that takes a cheapness and returns
a list of information about each item that falls under that category, as the function’s
information says.
Important: there should NOT be a loop inside this function. Our inventory is small, but
for a giant store, the inventory will be big. The store can’t afford to waste time looping
over all of the inventory every time a customer has a request.
When you’re finished, just run the program. All of the testing lines should print True.
Problem 2 – Indexing the Web, Part 2
So we have a working search engine. That’s great! But how do we know which sites are
better than others? Right now, they’re just returning the sites in an arbitrary order
(remember, dictionaries are unordered). In this problem, we’ll implement a ranking
system.
programminghomeworkhelp.com
What will we rank based on? Google used an innovative ranking system that ranked a
page higher if more *other* pages linked to it. We can’t do that unfortunately, because
that requires a considerable understanding of graph theory, so what else can we do? Well,
before Google, most engines ranked based on either the frequency (i.e. number of hits)
of search terms inside the page, or by the percentage of those search terms within the
page’s text. We’ll go with the frequency arbitrarily – we found after Google that neither of
these measures are particularly good, and there isn’t a clear advantage between the two.
To begin, download the following files:
webindexer2.py – this is the file in which you’ll write all of your code.
websearch2.py – this completed program is an updated search engine that will use your
new index with a ranking system.
Again, take a look at the main program, websearch2.py. It’s almost identical to the
previous version, but you can see that it now expects to have tuples of (site, frequency)
rather than just the sites themselves. This way, it is able to display how many hits each
site has. It also expects that the sites are already sorted/ranked from highest to lowest
frequency.
So let’s take a look at webindexer2.py. Again, it’s almost identical to the previous version,
but the descriptions for the search functions now state that frequencyis returned along
with each site, and the sites are sorted by rank.
programminghomeworkhelp.com
In order to rank each site by the frequency of search terms in it, we’ll have to store the
information in our index.
To begin, you can copy your functions’ code from webindexer1.py into webindexer2.py,
but you don’t have to.
Task 1 – Implement the index_site function. What information will each site in the index
need to store with it? What’s the best way to store this information? If we have more than
one choice, which choice is mutable, and which one is immutable? While we’re building
the index, we’ll be repeatedly making changes, so which choice is better?
Hints: If you’re stuck, think very logically. When I’m searching, I have a word. I want to be
able to look up this word and get what information? The information needs to be enough
for me to sort it.
Now that we’ve taken care of indexing, we can again move on to searching. And again, we’ll
tackle one word first before multiple words. This should be very similar to your previous
function, but we have to do one additional thing: sort the results based on frequency.
Task 2 – Implement the search_single_word function. We have to return a list of (site,
frequency) tuples. If we have a list L of these tuples, to sort them, do this:
L.sort(key = lambda pair: pair[1], reverse = True)
programminghomeworkhelp.com
Don’t worry about what this means yet, but if you’re interested, we can explain.
And again, now that we can handle one word, we’ll handle multiple words. The same
logic applies as before, but again, we have to sort the results before returning them.
Task 3 – Implement the search_multiple_words function. The argument words is a list,
not a string. Make sure you don’t return duplicate sites in your list! And as before, make
sure you sort the list (using the same statement as above).
You should now have a working indexer with a ranking system, so run websearch2.py
and try it out! And for some real fun, don’t use the smallest set of files. Use the 20 set
or the 50 set to see the ranking really come into play.
As before, on the next page, I’ve pasted my output for a few searches from the
mitsites20.txt file. If your output is quite different, you may have done something wrong.
If it’s just slightly different, it may just be a change in the pages (e.g. web.mit.edu) from
when I indexed the site to when you did.
programminghomeworkhelp.com
# inventory.py
# Stub file for lab 10, problem 1
#
# 6.189 - Intro to Python
# IAP 2008 - Class 8
CHEAP = "cheap" # less than $20
MODERATE = "moderate" # between $20 and $100
EXPENSIVE = "expensive" # more than $100
HAMMER = "hammer"
HAMMER_PRICE = 10
HAMMER_COUNT = 100
SCREW = "screw"
SCREW_PRICE = 1
SCREW_COUNT = 1000
NAIL = "nail"
NAIL_PRICE = 1
NAIL_COUNT = 1000
programminghomeworkhelp.com
SCREWDRIVER = "screwdriver"
SCREWDRIVER_PRICE = 8
SCREWDRIVER_COUNT = 100
DRILL = "drill"
DRILL_PRICE = 50
DRILL_COUNT = 20
WORKBENCH = "workbench"
WORKBENCH_PRICE = 150
WORKBENCH_COUNT = 5
HANDSAW = "handsaw"
HANDSAW_PRICE = 15
HANDSAW_COUNT = 50
CHAINSAW = "chainsaw"
CHAINSAW_PRICE = 80
CHAINSAW_COUNT = 30
# You should put all of the stuff above logically into this dictionary.
# You can just put it all in right here, like shown.
# Try to use only one *variable*, called inventory here.
programminghomeworkhelp.com
inventory = { # key1 : value1, note how I can continue on the next line,
# key2 : value2, I don't need a backslash or anything.
# key3 : value3
}
def get_items(cheapness):
""" Return a list of (item, (price, count) tuples that are the given
cheapness. Note that the second element of the tuple is another tuple. """
# your code here
return [] # delete this
# Testing
cheap = get_items(CHEAP)
print type(cheap) is list
print len(cheap) == 5
print (HAMMER, (HAMMER_PRICE, HAMMER_COUNT)) in cheap
print (NAIL, (NAIL_PRICE, NAIL_COUNT)) in cheap
print (SCREW, (SCREW_PRICE, SCREW_COUNT)) in cheap
print (SCREWDRIVER, (SCREWDRIVER_PRICE, SCREWDRIVER_COUNT)) in cheap
print (HANDSAW, (HANDSAW_PRICE, HANDSAW_COUNT)) in cheap
programminghomeworkhelp.com
moderate = get_items(MODERATE)
print type(moderate) is list
print len(moderate) == 2
print (DRILL, (DRILL_PRICE, DRILL_COUNT)) in moderate
print (CHAINSAW, (CHAINSAW_PRICE, CHAINSAW_COUNT)) in moderate
expensive = get_items(EXPENSIVE)
print type(expensive) is list
print len(expensive) == 1
print (WORKBENCH, (WORKBENCH_PRICE, WORKBENCH_COUNT)) in expensive
# webindexer2.py
# Stub file for lab 10, problem 2
#
# 6.189 - Intro to Python
# IAP 2008 - Class 8
from urllib import urlopen
from htmltext import HtmlTextParser
FILENAME = "smallsites.txt" programminghomeworkhelp.com
index = {}
def get_sites():
""" Return all the sites that are in FILENAME. """
sites_file = open(FILENAME)
sites = []
for site in sites_file:
sites.append("http://" + site.strip())
return sites
def read_site(site):
""" Attempt to read the given site. Return the text of the site if
successful, otherwise returns False. """
try:
connection = urlopen(site)
html = connection.read()
connection.close()
except:
return False
parser = HtmlTextParser()
parser.parse(html)
programminghomeworkhelp.com
return parser.get_text()
def index_site(site, text):
""" Index the given site with the given text. """
# YOUR CODE HERE #
pass # delete this when you write your code
def search_single_word(word):
""" Return a list of (site, frequency) tuples for all sites containing the
given word, sorted by decreasing frequency. """
# YOUR CODE HERE #
pass # delete this when you write your code
def search_multiple_words(words):
""" Return a list of (site, frequency) tuples for all sites containing any
of the given words, sorted by decreasing frequency. """
# YOUR CODE HERE #
pass # delete this when you write your code
def build_index():
""" Build the index by reading and indexing each site. """
programminghomeworkhelp.com
for site in get_sites():
text = read_site(site)
while text == False:
text = read_site(site) # keep attempting to read until successful
index_site(site, text)
programminghomeworkhelp.com
Solutions
# inventory.py
# Stub file for lab 10, problem 1
#
# 6.189 - Intro to Python
# IAP 2008 - Class 8
CHEAP = "cheap" # less than $20
MODERATE = "moderate" # between $20 and $100
EXPENSIVE = "expensive" # more than $100
HAMMER = "hammer"
HAMMER_PRICE = 10
HAMMER_COUNT = 100
SCREW = "screw"
SCREW_PRICE = 1
SCREW_COUNT = 1000
NAIL = "nail"
NAIL_PRICE = 1
NAIL_COUNT = 1000 programminghomeworkhelp.com
SCREWDRIVER = "screwdriver"
SCREWDRIVER_PRICE = 8
SCREWDRIVER_COUNT = 100
DRILL = "drill"
DRILL_PRICE = 50
DRILL_COUNT = 20
WORKBENCH = "workbench"
WORKBENCH_PRICE = 150
WORKBENCH_COUNT = 5
HANDSAW = "handsaw"
HANDSAW_PRICE = 15
HANDSAW_COUNT = 50
CHAINSAW = "chainsaw"
CHAINSAW_PRICE = 80
CHAINSAW_COUNT = 30
# You should put the stuff logically into this dictionary.
# You can just put it all in right here, like shown.
# Try to use only one *variable*, called inventory here.
programminghomeworkhelp.com
inventory = {
CHEAP : {
HAMMER : (HAMMER_PRICE, HAMMER_COUNT),
NAIL : (NAIL_PRICE, NAIL_COUNT),
SCREW : (SCREW_PRICE, SCREW_COUNT),
SCREWDRIVER : (SCREWDRIVER_PRICE, SCREWDRIVER_COUNT),
HANDSAW : (HANDSAW_PRICE, HANDSAW_COUNT)
},
MODERATE : {
DRILL : (DRILL_PRICE, DRILL_COUNT),
CHAINSAW : (CHAINSAW_PRICE, CHAINSAW_COUNT)
},
EXPENSIVE : {
WORKBENCH : (WORKBENCH_PRICE, WORKBENCH_COUNT)
}
}
def get_items(cheapness):
""" Return a list of (item, (price, count)) tuples that are the given
cheapness. Note that the second element of the tuple is another tuple. """
return inventory[cheapness].items()
programminghomeworkhelp.com
# Testing
cheap = get_items(CHEAP)
print type(cheap) is list
print len(cheap) == 5
print (HAMMER, (HAMMER_PRICE, HAMMER_COUNT)) in cheap
print (NAIL, (NAIL_PRICE, NAIL_COUNT)) in cheap
print (SCREW, (SCREW_PRICE, SCREW_COUNT)) in cheap
print (SCREWDRIVER, (SCREWDRIVER_PRICE, SCREWDRIVER_COUNT)) in cheap
print (HANDSAW, (HANDSAW_PRICE, HANDSAW_COUNT)) in cheap
moderate = get_items(MODERATE)
print type(moderate) is list
print len(moderate) == 2
print (DRILL, (DRILL_PRICE, DRILL_COUNT)) in moderate
print (CHAINSAW, (CHAINSAW_PRICE, CHAINSAW_COUNT)) in moderate
expensive = get_items(EXPENSIVE)
print type(expensive) is list
print len(expensive) == 1
print (WORKBENCH, (WORKBENCH_PRICE, WORKBENCH_COUNT)) in expensive
programminghomeworkhelp.com
# webindexer2.py
# Stub file for lab 10, problem 2
#
# 6.189 - Intro to Python
# IAP 2008 - Class 8
from urllib import urlopen
from htmltext import HtmlTextParser
FILENAME = "smallsites.txt"
index = {}
def get_sites():
""" Return all the sites that are in FILENAME. """
sites_file = open(FILENAME)
sites = []
for site in sites_file:
sites.append("http://" + site.strip())
return sites
programminghomeworkhelp.com
def read_site(site):
""" Attempt to read the given site. Return the text of the site if
successful, otherwise returns False. """
try:
connection = urlopen(site)
html = connection.read()
connection.close()
except:
return False
parser = HtmlTextParser()
parser.parse(html)
return parser.get_text()
def index_site(site, text):
""" Index the given site with the given text. """
words = text.lower().split()
for word in words:
if word not in index: # case 1: haven't seen word anywhere
index[word] = {site:1} # make a new entry for the word
elif site not in index[word]: # case 2: haven't seen word on
programminghomeworkhelp.com
this site
index[word][site] = 1 # make a new entry for this site
else: # case 3: seen this word on this site
index[word][site] += 1 # increment the frequency by 1
def search_single_word(word):
""" Return a list of (site, frequency) tuples for all sites containing the
given word, sorted by decreasing frequency. """
if word not in index:
return []
L = index[word].items()
L.sort(key = lambda pair: pair[1], reverse = True)
return L
def search_multiple_words(words):
""" Return a list of (site, frequency) tuples for all sites containing any
of the given words, sorted by decreasing frequency. """
all_sites = {}
for word in words:
for site, freq in search_single_word(word):
if site not in all_sites: # case 1: haven't included this site
all_sites[site] = freq # make a new entry for site, freq
programminghomeworkhelp.com
else: # case 2: have included this site
all_sites[site] += freq # add the frequencies
L = all_sites.items()
L.sort(key = lambda pair: pair[1], reverse = True)
return L
def build_index():
""" Build the index by reading and indexing each site. """
for site in get_sites():
text = read_site(site)
while text == False:
text = read_site(site) # keep attempting to read until successful
index_site(site, text)
programminghomeworkhelp.com

More Related Content

What's hot

CS225_Prelecture_Notes 2nd
CS225_Prelecture_Notes 2ndCS225_Prelecture_Notes 2nd
CS225_Prelecture_Notes 2nd
Edward Chen
 

What's hot (20)

Computer Science Homework Help
Computer Science Homework HelpComputer Science Homework Help
Computer Science Homework Help
 
Phyton Learning extracts
Phyton Learning extracts Phyton Learning extracts
Phyton Learning extracts
 
Introduction to Python - Training for Kids
Introduction to Python - Training for KidsIntroduction to Python - Training for Kids
Introduction to Python - Training for Kids
 
python.ppt
python.pptpython.ppt
python.ppt
 
Functions
FunctionsFunctions
Functions
 
Python Programming Essentials - M16 - Control Flow Statements and Loops
Python Programming Essentials - M16 - Control Flow Statements and LoopsPython Programming Essentials - M16 - Control Flow Statements and Loops
Python Programming Essentials - M16 - Control Flow Statements and Loops
 
BayFP: Concurrent and Multicore Haskell
BayFP: Concurrent and Multicore HaskellBayFP: Concurrent and Multicore Haskell
BayFP: Concurrent and Multicore Haskell
 
Currying and Partial Function Application (PFA)
Currying and Partial Function Application (PFA)Currying and Partial Function Application (PFA)
Currying and Partial Function Application (PFA)
 
Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)
 
python Function
python Function python Function
python Function
 
Python programming
Python  programmingPython  programming
Python programming
 
Lazy java
Lazy javaLazy java
Lazy java
 
Python language data types
Python language data typesPython language data types
Python language data types
 
Python programming Workshop SITTTR - Kalamassery
Python programming Workshop SITTTR - KalamasseryPython programming Workshop SITTTR - Kalamassery
Python programming Workshop SITTTR - Kalamassery
 
Introduction to Python - Part Three
Introduction to Python - Part ThreeIntroduction to Python - Part Three
Introduction to Python - Part Three
 
Learn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesLearn 90% of Python in 90 Minutes
Learn 90% of Python in 90 Minutes
 
1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers
 
CS225_Prelecture_Notes 2nd
CS225_Prelecture_Notes 2ndCS225_Prelecture_Notes 2nd
CS225_Prelecture_Notes 2nd
 
JavaScript Functions
JavaScript FunctionsJavaScript Functions
JavaScript Functions
 
Python Basics
Python BasicsPython Basics
Python Basics
 

Similar to Python Homework Help

Scanned by CamScannerModule 03 Lab WorksheetWeb Developmen.docx
Scanned by CamScannerModule 03 Lab WorksheetWeb Developmen.docxScanned by CamScannerModule 03 Lab WorksheetWeb Developmen.docx
Scanned by CamScannerModule 03 Lab WorksheetWeb Developmen.docx
anhlodge
 
Notes5
Notes5Notes5
Notes5
hccit
 
Assg 07 Templates and Operator OverloadingCOSC 2336 Sprin.docx
Assg 07 Templates and Operator OverloadingCOSC 2336 Sprin.docxAssg 07 Templates and Operator OverloadingCOSC 2336 Sprin.docx
Assg 07 Templates and Operator OverloadingCOSC 2336 Sprin.docx
festockton
 
Real World Haskell: Lecture 1
Real World Haskell: Lecture 1Real World Haskell: Lecture 1
Real World Haskell: Lecture 1
Bryan O'Sullivan
 
Objectives Assignment 09 Applications of Stacks COS.docx
Objectives Assignment 09 Applications of Stacks COS.docxObjectives Assignment 09 Applications of Stacks COS.docx
Objectives Assignment 09 Applications of Stacks COS.docx
dunhamadell
 
Porque aprender haskell me fez um programador python melhor?
Porque aprender haskell me fez um programador python melhor?Porque aprender haskell me fez um programador python melhor?
Porque aprender haskell me fez um programador python melhor?
UFPA
 

Similar to Python Homework Help (20)

lab4_php
lab4_phplab4_php
lab4_php
 
lab4_php
lab4_phplab4_php
lab4_php
 
Arrays & functions in php
Arrays & functions in phpArrays & functions in php
Arrays & functions in php
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Scanned by CamScannerModule 03 Lab WorksheetWeb Developmen.docx
Scanned by CamScannerModule 03 Lab WorksheetWeb Developmen.docxScanned by CamScannerModule 03 Lab WorksheetWeb Developmen.docx
Scanned by CamScannerModule 03 Lab WorksheetWeb Developmen.docx
 
Notes5
Notes5Notes5
Notes5
 
Python Exam (Questions with Solutions Done By Live Exam Helper Experts)
Python Exam (Questions with Solutions Done By Live Exam Helper Experts)Python Exam (Questions with Solutions Done By Live Exam Helper Experts)
Python Exam (Questions with Solutions Done By Live Exam Helper Experts)
 
Assg 07 Templates and Operator OverloadingCOSC 2336 Sprin.docx
Assg 07 Templates and Operator OverloadingCOSC 2336 Sprin.docxAssg 07 Templates and Operator OverloadingCOSC 2336 Sprin.docx
Assg 07 Templates and Operator OverloadingCOSC 2336 Sprin.docx
 
Real World Haskell: Lecture 1
Real World Haskell: Lecture 1Real World Haskell: Lecture 1
Real World Haskell: Lecture 1
 
Automation Testing theory notes.pptx
Automation Testing theory notes.pptxAutomation Testing theory notes.pptx
Automation Testing theory notes.pptx
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Objectives Assignment 09 Applications of Stacks COS.docx
Objectives Assignment 09 Applications of Stacks COS.docxObjectives Assignment 09 Applications of Stacks COS.docx
Objectives Assignment 09 Applications of Stacks COS.docx
 
ProgFund_Lecture_4_Functions_and_Modules-1.pdf
ProgFund_Lecture_4_Functions_and_Modules-1.pdfProgFund_Lecture_4_Functions_and_Modules-1.pdf
ProgFund_Lecture_4_Functions_and_Modules-1.pdf
 
Playfulness at Work
Playfulness at WorkPlayfulness at Work
Playfulness at Work
 
Reproducibility with R
Reproducibility with RReproducibility with R
Reproducibility with R
 
Python cheatsheet Indian Servers
Python cheatsheet Indian ServersPython cheatsheet Indian Servers
Python cheatsheet Indian Servers
 
Dutch PHP Conference 2013: Distilled
Dutch PHP Conference 2013: DistilledDutch PHP Conference 2013: Distilled
Dutch PHP Conference 2013: Distilled
 
Testing in Django
Testing in DjangoTesting in Django
Testing in Django
 
Porque aprender haskell me fez um programador python melhor?
Porque aprender haskell me fez um programador python melhor?Porque aprender haskell me fez um programador python melhor?
Porque aprender haskell me fez um programador python melhor?
 
Amusing C#
Amusing C#Amusing C#
Amusing C#
 

More from Programming Homework Help

Python Question - Python Assignment Help
Python Question - Python Assignment HelpPython Question - Python Assignment Help
Python Question - Python Assignment Help
Programming Homework Help
 

More from Programming Homework Help (20)

C Assignment Help
C Assignment HelpC Assignment Help
C Assignment Help
 
Python Question - Python Assignment Help
Python Question - Python Assignment HelpPython Question - Python Assignment Help
Python Question - Python Assignment Help
 
Best Algorithms Assignment Help
Best Algorithms Assignment Help Best Algorithms Assignment Help
Best Algorithms Assignment Help
 
Design and Analysis of Algorithms Assignment Help
Design and Analysis of Algorithms Assignment HelpDesign and Analysis of Algorithms Assignment Help
Design and Analysis of Algorithms Assignment Help
 
Algorithm Homework Help
Algorithm Homework HelpAlgorithm Homework Help
Algorithm Homework Help
 
programminghomeworkhelp.com_Advanced Algorithms Homework Help.pptx
programminghomeworkhelp.com_Advanced Algorithms Homework Help.pptxprogramminghomeworkhelp.com_Advanced Algorithms Homework Help.pptx
programminghomeworkhelp.com_Advanced Algorithms Homework Help.pptx
 
Algorithm Homework Help
Algorithm Homework HelpAlgorithm Homework Help
Algorithm Homework Help
 
Algorithms Design Assignment Help
Algorithms Design Assignment HelpAlgorithms Design Assignment Help
Algorithms Design Assignment Help
 
Algorithms Design Homework Help
Algorithms Design Homework HelpAlgorithms Design Homework Help
Algorithms Design Homework Help
 
Algorithm Assignment Help
Algorithm Assignment HelpAlgorithm Assignment Help
Algorithm Assignment Help
 
Algorithm Homework Help
Algorithm Homework HelpAlgorithm Homework Help
Algorithm Homework Help
 
C Homework Help
C Homework HelpC Homework Help
C Homework Help
 
C Homework Help
C Homework HelpC Homework Help
C Homework Help
 
Algorithm Assignment Help
Algorithm Assignment HelpAlgorithm Assignment Help
Algorithm Assignment Help
 
Algorithm Homework Help
Algorithm Homework HelpAlgorithm Homework Help
Algorithm Homework Help
 
Computer Science Assignment Help
Computer Science Assignment Help Computer Science Assignment Help
Computer Science Assignment Help
 
Algorithm Assignment Help
Algorithm Assignment HelpAlgorithm Assignment Help
Algorithm Assignment Help
 
Computer Science Assignment Help
Computer Science Assignment HelpComputer Science Assignment Help
Computer Science Assignment Help
 
Software Construction Assignment Help
Software Construction Assignment HelpSoftware Construction Assignment Help
Software Construction Assignment Help
 
C Assignment Help
C Assignment HelpC Assignment Help
C Assignment Help
 

Recently uploaded

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
ssuserdda66b
 

Recently uploaded (20)

Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 

Python Homework Help

  • 1. For any help regarding Python Homework Help Visit : https://www.programminghomeworkhelp.com/, Email : support@programminghomeworkhelp.com or call us at - +1 678 648 4277 programminghomeworkhelp.com
  • 2. Quick Reference D = { } – creates an empty dictionary D = {key1:value1, …} – creates a non-empty dictionary D[key] – returns the value that’s mapped to by key. (What if there’s no such key?) D[key] = newvalue – maps newvalue to key. Overwrites any previous value. del D[key] – deletes the mapping with that key from D. len(D) – returns the number of entries (mappings) in D. x in D, x not in D – checks whether the key x is in the dictionary D. D.items( ) – returns the entries as a list of (key, value) tuples. for k in D – iterates over all of the keys in D. Problem 1 – Inventory Finder Download the inventory.py file. The file shows eight different items, each having a name, a price and a count, like so: HAMMER = “hammer” HAMMER_PRICE = 10 HAMMER_COUNT = 100 Problems programminghomeworkhelp.com
  • 3. We’re going to consider that customers generally come in with an idea of how much money they want to spend. So we’re going to think of items as either CHEAP (under $20), MODERATE (between $20 and $100) or EXPENSIVE (over $100). First, fill in the variable inventory so that all of the data for the eight items is inside inventory. Make sure that you maintain the notion of CHEAP, MODERATE and EXPENSIVE. Then, implement the function get_info that takes a cheapness and returns a list of information about each item that falls under that category, as the function’s information says. Important: there should NOT be a loop inside this function. Our inventory is small, but for a giant store, the inventory will be big. The store can’t afford to waste time looping over all of the inventory every time a customer has a request. When you’re finished, just run the program. All of the testing lines should print True. Problem 2 – Indexing the Web, Part 2 So we have a working search engine. That’s great! But how do we know which sites are better than others? Right now, they’re just returning the sites in an arbitrary order (remember, dictionaries are unordered). In this problem, we’ll implement a ranking system. programminghomeworkhelp.com
  • 4. What will we rank based on? Google used an innovative ranking system that ranked a page higher if more *other* pages linked to it. We can’t do that unfortunately, because that requires a considerable understanding of graph theory, so what else can we do? Well, before Google, most engines ranked based on either the frequency (i.e. number of hits) of search terms inside the page, or by the percentage of those search terms within the page’s text. We’ll go with the frequency arbitrarily – we found after Google that neither of these measures are particularly good, and there isn’t a clear advantage between the two. To begin, download the following files: webindexer2.py – this is the file in which you’ll write all of your code. websearch2.py – this completed program is an updated search engine that will use your new index with a ranking system. Again, take a look at the main program, websearch2.py. It’s almost identical to the previous version, but you can see that it now expects to have tuples of (site, frequency) rather than just the sites themselves. This way, it is able to display how many hits each site has. It also expects that the sites are already sorted/ranked from highest to lowest frequency. So let’s take a look at webindexer2.py. Again, it’s almost identical to the previous version, but the descriptions for the search functions now state that frequencyis returned along with each site, and the sites are sorted by rank. programminghomeworkhelp.com
  • 5. In order to rank each site by the frequency of search terms in it, we’ll have to store the information in our index. To begin, you can copy your functions’ code from webindexer1.py into webindexer2.py, but you don’t have to. Task 1 – Implement the index_site function. What information will each site in the index need to store with it? What’s the best way to store this information? If we have more than one choice, which choice is mutable, and which one is immutable? While we’re building the index, we’ll be repeatedly making changes, so which choice is better? Hints: If you’re stuck, think very logically. When I’m searching, I have a word. I want to be able to look up this word and get what information? The information needs to be enough for me to sort it. Now that we’ve taken care of indexing, we can again move on to searching. And again, we’ll tackle one word first before multiple words. This should be very similar to your previous function, but we have to do one additional thing: sort the results based on frequency. Task 2 – Implement the search_single_word function. We have to return a list of (site, frequency) tuples. If we have a list L of these tuples, to sort them, do this: L.sort(key = lambda pair: pair[1], reverse = True) programminghomeworkhelp.com
  • 6. Don’t worry about what this means yet, but if you’re interested, we can explain. And again, now that we can handle one word, we’ll handle multiple words. The same logic applies as before, but again, we have to sort the results before returning them. Task 3 – Implement the search_multiple_words function. The argument words is a list, not a string. Make sure you don’t return duplicate sites in your list! And as before, make sure you sort the list (using the same statement as above). You should now have a working indexer with a ranking system, so run websearch2.py and try it out! And for some real fun, don’t use the smallest set of files. Use the 20 set or the 50 set to see the ranking really come into play. As before, on the next page, I’ve pasted my output for a few searches from the mitsites20.txt file. If your output is quite different, you may have done something wrong. If it’s just slightly different, it may just be a change in the pages (e.g. web.mit.edu) from when I indexed the site to when you did. programminghomeworkhelp.com
  • 7. # inventory.py # Stub file for lab 10, problem 1 # # 6.189 - Intro to Python # IAP 2008 - Class 8 CHEAP = "cheap" # less than $20 MODERATE = "moderate" # between $20 and $100 EXPENSIVE = "expensive" # more than $100 HAMMER = "hammer" HAMMER_PRICE = 10 HAMMER_COUNT = 100 SCREW = "screw" SCREW_PRICE = 1 SCREW_COUNT = 1000 NAIL = "nail" NAIL_PRICE = 1 NAIL_COUNT = 1000 programminghomeworkhelp.com
  • 8. SCREWDRIVER = "screwdriver" SCREWDRIVER_PRICE = 8 SCREWDRIVER_COUNT = 100 DRILL = "drill" DRILL_PRICE = 50 DRILL_COUNT = 20 WORKBENCH = "workbench" WORKBENCH_PRICE = 150 WORKBENCH_COUNT = 5 HANDSAW = "handsaw" HANDSAW_PRICE = 15 HANDSAW_COUNT = 50 CHAINSAW = "chainsaw" CHAINSAW_PRICE = 80 CHAINSAW_COUNT = 30 # You should put all of the stuff above logically into this dictionary. # You can just put it all in right here, like shown. # Try to use only one *variable*, called inventory here. programminghomeworkhelp.com
  • 9. inventory = { # key1 : value1, note how I can continue on the next line, # key2 : value2, I don't need a backslash or anything. # key3 : value3 } def get_items(cheapness): """ Return a list of (item, (price, count) tuples that are the given cheapness. Note that the second element of the tuple is another tuple. """ # your code here return [] # delete this # Testing cheap = get_items(CHEAP) print type(cheap) is list print len(cheap) == 5 print (HAMMER, (HAMMER_PRICE, HAMMER_COUNT)) in cheap print (NAIL, (NAIL_PRICE, NAIL_COUNT)) in cheap print (SCREW, (SCREW_PRICE, SCREW_COUNT)) in cheap print (SCREWDRIVER, (SCREWDRIVER_PRICE, SCREWDRIVER_COUNT)) in cheap print (HANDSAW, (HANDSAW_PRICE, HANDSAW_COUNT)) in cheap programminghomeworkhelp.com
  • 10. moderate = get_items(MODERATE) print type(moderate) is list print len(moderate) == 2 print (DRILL, (DRILL_PRICE, DRILL_COUNT)) in moderate print (CHAINSAW, (CHAINSAW_PRICE, CHAINSAW_COUNT)) in moderate expensive = get_items(EXPENSIVE) print type(expensive) is list print len(expensive) == 1 print (WORKBENCH, (WORKBENCH_PRICE, WORKBENCH_COUNT)) in expensive # webindexer2.py # Stub file for lab 10, problem 2 # # 6.189 - Intro to Python # IAP 2008 - Class 8 from urllib import urlopen from htmltext import HtmlTextParser FILENAME = "smallsites.txt" programminghomeworkhelp.com
  • 11. index = {} def get_sites(): """ Return all the sites that are in FILENAME. """ sites_file = open(FILENAME) sites = [] for site in sites_file: sites.append("http://" + site.strip()) return sites def read_site(site): """ Attempt to read the given site. Return the text of the site if successful, otherwise returns False. """ try: connection = urlopen(site) html = connection.read() connection.close() except: return False parser = HtmlTextParser() parser.parse(html) programminghomeworkhelp.com
  • 12. return parser.get_text() def index_site(site, text): """ Index the given site with the given text. """ # YOUR CODE HERE # pass # delete this when you write your code def search_single_word(word): """ Return a list of (site, frequency) tuples for all sites containing the given word, sorted by decreasing frequency. """ # YOUR CODE HERE # pass # delete this when you write your code def search_multiple_words(words): """ Return a list of (site, frequency) tuples for all sites containing any of the given words, sorted by decreasing frequency. """ # YOUR CODE HERE # pass # delete this when you write your code def build_index(): """ Build the index by reading and indexing each site. """ programminghomeworkhelp.com
  • 13. for site in get_sites(): text = read_site(site) while text == False: text = read_site(site) # keep attempting to read until successful index_site(site, text) programminghomeworkhelp.com
  • 14. Solutions # inventory.py # Stub file for lab 10, problem 1 # # 6.189 - Intro to Python # IAP 2008 - Class 8 CHEAP = "cheap" # less than $20 MODERATE = "moderate" # between $20 and $100 EXPENSIVE = "expensive" # more than $100 HAMMER = "hammer" HAMMER_PRICE = 10 HAMMER_COUNT = 100 SCREW = "screw" SCREW_PRICE = 1 SCREW_COUNT = 1000 NAIL = "nail" NAIL_PRICE = 1 NAIL_COUNT = 1000 programminghomeworkhelp.com
  • 15. SCREWDRIVER = "screwdriver" SCREWDRIVER_PRICE = 8 SCREWDRIVER_COUNT = 100 DRILL = "drill" DRILL_PRICE = 50 DRILL_COUNT = 20 WORKBENCH = "workbench" WORKBENCH_PRICE = 150 WORKBENCH_COUNT = 5 HANDSAW = "handsaw" HANDSAW_PRICE = 15 HANDSAW_COUNT = 50 CHAINSAW = "chainsaw" CHAINSAW_PRICE = 80 CHAINSAW_COUNT = 30 # You should put the stuff logically into this dictionary. # You can just put it all in right here, like shown. # Try to use only one *variable*, called inventory here. programminghomeworkhelp.com
  • 16. inventory = { CHEAP : { HAMMER : (HAMMER_PRICE, HAMMER_COUNT), NAIL : (NAIL_PRICE, NAIL_COUNT), SCREW : (SCREW_PRICE, SCREW_COUNT), SCREWDRIVER : (SCREWDRIVER_PRICE, SCREWDRIVER_COUNT), HANDSAW : (HANDSAW_PRICE, HANDSAW_COUNT) }, MODERATE : { DRILL : (DRILL_PRICE, DRILL_COUNT), CHAINSAW : (CHAINSAW_PRICE, CHAINSAW_COUNT) }, EXPENSIVE : { WORKBENCH : (WORKBENCH_PRICE, WORKBENCH_COUNT) } } def get_items(cheapness): """ Return a list of (item, (price, count)) tuples that are the given cheapness. Note that the second element of the tuple is another tuple. """ return inventory[cheapness].items() programminghomeworkhelp.com
  • 17. # Testing cheap = get_items(CHEAP) print type(cheap) is list print len(cheap) == 5 print (HAMMER, (HAMMER_PRICE, HAMMER_COUNT)) in cheap print (NAIL, (NAIL_PRICE, NAIL_COUNT)) in cheap print (SCREW, (SCREW_PRICE, SCREW_COUNT)) in cheap print (SCREWDRIVER, (SCREWDRIVER_PRICE, SCREWDRIVER_COUNT)) in cheap print (HANDSAW, (HANDSAW_PRICE, HANDSAW_COUNT)) in cheap moderate = get_items(MODERATE) print type(moderate) is list print len(moderate) == 2 print (DRILL, (DRILL_PRICE, DRILL_COUNT)) in moderate print (CHAINSAW, (CHAINSAW_PRICE, CHAINSAW_COUNT)) in moderate expensive = get_items(EXPENSIVE) print type(expensive) is list print len(expensive) == 1 print (WORKBENCH, (WORKBENCH_PRICE, WORKBENCH_COUNT)) in expensive programminghomeworkhelp.com
  • 18. # webindexer2.py # Stub file for lab 10, problem 2 # # 6.189 - Intro to Python # IAP 2008 - Class 8 from urllib import urlopen from htmltext import HtmlTextParser FILENAME = "smallsites.txt" index = {} def get_sites(): """ Return all the sites that are in FILENAME. """ sites_file = open(FILENAME) sites = [] for site in sites_file: sites.append("http://" + site.strip()) return sites programminghomeworkhelp.com
  • 19. def read_site(site): """ Attempt to read the given site. Return the text of the site if successful, otherwise returns False. """ try: connection = urlopen(site) html = connection.read() connection.close() except: return False parser = HtmlTextParser() parser.parse(html) return parser.get_text() def index_site(site, text): """ Index the given site with the given text. """ words = text.lower().split() for word in words: if word not in index: # case 1: haven't seen word anywhere index[word] = {site:1} # make a new entry for the word elif site not in index[word]: # case 2: haven't seen word on programminghomeworkhelp.com
  • 20. this site index[word][site] = 1 # make a new entry for this site else: # case 3: seen this word on this site index[word][site] += 1 # increment the frequency by 1 def search_single_word(word): """ Return a list of (site, frequency) tuples for all sites containing the given word, sorted by decreasing frequency. """ if word not in index: return [] L = index[word].items() L.sort(key = lambda pair: pair[1], reverse = True) return L def search_multiple_words(words): """ Return a list of (site, frequency) tuples for all sites containing any of the given words, sorted by decreasing frequency. """ all_sites = {} for word in words: for site, freq in search_single_word(word): if site not in all_sites: # case 1: haven't included this site all_sites[site] = freq # make a new entry for site, freq programminghomeworkhelp.com
  • 21. else: # case 2: have included this site all_sites[site] += freq # add the frequencies L = all_sites.items() L.sort(key = lambda pair: pair[1], reverse = True) return L def build_index(): """ Build the index by reading and indexing each site. """ for site in get_sites(): text = read_site(site) while text == False: text = read_site(site) # keep attempting to read until successful index_site(site, text) programminghomeworkhelp.com