This class is an introductory Python class. My homework question is: Your goal i
ID: 666583 • Letter: T
Question
This class is an introductory Python class. My homework question is:
Your goal is to generate strings of the form "Lastname, Firstname" where the last name is randomly chosen from a surnames list and the first name is randomly chosen from either a female names list or a male names list.
The three lists of names should be downloaded here.
• Surnames list: http://www.ics.uci.edu/~harris/python/surnames.txt
• Female names list: : http://www.ics.uci.edu/~harris/python/femalenames.txt
• Male names list: : http://www.ics.uci.edu/~harris/python/malenames.txt
(1) Write a function called random_names that takes an integer and returns a list of that many strings, with each string a randomly generated name as described above. This function is the ultimate goal of part (a) of this lab assignment. If you and your partner feel comfortable designing a solution without further guidance, go right ahead. But if you'd like a little more guidance, that's also fine. The remaining subparts of part (a) break the task down and give you some hints and approaches.
(2) To start, you'll need to read the three files of names into your program. You'll also notice that there's more data on each line of the name files than just the name. The first line of the surnames file, for example, is "SMITH 1.006 2,501,922 1", which means that the surname Smith accounts for 1.006% of the surnames in America. For this assignment, you can get by with just extracting the name and fixing its capitalization.
(3) Your function random_names should call a function to generate a single random name—a random surname, a random choice of male or female, and a random first name chosen from that list. It will be most convenient for your single-random-name function to call a function that takes one of the three name lists as a parameter and returns a name chosen at random from that list.
Explanation / Answer
from random import randint
# Array containing Surnames
surname = []
# Open the surnames.txt and populate surname
file = open("surnames.txt",'r')
for line in file:
# Split a line across Space
l = line.strip().split(" ")
surname.append(l[0])
file.close()
# Array containing female first name
female_name = []
# open the femalename.txt and
file = open("femalenames.txt",'r')
for line in file:
# Split a line across Space
l = line.strip().split(" ")
female_name.append(l[0])
file.close()
# Array containing male first name
male_name = []
# open the malename.txt and
file = open("malenames.txt",'r')
for line in file:
# Split a line across Space
l = line.strip().split(" ")
male_name.append(l[0])
file.close()
# Choose male or female
def choose_sex():
return randint(0,1)%2
def choose_index():
return randint(0,1000)%1000
def random_name(int n):
l = []
for i in range(n):
# index for random generating name
index = choose_index()
# choose male or female name
choose = choose_sex()
if (choose == 1):
s = surname[index]+", "+male_name[index]
l.append(s)
else:
s = surname[index]+", "+female_name[index]
l.append(s)
return l