Beispiel #1
0
def index(year):
    #IF NOT WITHIN 1880 AND 2014, PRINT NOTHING
    (female_names, female_counts) = read_names.top_in_year(year, 'f')
    (male_names, male_counts) = read_names.top_in_year(year, 'M')
    if year > 2014 or year < 1880:
        return -1
    #TEST TO SEE IF NAME IS IN TOP 250
    elif name not in female_names:
        print(name, 'is not among the top 250 names for', year)
    else:  #ASSIGNING VARIABLES
        rank = int(female_names.index(name))
        male_name = male_names[rank]
        female_count = female_counts[rank]
        male_count = male_counts[rank]
        female_percent_top = (female_count / female_counts[0]) * 100
        male_percent_top = (male_count / male_counts[0]) * 100
        female_percent_sum = (female_count / sum(female_counts)) * 100
        male_percent_sum = (male_count / sum(male_counts)) * 100
        #PRINT STATEMENTS
        print('Year:', year)
        print('{:12s}: {:3d} {:5d} {:7.3f} {:7.3f}%'.format(
            name, (rank + 1), female_count, female_percent_top,
            female_percent_sum))
        print('{:12s}: {:3d} {:5d} {:7.3f} {:7.3f}%'.format(
            male_name, (rank + 1), male_count, male_percent_top,
            male_percent_sum))
        print()
Beispiel #2
0
def male(year, rank):
    '''
    This function prints some statistics about the male name of an inputed rank, rank in an specific, inputed year, year. Statistics include the number of people with that name that year, the total names in the top 250 list that year, and the percentage of children in the list with the given name. Also printed is a histogram of '*'s for the inputed year, a year prior, and a year after where each '*' corresponds to 0.1 of a percent.
    '''
    read_names.read_from_file("top_names_1880_to_2014.txt")

    ##finds name of a given rank and year, defines that name in variable "name"
    (male_names, male_counts) = read_names.top_in_year(year, 'M')
    print("The rank {} most popular male name in {} is {}".format(
        rank, year, male_names[rank - 1]))
    print("\t{} out of {} or {:.2f}%".format(
        male_counts[rank - 1], sum(read_names.all_male_counts[year - 1880]),
        (male_counts[rank - 1] / sum(read_names.all_male_counts[year - 1880]) *
         100)))
    print("Histogram for {}".format(male_names[rank - 1]))
    name = male_names[rank - 1]

    ##redefines male_names and male_counts to the year before the inputed year, finds the index (position) of the targeted name, finds the percent of the name used in that year, calculates the number of asterisks, prints histogram for that year
    (male_names, male_counts) = read_names.top_in_year((year - 1), 'M')

    ##if statement for if name does not appear in the list for a year
    if male_names.count(name) == 0:
        percent = 0
    else:
        position = read_names.all_male_names[(year - 1) - 1880].index(name)
        percent = male_counts[position] / sum(
            read_names.all_male_counts[(year - 1) - 1880]) * 100

    ast = (int(percent * 10)) * '*'
    print("{}:\t{}\t({:.2f}%)".format(year - 1, ast, percent))

    ##redefines male_names and male_counts to the inputed year, finds the index (position) of the targeted name, finds the percent of the name used in that year, calculates the number of asterisks, prints histogram for that year
    (male_names, male_counts) = read_names.top_in_year(year, 'M')
    position = read_names.all_male_names[year - 1880].index(name)
    percent = male_counts[position] / sum(
        read_names.all_male_counts[year - 1880]) * 100
    ast = (int(percent * 10)) * '*'
    print("{}:\t{}\t({:.2f}%)".format(year, ast, percent))

    ##redefines male_names and male_counts to the year after the inputed year, finds the index (position) of the targeted name, finds the percent of the name used in that year, calculates the number of asterisks, prints histogram for that year
    (male_names, male_counts) = read_names.top_in_year((year + 1), 'M')

    ##if statement for if name does not appear in the list for a year
    if male_names.count(name) == 0:
        percent = 0
    else:
        position = read_names.all_male_names[(year + 1) - 1880].index(name)
        percent = male_counts[position] / sum(
            read_names.all_male_counts[(year + 1) - 1880]) * 100

    ast = (int(percent * 10)) * '*'
    print("{}:\t{}\t({:.2f}%)".format(year + 1, ast, percent))
Beispiel #3
0
def year_before_percent(current_year, name, gender):
    year_before = current_year - 1
    names, counts = read_names.top_in_year(year_before, gender)
    if (name in names) == True:
        index_name = (names.index(name))
        sum_count = sum(counts)
        count_name = counts[index_name]
        percent = count_name / sum_count
        percent = percent * 100
    else:
        percent = 0
    return percent, year_before
def analyse_male(target_male, year):
    (male_names, male_counts) = read_names.top_in_year(year, 'm')
    if male_names != []:
        if target_male in male_names:
            target_male_index = male_names.index(target_male)
            rank = target_male_index + 1
            counts = male_counts[target_male_index]
            percentage_to_top = male_counts[target_male_index] / male_counts[
                0] * 100
            percentage_to_sum = male_counts[target_male_index] / sum(
                male_counts[0:250]) * 100
            print('   {0}: {1:3d} {2:5d} {3:7.3f} {4:7.3f}'.format(
                year, rank, counts, percentage_to_top, percentage_to_sum))
        else:
            print('   {0}: Not in the top 250'.format(year))
    else:
        return 0
