Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Instructions from your teacher: Four things to do in this assignment: 1) Complet

ID: 3599820 • Letter: I

Question

Instructions from your teacher:

Four things to do in this assignment:

1) Complete the print_first_names() function so that it prints all the first names in the list of students passed as parameter. If there are duplicates in the list (i.e. 2 or more students sharing the same first name) then print them all.

Hint: use a for loop.

2) Complete the print_unique_first_names() function so that it prints all the first names in the list of students, but duplicates should only be printed once. This is the main difference with the print_first_names() function above.

Hint: use a set to get all the first names and remove duplicates, and use a for loop to print all the names in the set.

3) Complete the print_how_many_of_each_letter_we_need() function to print how many of each letter we need (see comment in code about why we may want to know how many letters are needed).

Hint: use a dictionary: key would be a letter, value would be the number of letters needed.

4) Generate random list of students, i.e. change the implementation of the generate_students_list() function. The list I provided only has 3 students, which is not really enough to test the other functions properly. Since we don't want to type the names of all the students at Howard University, we can create (fake) random names instead. This would give us a list of maybe 10,000 students (close to what the actual number of students here is).

We basically implemented the generate_students_list() function during the lab on the whiteboard already. You can just copy that in your assignment, but feel free to modify it to for example use different syllables and/or longer names. You are free to create your own implementation as well, you don't _have_ to use the one below if you don't want to.

PROBLEM:

import random

# Let's assume we have a list of all of the Howard University students. Each
# element in the list is a 2-tuple (a tuple with 2 elements) containing the
# first name and the last name of a student.

# For example, a very short list would be:
howard_students = [('John', 'Doe'), ('John', 'Dow'), ('Jane', 'Doe')]

# In practice, such a list would contain thousands of tuples considering
# the size of Howard University.

def print_first_names(students):
# FIXME: write loop that prints all the first names (if two students
# share the same first name you should print the first name twice).
pass

def print_unique_first_names(students):
# FIXME: write loop that prints all the unique first names (if two
# students share the same first name you should print the first name
# only once).
# Hint: what data structure could you use? How many loops do you need?
pass

def print_how_many_of_each_letter_we_need(students):
# FIXME: write loop that prints how many of each letter in the alphabet
# we need. For example, imagine we need to order t-shirts for every
# student, and each t-shirt will have the student's full name in the back.
# The cost of the t-shirt may depend on how many letters we need to
# print, and each letter may have a different cost. For example, the
# letter W (upper case) could cost more to print than the lower-case
# letter o.
# Hint: what data structure could you use? How many loops do you need?
pass
  
def generate_random_student(first_names, last_names):
first_name_index = random.randint(0, len(first_names) - 1)
first_name = first_names[first_name_index]

last_name_index = random.randint(0, len(last_names) - 1)
last_name = last_names[last_name_index]

return (first_name, last_name)

def generate_students_list(number_of_students):
# FIXME: Build longer lists of possible first and last names. The names
# don't really have to exist.
# Hint: use the random number generator and a list of syllables to
# generate names.
first_names = ['John', 'Jane', 'Bob']
last_names = ['Doe', 'Dow', 'Daw']

return [generate_random_student(first_names, last_names) for _ in range(number_of_students)]

# Let's test that code now...
# First, generate a list of (fake) students.
num_students = 10000
random_students = generate_students_list(num_students)
if random_students is not None:
assert len(random_students) == num_students
howard_students = random_students
print('All first names:')
print_first_names(howard_students)

print('Unique first names:')
print_unique_first_names(howard_students)

print('How many of each letter we need:')
print_how_many_of_each_letter_we_need(howard_students)

Explanation / Answer

import random

# Let's assume we have a list of all of the Howard University students. Each
# element in the list is a 2-tuple (a tuple with 2 elements) containing the
# first name and the last name of a student.

# For example, a very short list would be:
howard_students = [('John', 'Doe'), ('John', 'Dow'), ('Jane', 'Doe')]

# In practice, such a list would contain thousands of tuples considering
# the size of Howard University.

def print_first_names(students):

for i in students:

print(i[0])

def print_unique_first_names(students):

temp_list = []

for i in students:

temp_list.append(i[0])

temp_set = set(temp_list)

print(temp_set)

def print_how_many_of_each_letter_we_need(students):

d = {} #empty dictionary

for i in students:

for j in i:

for z in j:

if(z in d.keys()):

d[z] = d[z] + 1

else:

d[z] = 1

  
def generate_random_student(first_names, last_names):
first_name_index = random.randint(0, len(first_names) - 1)
first_name = first_names[first_name_index]

last_name_index = random.randint(0, len(last_names) - 1)
last_name = last_names[last_name_index]

return (first_name, last_name)

def generate_students_list(number_of_students):

first_names = ['John', 'Jane', 'Bob']
last_names = ['Doe', 'Dow', 'Daw']

return [generate_random_student(first_names, last_names) for k in range(number_of_students)]