100% found this document useful (1 vote)
16 views10 pages

Best and Affordable Python Homework Help

Get Best and Affordable Python Homework Help
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
16 views10 pages

Best and Affordable Python Homework Help

Get Best and Affordable Python Homework Help
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

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

# your code her pass


# delete this line when you write your code
# Testing

print people(18) == ["Cathy", "Dan"]


print people(19) == ["Ed", "Helen", "Irene", "Jack",
"Larry"]
print people(20) == ["Alice", "Frank", "Gary"]
print people(21) == ["Bob"]
print people(22) == ["Kelly"]
print people(23) == []

# webindexer1.py
# Stub file for lab 9, 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

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. """
# YOUR CODE HERE #
pass # delete this when you write your code

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

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

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

# You can rename this function

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

print people(18) == ["Cathy", "Dan"]


print people(19) == ["Ed", "Helen", "Irene",
"Jack", "Larry"]
print people(20) == ["Alice", "Frank", "Gary"]
print people(21) == ["Bob"]
print people(22) == ["Kelly"]
print people(23) == []
 
# webindexer1.py
# Stub file for lab 9, 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
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
this word ever
index[word] = [ site ] # make a new
list for the word
elif site not in index: # case 2: haven't
seen word on this site
index[word].append(site) # add this site to this
word's list
# case 3: have included site for
word
# do nothing (ignore this word)

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

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)

You might also like