Question

In: Computer Science

Intro to Python I'm getting trouble obtaining the solution for the following questions: "How many babies...

Intro to Python

I'm getting trouble obtaining the solution for the following questions:

"How many babies were born with names starting with that least-common letter?" For the file used it is "U"

"How many babies were born with names starting with that-common letter"? For the file used it is "A"

"How many people have that name?" (This follows the question "By default, the Social Security Administration's data separates out names by gender. For example, Jamie is listed separately for girls and for boys. If you were to remove this separation, what would be the most common name in the 2010s regardless of gender?" And the file I used it is "Isabella")

"What name that is used for both genders has the smallest difference in which gender holds the name most frequently? In case of a tie, enter any one of the correct answers."

This is the problem:

#-----------------------------------------------------------
#The United States Social Security Administration publishes
#a list of all documented baby names each year, along with
#how often each name was used for boys and for girls. The
#list is used to see what names are most common in a given
#year.
#
#We've grabbed that data for any name used more than 25
#times, and provided it to you in a file called
#babynames.csv. The line below will open the file:

names_file = open('../resource/lib/public/babynames.csv', 'r')

#We've also provided a sample subset of the data in
#sample.csv.
#
#Each line of the file has three values, separated by
#commas. The first value is the name; the second value is
#the number of times the name was given in the 2010s (so
#far); and the third value is whether that count
#corresponds to girls or boys. Note that if a name is
#given to both girls and boys, it is listed twice: for
#example, so far in the 2010s, the name Jamie has been
#given to 611 boys and 1545 girls.
#
#Use this dataset to answer the questions below.


#Here, add any code you want to allow you to answer the
#questions asked below over on edX. This is just a sandbox
#for you to explore the dataset: nothing is required for
#submission here.

//

This is the format of the file but this is not file the problem is based on:

Isabella,42567,Girl
Sophia,42261,Girl
Jacob,42164,Boy
Emma,35951,Girl
Ethan,34523,Boy
Mason,34195,Boy
William,34130,Boy
Olivia,34128,Girl
Jayden,33962,Boy
Ava,30765,Girl

I found the solution and it's this:

baby_list=[]

for line in names_file:
line=line.strip().split(",")
name=line[0]
count=int(line[1])
gender=line[2]
baby_list.append([name,count,gender])

#How many total names are listed in the database?
print('Total names:',len(baby_list))

#How many total births are covered by the names in the database?
births=0
for data in baby_list:
births=births+data[1]

print('total births:',births)

#How many different boys' names are there that begin with the letter Z?
#(Count the names, not the people.)
names_with_z=0
for data in baby_list:
if data[0][0]=='Z' and data[2]=='Boy':
names_with_z=names_with_z+1

print('Different boys names are there that begin with the letter Z:',names_with_z)

#What is the most common girl's name that begins with the letter Q?
names_with_Q=0
for data in baby_list:
if data[0][0]=='Q' and data[2]=='Girl' and data[1]> names_with_Q:
girl_name=data[0]
names_with_Q=data[1]

print('The most common girl\'s name that begins with the letter Q:',names_with_Q)

#How many total babies were given names that both start and end with vowels (A, E, I, O, or U)?
vowels='AEIOUaeiou'
total_names=0
for data in baby_list:
if data[0][0] in vowels and data[0][-1] in vowels:
total_names=total_names+ data[1]

print('The total babies were given names that both start and end with vowels(A,E,I,O,or U):'\
,total_names)

#Here, add any code you want to allow you to answer the
#questions asked below over on edX. This is just a sandbox
#for you to explore the dataset: nothing is required for
#submission here.

#What letter is the least common first letter of a baby's name
name_list = {}
for i in range(len(baby_list)):
fl = list(baby_list[i][0])[0]
if fl not in name_list.keys():
name_list[fl] = 1
else:
name_list[fl] = int(name_list[fl]) + 1
#What letter is the least common first letter of a baby's name
leastKey = 9999
mostKey = 0
least=''
most=''
print(name_list.keys())
for key in name_list.keys():
if name_list[key] < leastKey:
least = key
leastKey = name_list[key]
if name_list[key] > mostKey:
most = key
mostKey = name_list[key]
print('\nThe letter is the least common first letter of a baby\'s name is: ',least)
#How many babies were born with names starting with that least-common letter?
print('\nNumber of babie names starting with that least-common letter',name_list[least])
#What letter is the most common first letter of a baby's name
print("\nThe letter is the most common first letter of a baby's name is: ",most)
#How many babies were born with names starting with that most-common letter?
print('\nNumber of babie names starting with that most-common letter: ',name_list[most])

