Example #1
0
def main():
    # This command-line parsing code is provided.
    # Make a list of command line arguments, omitting the [0] element
    # which is the script itself.
    args = sys.argv[1:]

    if len(args) < 1:
        print('usage: python run.py filename')
        sys.exit(1)

    filename = args[0]
    parser = BabynameParser(filename)

    # Parse male-names and female-names from the file with the implemented parser.
    parsed_malenames = parser.parse(lambda tup: (tup[0], tup[
        1]))  # TODO: Parse the rank and male-name tuples with your lambda.
    # TODO: Parse the rank and female-name tuples with your lambda.
    parsed_femalenames = parser.parse(lambda tup: (tup[0], tup[2]))
    # Find the common popular names.
    common_names = []
    for male in parsed_malenames:
        for female in parsed_femalenames:
            if male[1] == female[1]:
                common_names.append(male[1] + ": " + male[0] + ", " +
                                    female[0])
    # TODO: Fill the common_names with (common popular babyname: male-rank, female-rank) strings.
    # TODO: Sort the list in ascending alphabetical order.
    common_names.sort()

    # Print your result.
    print("Common popular babynames in {0} (Count: {1})".format(
        parser.year, str(len(common_names))))
    print("Common babyname: male rank, female rank")
    for common_name in common_names:
        print(common_name)
Example #2
0
def main():
    """
    (5 points)
    """

    args = sys.argv[1:]

    if len(args) < 2:
        print('usage: python run.py `starting year` `ending year`')
        sys.exit(1)

    year1, year2 = int(args[0]), int(args[1])
    records = []  # list of BabyRecord objects
    prev_male_ranking = {}  # use this to calculate the rank if you need
    prev_female_ranking = {}
    for year in range(year1, year2 + 1):
        parser = BabynameParser("babydata", year)
        # TODO: In the following two lines, change `None` to your lambda function to parse baby name records.
        # By using the lambda function, `parse` method should return a list of `BabyRecord` objects
        # that contain year, rank, name, and gender data.
        male_records = parser.parse(
            lambda baby_tuple: baby_tuple[1]
        )  # Parse the male ranks and store them as a list of `BabyRecord` objects.
        for i in range(len(male_records)):

            if male_records[i] in prev_male_ranking:
                bb = BabyRecord(parser.year, i, male_records[i], 'M',
                                i - prev_male_ranking[male_records[i]])
            else:
                bb = BabyRecord(parser.year, i, male_records[i], 'M')
            records.append(bb)
        prev_male_ranking = {}
        for i in range(len(male_records)):
            prev_male_ranking[male_records[i]] = i

        female_records = parser.parse(
            lambda baby_tuple: baby_tuple[2]
        )  # Parse the female ranks and store it as a list of `BabyRecord` objects.
        for i in range(len(female_records)):
            if female_records[i] in prev_female_ranking:
                bb = BabyRecord(parser.year, i, female_records[i], 'F',
                                i - prev_female_ranking[female_records[i]])
            else:
                bb = BabyRecord(parser.year, i, female_records[i], 'F')
            records.append(bb)
        prev_female_ranking = {}
        for i in range(len(female_records)):
            prev_female_ranking[female_records[i]] = i

        # TODO: Calculate the rank change for each of `male_records` and `female_records`.
        # For example, if the rank of the previous year is 8 and the rank of the current year is 5,
        # -3 is the rank change. (Beware the sign of the value. Rank-up is respresented with a negative value!)
        # If the rank of previous year is not available, set `rank_change` to `None`.

    # TODO: Save the result as a csv file named `babyname.report.csv` under `babydata/` directory.
    # To verify correctness, try running this function using the html files we provided in the swppfall2020 repository
    # and compare the content of the babyname.report.csv.
    # Due to the inconsistency of the ranking information provided by ssa.gov, your output and the provided example output may differ
    # See issue #12 for more info
    save(os.path.join("babydata", "babyname.report.csv"), records)