Beispiel #5
0
def print_line(year, gender):
    #Check to see if year is within bounds
    if year >= 1880 and year <= 2014:
        (names, counts) = read_names.top_in_year(year, gender)

        if name in names:
            # Name is found
            index = names.index(name)
            rank = index + 1
            count = counts[index]
            percentage1 = (count / counts[0]) * 100
            percentage2 = (count / sum(counts)) * 100
            outstring = "   " + str(
                year) + ": {:3d} {:5d} {:7.3f} {:7.3f}".format(
                    rank, count, percentage1, percentage2)
            print(outstring)
        else:
            # Name not found
            print("   ", year, ": Not in the top 250", sep="")
Beispiel #6
0
base_year = int(input("Enter a year (1881-2013) => "))
print(base_year)
rank = int(input("Enter a rank (1-250) => "))
print(rank)

#checking to see if year and rank are in range
if (base_year < 1881) or (base_year > 2013) or (rank < 1) or (rank > 250):
    print(
        str(base_year) + " is not in the range 1881-2013 or " + str(rank) +
        " is not in the range 1-250")
else:
    #creates the correct ranking ot use for the data
    input_ranking = rank - 1

    #reads base_year for names and counts
    (female_names, female_counts) = read_names.top_in_year(base_year, 'f')
    (male_names, male_counts) = read_names.top_in_year(base_year, 'M')
    the_female_name = female_names[input_ranking]
    the_male_name = male_names[input_ranking]

    #main rank
    main_cf, main_tf, main_pf, main_cm, main_tm, main_pm = find_percent(
        base_year, input_ranking)
    main_stars_female = stars(main_pf)
    main_stars_male = stars(main_pm)

    #find percent for before and after year
    before_pf, year_before = year_before_percent(base_year, the_female_name,
                                                 'f')
    after_pf, year_after = year_after_percent(base_year, the_female_name, 'f')
    before_pm, year_before = year_before_percent(base_year, the_male_name, 'm')
Beispiel #7
0
#Read in all the names. The result is stored in the module
read_names.read_from_file("top_names_1880_to_2014.txt")

#Check is year is valid
yearCheck = int(input('Enter the year to check => '))

#Check for female name in - 10 years
if (yearCheck >= 1880 and yearCheck <= 2014):
    print(yearCheck)
    name = input('Enter a female name => ')
    print(name)
    print('Data about female names')
    print(name + ':')
    if (yearCheck - 10 >= 1880 and yearCheck - 10 <= 2014):
        (female_names,
         female_counts) = read_names.top_in_year(yearCheck - 10, 'f')
        if (name in female_names):
            i = female_names.index(name)
            percent = female_counts[i] / female_counts[0] * 100
            percentC = female_counts[i] / sum(female_counts) * 100
            print('   {:d}: {:3d} {:5d} {:7.3f} {:7.3f}'.format(
                yearCheck - 10, i + 1, female_counts[i], percent, percentC))

        else:
            print('   {:d}: Not in the top 250'.format(yearCheck - 10))

#Check for female name in - 5 years
    if (yearCheck - 5 >= 1880 and yearCheck - 5 <= 2014):
        (female_names,
         female_counts) = read_names.top_in_year(yearCheck - 5, 'f')
        if (name in female_names):
Beispiel #8
0
'''
Initial example to demonstrate how to read and access the baby names
and counts.
'''
import read_names

# Read in all the names.  The result is stored in the module.
read_names.read_from_file("top_names_1880_to_2014.txt")

# Access the female list and counts for 1886
(female_names,female_counts) = read_names.top_in_year(1886, 'f') 
print("The most common female name in 1886, with a count of {:d}, is {:s}"\
    .format(female_counts[0], female_names[0]))

# Access the male list and counts for 1997.  Note that the 100th most
# popular name is in position 99 of the list.
(male_names,male_counts) = read_names.top_in_year(1997, 'M') 
print("The 100th most common male name in 1997, with a count of {:d}, is {:s}"\
    .format(male_counts[99], male_names[99]))
    
Beispiel #9
0
    else:
        print '%s is not in top 250.' % name


# read in year and names from the user and call the functions above to output the table required.
read_names.read_from_file('top_names_1880_to_2014.txt')
year = raw_input('Enter the year to check => ')
print year
if int(year) < 1880 or int(year) > 2014:
    print 'Year must be at least 1880 and at most 2014'
    sys.exit()
print
female_name = raw_input('Enter a female name => ')
print female_name
print 'Data about female names'
(female_names, female_counts) = read_names.top_in_year(int(year), 'f')
print 'Top ranked name %s' % female_names[0]
print '250th ranked name %s' % female_names[249]
list_250_female = female_names[0:250]

female = table_(female_name, female_names, female_counts)
print
male_name = raw_input('Enter a male name => ')
print male_name
print 'Data about male names'
(male_names, male_counts) = read_names.top_in_year(int(year), 'M')
print 'Top ranked name %s' % male_names[0]
print '250th ranked name %s' % male_names[249]
list_250_male = male_names[0:250]
male = table_(male_name, male_names, male_counts)