100% found this document useful (1 vote)
21 views

Best Python Assignment Help

Best Python Assignment Help
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
21 views

Best Python Assignment Help

Best Python Assignment Help
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

For any Assignment related queries, Call us at : -  

+1 678 648 4277


You can mail us at :- [email protected] or
reach us at :- www.pythonhomeworkhelp.com/

BEST PYTHON ASSIGNMENT HELP


PROBLEMS
# 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
pythonhomeworkhelp.com
NAIL = "nail"
NAIL_PRICE = 1
NAIL_COUNT = 1000

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

pythonhomeworkhelp.com
# 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.

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

pythonhomeworkhelp.com
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

# webindexer2.py
# Stub file for lab 10, problem 2
#
# 6.189 - Intro to Python
# IAP 2008 - Class 8

pythonhomeworkhelp.com
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

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()

pythonhomeworkhelp.com
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. """
# 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 #

pythonhomeworkhelp.com
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)
 

pythonhomeworkhelp.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"

pythonhomeworkhelp.com
NAIL_PRICE = 1
NAIL_COUNT = 1000

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

pythonhomeworkhelp.com
# 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.

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)
}
}

pythonhomeworkhelp.com
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()

# 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

pythonhomeworkhelp.com
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"
index = {}

def get_sites():

pythonhomeworkhelp.com
""" 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)
return parser.get_text()

pythonhomeworkhelp.com
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 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

pythonhomeworkhelp.com
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
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)

pythonhomeworkhelp.com

You might also like