Best and Affordable Python Homework Help
Best and Affordable Python Homework Help
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/
Problems:
# namesages.py
# Stub file for lab 9, problem 1
#
# 6.189 - Intro to Python
# IAP 2008 - Class 8
NAMES = ["Alice", "Bob", "Cathy", "Dan", "Ed", "Frank",
"Gary", "Helen", "Irene", "Jack", "Kelly", "Larry"]
AGES = [20, 21, 18, 18, 19, 20, 20, 19, 19, 19, 22, 19]
# Put code here that will combine these lists into a dictionary
# You can rename this function
def people(age):
""" Return the names of all the people who are the given age.
"""
# webindexer1.py
# Stub file for lab 9, problem 2
#
# 6.189 - Intro to Python
# IAP 2008 - Class 8
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()
except:
return False
parser = HtmlTextParser()
parser.parse(html)
return parser.get_text()
def search_single_word(word):
""" Return a list of sites containing the given word. """
# YOUR CODE HERE #
pass # delete this when you write your code
def search_multiple_words(words):
""" Return a list of sites containing any of the given words.
"""
# YOUR CODE HERE #
pass # delete this when you write your code
def build_index():
""" Build the index by reading and indexing each site. """
for site in get_sites():
text = read_site(site)
while text == False:
Solutions:
# namesages.py
# Stub file for lab 9, problem 1
#
# 6.189 - Intro to Python
# IAP 2008 - Class 8
ages_to_names = {}
for i in range(len(NAMES)):
name = NAMES[i]
age = AGES[i]
if age in ages_to_names:
ages_to_names[age].append(name)
else:
ages_to_names[age] = [name] # the LIST
containing the name
def people(age):
""" Return the names of all the people who
are the given age. """
if age in ages_to_names:
return ages_to_names[age]
return []
# Testing
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()
except:
return False
parser = HtmlTextParser()
parser.parse(html)
return parser.get_text()
def search_single_word(word):
""" Return a list of sites containing the given word.
"""
if word not in index:
return []
return index[word]
def search_multiple_words(words):
""" Return a list of sites containing any of the given
words. """
all_sites = []
for word in words:
sites = search_single_word(word)
for site in sites:
if site not in all_sites:
all_sites.append(site)
return all_sites
def build_index():
""" Build the index by reading and indexing each site. ""“