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

Phyton Learning extracts
Phyton Learning extracts Phyton Learning extracts
Phyton Learning extracts Pavan Babu .G
 
Introduction to Python - Training for Kids
Introduction to Python - Training for KidsIntroduction to Python - Training for Kids
Introduction to Python - Training for KidsAimee Maree Forsstrom
 
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 LoopsP3 InfoTech Solutions Pvt. Ltd.
 
BayFP: Concurrent and Multicore Haskell
BayFP: Concurrent and Multicore HaskellBayFP: Concurrent and Multicore Haskell
BayFP: Concurrent and Multicore HaskellBryan O'Sullivan
 
Currying and Partial Function Application (PFA)
Currying and Partial Function Application (PFA)Currying and Partial Function Application (PFA)
Currying and Partial Function Application (PFA)Dhaval Dalal
 
Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)Rick Copeland
 
python Function
python Function python Function
python Function Ronak Rathi
 
Python language data types
Python language data typesPython language data types
Python language data typesHoang Nguyen
 
Python programming Workshop SITTTR - Kalamassery
Python programming Workshop SITTTR - KalamasseryPython programming Workshop SITTTR - Kalamassery
Python programming Workshop SITTTR - KalamasserySHAMJITH KM
 
Introduction to Python - Part Three
Introduction to Python - Part ThreeIntroduction to Python - Part Three
Introduction to Python - Part Threeamiable_indian
 
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 MinutesMatt Harrison
 
1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answersAkash Gawali
 
CS225_Prelecture_Notes 2nd
CS225_Prelecture_Notes 2ndCS225_Prelecture_Notes 2nd
CS225_Prelecture_Notes 2ndEdward Chen
 
JavaScript Functions
JavaScript FunctionsJavaScript Functions
JavaScript FunctionsBrian Moschel
 

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

Arrays & functions in php
Arrays & functions in phpArrays & functions in php
Arrays & functions in phpAshish Chamoli
 
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.docxanhlodge
 
Notes5
Notes5Notes5
Notes5hccit
 
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)Live Exam Helper
 
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.docxfestockton
 
Real World Haskell: Lecture 1
Real World Haskell: Lecture 1Real World Haskell: Lecture 1
Real World Haskell: Lecture 1Bryan O'Sullivan
 
Automation Testing theory notes.pptx
Automation Testing theory notes.pptxAutomation Testing theory notes.pptx
Automation Testing theory notes.pptxNileshBorkar12
 
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.docxdunhamadell
 
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.pdflailoesakhan
 
Playfulness at Work
Playfulness at WorkPlayfulness at Work
Playfulness at WorkErin Dees
 
Reproducibility with R
Reproducibility with RReproducibility with R
Reproducibility with RMartin Jung
 
Python cheatsheet Indian Servers
Python cheatsheet Indian ServersPython cheatsheet Indian Servers
Python cheatsheet Indian ServersIndian Servers
 
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

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 HelpProgramming 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.pptxProgramming 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

AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayMakMakNepo
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 

Recently uploaded (20)

AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
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
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up Friday
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
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🔝
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 

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