Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

please fix this code (python) to give the same following output def break_apart(my_string, my_delimiter): split_string=my_string.split(my_delimiter) return split_string # Program to find most frequent # element

please fix this code (python) to give the same following output

image text in transcribed

def break_apart(my_string, my_delimiter): split_string=my_string.split(my_delimiter) return split_string

# Program to find most frequent # element in a list # Figure out the most spoken language in each region. # for most frequent data in laung def most_frequent(list): dict_temp = {}#empty dictionary # for loop to iterate every list in list for i in list: # if key persent in dictionary increment else add key if i[3] in dict_temp.keys(): dict_temp[i[3]] += 1 else: dict_temp[i[3]] = 1

# printing dictionary value for key,value in dict_temp.items(): print(f"{key} : {value}")

# reading data from file file_data = open("data_short.txt","r") file_data_as_list =[] # converting to list of lists for i in file_data.readlines(): i = i.strip() file_data_as_list.append(i.split(","))

most_frequent(file_data_as_list)

#Function to identify all the unique regions in the .txt file. def unique_regions(list_of_all_countries_with_data): regions = set() for item in list_of_all_countries_with_data: item_parts = item.strip().split(',') region = item_parts[0] regions.add(region) regions = list(regions) return regions

print(unique_regions(l))

#Function that finds the total population of a given region def population_count_for_a_region(region,list_of_all_countries_with_data): population = 0 for item in list_of_all_countries_with_data: item_parts = item.strip().split(',') item_region = item_parts[0] if region == item_region: population += int(item_parts[1]) return population

#Function that finds the total area of a given region def area_count_for_a_region(region,list_of_all_countries_with_data): area = 0 for item in list_of_all_countries_with_data: item_parts = item.strip().split(',') item_region = item_parts[0] if region == item_region: area += int(item_parts[2]) return area

#Function that finds list of all languages and the most spoken language (most spoken language for extra credit) def list_of_all_languages(region, list_of_all_countries_with_data): languages = list() for item in list_of_all_countries_with_data: item_parts = item.strip().split(',') language = item_parts[3] languages.append(language) return languages

if __name__ == "__main__": file_name = input('What file do you want to parse?: ')

try: with open(file_name) as my_file: list_of_all_lines = my_file.readlines() except: print('File not found.') sys.exit(1)

Your program is to calculate the continent information based on an input file. Each input file contains the meta-information in the following order. 1) Name of country 2) Population of country 3) Area of country 4) Main language 5) Continent For example, the data_short.txt as input looks like: Latvia, 1991888, 64559, lv, Europe South Sudan , 11384393,619745,en, Africa Eeypt, 87668108, 1882450, ar, Africa Guinea-Bissau, 1746080, 36125,pt, Africa Zimbabwe, 13061239, 390757, en, Africa Mali,15768000, 1240192, fr, Africa Portugal, 10477888,92890, pt, Europe New Zealand, 4547980, 270467, en, Oceania Croatia, 4267558,56594, hr, Furope Nauru, 18984,21, en, Oceania Estonia, 1315819,45227,et, Europe Libya, 6253800,1759540, ar, Africa Sierra Leone,6285898,71740, en, Africa Denmark, 5655750,43894, da, Europe Tuvalu, 11323,26,en, Oceania Chad, 13211800,1284000, fr, Africa Tonga, 103252,747,en, Oceania Niger, 17138787,1267000, fr, Africa Benin, 9988668, 112622,fr, Africa Penguin Land,6,14200999, penguin_language, Antarctica what file do you want to parse?: data_short.txt Name of Continent: Africa Population: 182423507 Area: 7784171 output List of All Languages: ['ar', 'en', 'fr', 'pt'] Most spoken language: fr Name of Continent: Antarctica Population: @ Area: 14200000 List of All Languages: ['penguin_language'] Most spoken language: penguin_language Name of Continent: Europe Population: 23708727 Area: 301564 List of All Languages: ['pt', 'lv', 'hr', 'et', 'da'] Most spoken language: pt Name of Continent: Oceania Population: 4672559 Area: 271261 List of All Languages: ['en') Most spoken language: en

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Successful Keyword Searching Initiating Research On Popular Topics Using Electronic Databases

Authors: Randall MacDonald, Susan MacDonald

1st Edition

0313306761, 978-0313306761

More Books

Students also viewed these Databases questions