Example #3
0
def main():
    # This command-line parsing code is provided.
    # Make a list of command line arguments, omitting the [0] element
    # which is the script itself.
    args = sys.argv[1:]

    if len(args) < 1:
        print('usage: python run.py filename')
        sys.exit(1)

    filename = args[0]
    parser = BabynameParser(filename)

    # Parse male-names and female-names from the file with the implemented parser.
    parsed_malenames = parser.parse(lambda x: x[1])
    parsed_femalenames = parser.parse(lambda x: x[2])

    # Find the common popular names.
    common_names = sorted(set(parsed_malenames) & set(parsed_femalenames))
    common_names = [
        '{}: {}, {}'.format(name,
                            parsed_malenames.index(name) + 1,
                            parsed_femalenames.index(name) + 1)
        for name in common_names
    ]

    # Print your result.
    print("Common popular babynames in {0} (Count: {1})".format(
        parser.year, str(len(common_names))))
    print("Common babyname: male rank, female rank")
    for common_name in common_names:
        print(common_name)
Example #4
0
def main():
    # This command-line parsing code is provided.
    # Make a list of command line arguments, omitting the [0] element
    # which is the script itself.
    args = sys.argv[1:]

    if len(args) < 1:
        print('usage: python run.py filename')
        sys.exit(1)

    filename = args[0]
    parser = BabynameParser(filename)

    # Parse male-names and female-names from the file with the implemented parser.
    parsed_malenames = parser.parse(None)  # TODO: Parse the rank and male-name tuples with your lambda.
    parsed_femalenames = parser.parse(None)  # TODO: Parse the rank and female-name tuples with your lambda.

    # Find the common popular names.
    common_names = []
    # TODO: Fill the common_names with (common popular babyname: male-rank, female-rank) strings.
    # TODO: Sort the list in ascending alphabetical order.

    # Print your result.
    print("Common popular babynames in {0} (Count: {1})".format(parser.year, str(len(common_names))))
    print("Common babyname: male rank, female rank")
    for common_name in common_names:
        print(common_name)
Example #5
0
def main():
    # This command-line parsing code is provided.
    # Make a list of command line arguments, omitting the [0] element
    # which is the script itself.

    args = sys.argv[1:]

    if len(args) < 1:
        print('usage: python run.py filename')
        sys.exit(1)

    filename = args[0]
    parser = BabynameParser(filename)

    # Parse male-names and female-names from the file with the implemented parser.
    parsed_malenames = parser.parse(lambda rank_tuple: rank_tuple[
        1])  # TODO: Parse the rank and male-name tuples with your lambda.
    parsed_femalenames = parser.parse(lambda rank_tuple: rank_tuple[
        2])  # TODO: Parse the rank and female-name tuples with your lambda.

    # Find the common popular names.
    common_names = []
    # TODO: Fill the common_names with (common popular babyname: male-rank, female-rank) strings.
    # TODO: Sort the list in ascending alphabetical order.
    malenames_sorted = sorted(parser.rank_to_names_tuples,
                              key=lambda rank_tuple: rank_tuple[1])
    femalenames_sorted = sorted(parser.rank_to_names_tuples,
                                key=lambda rank_tuple: rank_tuple[2])
    parsed_malenames.sort()
    parsed_femalenames.sort()
    malename_idx = 0
    femalename_idx = 0
    while True:
        if malename_idx == len(parsed_malenames) or femalename_idx == len(
                parsed_femalenames):
            break
        if parsed_malenames[malename_idx] > parsed_femalenames[femalename_idx]:
            femalename_idx += 1
        elif parsed_malenames[malename_idx] < parsed_femalenames[
                femalename_idx]:
            malename_idx += 1
        else:
            common_names.append(parsed_malenames[malename_idx] + ": " +
                                malenames_sorted[malename_idx][0] + ", " +
                                femalenames_sorted[femalename_idx][0])
            femalename_idx += 1
            malename_idx += 1

    # Print your result.
    print("Common popular babynames in {0} (Count: {1})".format(
        parser.year, str(len(common_names))))
    print("Common babyname: male rank, female rank")
    for common_name in common_names:
        print(common_name)
Example #6
0
def main():
    """
    (5 points)
    """
    args = sys.argv[1:]

    if len(args) < 2:
        print('usage: python run.py `starting year` `ending year`')
        sys.exit(1)

    year1, year2 = int(args[0]), int(args[1])

    records = []  # list of BabyRecord objects
    prev_male_ranking = {}  # use this to calculate the rank if you need
    prev_female_ranking = {}

    for year in range(year1, year2 + 1):
        parser = BabynameParser("babydata", year)

        # TODO: In the following two lines, change `None` to your lambda function to parse baby name records.
        # By using the lambda function, `parse` method should return a list of `BabyRecord` objects
        # that contain year, rank, name, and gender data.
        male_records = parser.parse(
            lambda x: BabyRecord(year, x[0], x[1], 'M')
        )  # Parse the male ranks and store them as a list of `BabyRecord` objects.
        female_records = parser.parse(
            lambda x: BabyRecord(year, x[0], x[2], 'F')
        )  # Parse the female ranks and store it as a list of `BabyRecord` objects.

        # TODO: Calculate the rank change for each of `male_records` and `female_records`.
        # For example, if the rank of the previous year is 8 and the rank of the current year is 5,
        # -3 is the rank change. (Beware the sign of the value. Rank-up is respresented with a negative value!)
        # If the rank of previous year is not available, set `rank_change` to `None`.
        if year > year1:
            prev_parser = BabynameParser("babydata", year - 1)
            prev_male_ranking = prev_parser.parse(lambda x: x[1])
            prev_female_ranking = prev_parser.parse(lambda x: x[2])

            for i in range(0, 1000):
                if male_records[i].name in prev_male_ranking:
                    male_records[i].rank_change = int(
                        male_records[i].rank) - prev_male_ranking.index(
                            male_records[i].name) - 1
                if female_records[i].name in prev_female_ranking:
                    female_records[i].rank_change = int(
                        female_records[i].rank) - prev_female_ranking.index(
                            female_records[i].name) - 1

        records = records + male_records + female_records
    # TODO: Save the result as a csv file named `babyname.report.csv` under `babydata/` directory.
    # The example output of `babyname.report.csv` is provided in `babydata/` folder.
    # You should make the same result with the example file.

    save(os.path.join("babydata", "babyname.report.csv"), records)
Example #7
0
def main():
    # This command-line parsing code is provided.
    # Make a list of command line arguments, omitting the [0] element
    # which is the script itself.
    args = sys.argv[1:]

    if len(args) < 1:
        print('usage: python run.py filename')
        sys.exit(1)

    filename = args[0]
    parser = BabynameParser(filename)

    # Parse male-names and female-names from the file with the implemented parser.
    parsed_malenames = parser.parse(lambda x: (x[0], x[
        1]))  # TODO: Parse the rank and male-name tuples with your lambda.
    parsed_femalenames = parser.parse(lambda x: (x[0], x[
        2]))  # TODO: Parse the rank and female-name tuples with your lambda.

    malenames = {x[1]: x[0]
                 for x in parsed_malenames
                 }  # (names: ranks) /made to dict for fast search
    femalenames = {x[1]: x[0]
                   for x in parsed_femalenames
                   }  # (names: ranks) /made to dict for fast search

    # Find the common popular names.
    common_names = []
    for name in malenames:
        if name in femalenames:  # if it is common
            s = "{}: {}, {}".format(name, malenames[name], femalenames[name])
            common_names.append(s)

    common_names = sorted(common_names)

    # Print your result.
    print("Common popular babynames in {0} (Count: {1})".format(
        parser.year, str(len(common_names))))
    print("Common babyname: male rank, female rank")
    for common_name in common_names:
        print(common_name)
Example #8
0
def main():
    # This command-line parsing code is provided.
    # Make a list of command line arguments, omitting the [0] element
    # which is the script itself.
    args = sys.argv[1:]

    if len(args) < 1:
        print('usage: python run.py filename')
        sys.exit(1)

    filename = args[0]
    parser = BabynameParser(filename)

    # Parse male-names and female-names from the file with the implemented parser.
    parsed_malenames = parser.parse(lambda x : (x[1], x[0]))  # TODO: Parse the rank and male-name tuples with your lambda.
    parsed_femalenames = parser.parse(lambda x : (x[2], x[0]))  # TODO: Parse the rank and female-name tuples with your lambda.

    md = dict(parsed_malenames)
    fd = dict(parsed_femalenames)
    m_name, m_rank = zip(*parsed_malenames)
    f_name, f_rank = zip(*parsed_femalenames)

    c_name = list(set(m_name) & set(f_name))
    c_name.sort()

    # Find the common popular names.
    common_names = []
    # TODO: Fill the common_names with (common popular babyname: male-rank, female-rank) strings.
    # TODO: Sort the list in ascending alphabetical order.
    for name in c_name:
        common_names.append("{n}: {mr}, {fr}".format(n = name, mr = md[name], fr = fd[name]))

    # Print your result.
    print("Common popular babynames in {0} (Count: {1})".format(parser.year, str(len(common_names))))
    print("Common babyname: male rank, female rank")
    for common_name in common_names:
        print(common_name)