Ejemplo n.º 1
0
        static void synopsis(LifeExpectancyAnalyzer analyzer)
        {
            l("Total number of entities: ", analyzer.Entities.Count);
            l("Number of countries/territories: ", analyzer.Countries_Slash_Territories.Count);

            l();

            l("Regions and their country count:");

            foreach (string region in analyzer.Regions)
            {
                l(region, ": ", analyzer.Entities_By_Region(region).Count);
            }

            l();

            l("Income categories and their country count:");

            foreach (string income_group in analyzer.Income_Groups)
            {
                l(income_group, ": ", analyzer.Entities_By_Income_Group(income_group).Count);
            }

            l();

            while (true)
            {
                string user_input_region_name = prompt("Enter region name: ");

                if (analyzer.Regions.Contains(user_input_region_name))
                {
                    l("Countries in the '", user_input_region_name, "' region: ");

                    foreach (KeyValuePair <string, Entity> kvp in analyzer.Entities_By_Region(user_input_region_name))
                    {
                        l(kvp.Value.Country_Name, " (", kvp.Value.Country_Code, ")");
                    }

                    break;
                }
                else
                {
                    l("Unknown region name, the available options are: ", String.Join(", ", analyzer.Regions));
                }
            }

            l();

            while (true)
            {
                string user_input_income_category = prompt("Enter income category: ");

                if (analyzer.Income_Groups.Contains(user_input_income_category))
                {
                    l("Countries in the '", user_input_income_category, "' income category: ");

                    foreach (KeyValuePair <string, Entity> kvp in analyzer.Entities_By_Income_Group(user_input_income_category))
                    {
                        l(kvp.Value.Country_Name, " (", kvp.Value.Country_Code, ")");
                    }

                    break;
                }
                else
                {
                    l("Unknown income category, the available options are: ", String.Join(", ", analyzer.Income_Groups));
                }
            }

            l();

            while (true)
            {
                string user_input_country_or_country_code = prompt("Enter name of country or country code (Enter to quit): ");

                if (user_input_country_or_country_code == "")
                {
                    break;
                }

                Dictionary <string, Entity> results = analyzer.Entities.Where(val => val.Value.Country_Code == user_input_country_or_country_code || val.Value.Country_Name == user_input_country_or_country_code).ToDictionary(p => p.Key, p => p.Value);

                if (results.Count > 0)
                {
                    l("Data for ", user_input_country_or_country_code, ":");

                    foreach (KeyValuePair <int, double> kvp in results.First().Value.LifeExpectancyByYear)
                    {
                        l("Year: ", kvp.Key, "  Life expectancy: ", kvp.Value);
                    }
                }
                else
                {
                    l("Unknown country/country code, available options are: ", String.Join(", ", Array.ConvertAll <Entity, string>(analyzer.Entities.Values.ToArray(), Convert.ToString)));
                }
            }
        }
Ejemplo n.º 2
0
        static void ranking(LifeExpectancyAnalyzer analyzer)
        {
            while (true)
            {
                string str_year_of_interest = prompt("Enter year of interest (-1 to quit): ");

                int year_of_interest;

                if (!int.TryParse(str_year_of_interest, out year_of_interest))
                {
                    l("Error parsing year of interest, integers only please!");
                    continue;
                }

                if (year_of_interest == -1)
                {
                    break;
                }

                string user_input_region_name = prompt("Enter region (type 'all' to consider all): ");

                if (user_input_region_name != "all" && !analyzer.Regions.Contains(user_input_region_name))
                {
                    l("No such region!");
                    continue;
                }

                string user_input_income_category = prompt("Enter income category (type 'all' to consider all): ");

                if (user_input_income_category != "all" && !analyzer.Income_Groups.Contains(user_input_income_category))
                {
                    l("No such income category!");
                    continue;
                }

                Dictionary <string, Entity> countries_filtered_by_region =
                    (user_input_region_name == "all" ?
                     analyzer.Entities :
                     analyzer.Entities_By_Region(user_input_region_name));

                Dictionary <string, Entity> countries_filtered_by_income_category =
                    (user_input_income_category == "all" ?
                     countries_filtered_by_region :
                     countries_filtered_by_region.Intersect(
                         analyzer.Entities_By_Income_Group(user_input_income_category)
                         ).ToDictionary(k => k.Key, k => countries_filtered_by_region[k.Key]));

                Dictionary <string, Entity> countries_filtered_by_has_data = countries_filtered_by_income_category.Where(p => p.Value.LifeExpectancyByYear.ContainsKey(year_of_interest)).ToDictionary(p => p.Key, p => p.Value);

                Dictionary <string, Entity> sorted_countries_dict = countries_filtered_by_has_data.OrderByDescending(
                    p => p.Value.LifeExpectancyByYear[year_of_interest]
                    ).ToDictionary(p => p.Key, p => p.Value);

                List <Entity> sorted_countries = sorted_countries_dict.Values.ToList();

                l("Top 10 Life Expectancy for ", year_of_interest);

                for (int i = 0; i < (sorted_countries.Count >= 10 ? 10 : sorted_countries.Count); i++)
                {
                    l(i + 1, ": ", sorted_countries[i].Country_Name, " ", sorted_countries[i].LifeExpectancyByYear[year_of_interest]);
                }

                l();

                l("Bottom 10 Life Expectancy for ", year_of_interest);

                for (int i = sorted_countries.Count - 1; i > (sorted_countries.Count - 1) - (sorted_countries.Count >= 10 ? 10 : sorted_countries.Count); i--)
                {
                    l(i + 1, ": ", sorted_countries[i].Country_Name, " ", sorted_countries[i].LifeExpectancyByYear[year_of_interest]);
                }
            }
        }