'''
#By default, the Social Security Administration's data separates out names by gender.
#For example, Jamie is listed separately for girls and for boys.
#If you were to remove this separation, what would be the most common name
in the 2010s regardless of gender?
'''
name_list = {}
for i in range(len(baby_list)):
name = baby_list[i][0]
ctr = baby_list[i][1]
gndr = baby_list[i][2]
if name in name_list.keys():
name_list[name] = int(name_list[name]) + 1
else:
name_list[name] = 1
mostKey = 0
most=''
for key in name_list.keys():
if name_list[key] > mostKey:
most = key
mostKey = name_list[key]
print('\nThe most common name in the 2010s regardless of gender: ',most)

#How many people would have that name?
print('\nThe number of people have most common name regardless of gender: ',mostKey)

'''
What name that is used for both genders has the smallest difference in
which gender holds the name most frequently? In case of a tie,
enter any one of the correct answers.
'''
mini = name_list[most]
name_list = {}
for i in range(len(baby_list)):
name = baby_list[i][0]
ctr = baby_list[i][1]
gndr = baby_list[i][2]
if name in name_list.keys() and gndr != name_list[name][1]:
name_list[name] = [int(name_list[name][0]) -1, gndr, 1]
else:
name_list[name] = [1, gndr, 0]

for x in name_list.keys():
if name_list[x][2] == 1 and name_list[x][0] < mini:
mini = name_list[x][0]
nme = x
print('Name that is used for both genders has the smallest difference: ',x)

Solutions

Expert Solution


# function to return count of babies names starting with least common letter
def leastCommon(x):
    c=0
    for i in range(len(names)):
        if names[i][0]==x:
            c+=count[i];
    return c

# function to return count of babies names starting with common letter
def common(x):
    c=0
    for i in range(len(names)):
        if names[i][0]==x:
            c+=count[i];
    return c
    
# function to count babies having same given name
def sameName(x):
    c=0
    for i in range(len(names)):
        if names[i]==x:
            c+=count[i];
    return c
    
# function to get name used for both gender having smallest difference
def smallestDiff():
    dict1={}
    m=999999
    name=""
    for i in range(len(names)):
        if names[i] in dict1 and dict1[names[i]][1]!=gen[i]:
            if abs(count[i]-dict1[names[i]][1])<m:
                m=abs(count[i]-dict1[names[i]][1])
                name=names[i]
        else:# if new name in dict1 then add value as list of gender and it's count
            dict1[names[i]]=[gen[i],count[i]]
    return name

# open file in reading mode
file=open("sample.csv","r") 
names=[]# list of name
count=[]# list of count
gen=[]# list of gender

for line in file:
    line=line.strip().split(",")
    names.append(line[0])
    count.append(int(line[1]))
    gen.append(line[2])

print("Number of babies name having starting letter is least common letter ie. U: "+str(leastCommon('U')))
print("Number of babies name having starting letter is common letter ie. A: "+str(leastCommon('A')))
print("Number of babies having same name as Isabella: "+str(sameName("Isabella")))
print("Name that is used for both genders has the smallest difference: "+smallestDiff())


Related Solutions

Over and over I'm getting into the same trouble, so I'd like to ask for some...
Over and over I'm getting into the same trouble, so I'd like to ask for some help. I need to solve some basic electrodynamics problem, involving magnetic fields, moving charges or currents. But I forgot all this rules about "where the magnetic field should go if the current flows up". I vaguely remember that it about hands or fingers or books or guns, and magnetic field should go somewhere along something, while current should flow along something else... But it...
​​​​​​This is an assignment that I'm having trouble figuring out in python: Modify Node class so...
​​​​​​This is an assignment that I'm having trouble figuring out in python: Modify Node class so it has a field called frequency and initialize it as one. Add a search function. If a number is in the list, return its frequency. Modify Insert function. Remove push, append, and insertAfter as one function called insert(self, data). If you insert a data that is already in the list, simply increase this node’s frequency. If the number is not in the list, add...
I'm having trouble with these few questions out of a long list of journal entries that...
I'm having trouble with these few questions out of a long list of journal entries that I have to record for a project. If you could please state the journal entries for these and why that would be very helpful. Thank you! May 2 – Sold merchandise on credit to Yellow Rock Company, Invoice No. 9501, for $4,500 (cost is $2,000). I get the first part of the journal entry but don't know how to record the cost. May 3...
Balance in neutral solution: MnO4- + S2O32- ---> SO42- + MnO2 I'm specifically having trouble with...
Balance in neutral solution: MnO4- + S2O32- ---> SO42- + MnO2 I'm specifically having trouble with using H2O, H+, and OH- in balancing the half reactions.
Hi, I'm having trouble with answering these organic chemistry lab questions. Please give full answers in...
Hi, I'm having trouble with answering these organic chemistry lab questions. Please give full answers in your own words. Thanks in advance 1. Which of the following solvents: acetone, diethly ether, dichloromethane, pentane, THF, form upper layers or lower layers and which are miscible or immiscible with water during an extraction? 2. What is the consequence of only cooling the recrystallization solution to room temperature instead of 0 degrees celsius. 3. What is the consequence of only cooling the recrysyallization...
I'm having trouble with my ZeroDenominatorException. How do I implement it to where if the denominator...
I'm having trouble with my ZeroDenominatorException. How do I implement it to where if the denominator is 0 it throws the ZeroDenominatorException and the catch catches to guarantee that the denominator is never 0. /** * The main class is the driver for my Rational project. */ public class Main { /** * Main method is the entry point to my code. * @param args The command line arguments. */ public static void main(String[] args) { int numerator, denominator =...
I'm having trouble understanding the following code (a snippet of a code). What does it do?...
I'm having trouble understanding the following code (a snippet of a code). What does it do? The whole code is about comparing efficiencies of different algorithms. def partition(list,first,last): piv = list[first] lmark = first+1 rmark = last done = False while not done: while lmark <= rmark and list[lmark]<=piv: lmark=lmark+1 while list[rmark]>=piv and rmark>=lmark: rmark=rmark-1 if rmark<lmark: done = True else: temp = list[lmark] list[lmark]=list[rmark] list[rmark]=temp temp = list[first] list[first]=list[rmark] list[rmark]=temp return rmark
I'm getting an error message with this code and I don't know how to fix it...
I'm getting an error message with this code and I don't know how to fix it The ones highlighted give me error message both having to deal Scanner input string being converted to an int. I tried changing the input variable to inputText because the user will input a number and not a character or any words. So what can I do to deal with this import java.util.Scanner; public class Project4 { /** * @param args the command line arguments...
hello, I'm having trouble understanding how to do these two problems could you show me a...
hello, I'm having trouble understanding how to do these two problems could you show me a step by step. 1)Eight sprinters have made it to the Olympic finals in the 100-meter race. In how many different ways can the gold, silver, and bronze medals be awarded? 2)Suppose that 34% of people own dogs. If you pick two people at random, what is the probability that they both own a dog? Give your answer as a decimal (to at least 3...
The following is the adjusted trial balance for Miller Company. NOTE: I'M GETTING 7840 FOR INCOME...
The following is the adjusted trial balance for Miller Company. NOTE: I'M GETTING 7840 FOR INCOME SUMMARY AND RETAINED EARNINGS, BUT THE HOMEWORK SYSTEM SAYS ITS WRONG, HELP PLEASE! Miller Company Adjusted Trial Balance December 31 Cash 8,130 Accounts Receivable 3,300 Prepaid Expenses 2,750 Equipment 10,400 Accumulated Depreciation 2,200 Accounts Payable 2,700 Notes Payable 1,000 Common Stock 9,200 Retained Earnings 2,000 Dividends 4,870 Fees Earned 36,600 Wages Expense 12,450 Rent Expense 4,900 Utilities Expense 3,475 Depreciation Expense 2,150 Miscellaneous Expense...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT