Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import csv
- import re
- import sys
- from collections import (
- Counter,
- defaultdict,
- )
- LANGUAGE_REGEXP = re.compile(r'\((.+)\)')
- COUNTRY_REGEXP = re.compile(r'(\w{2,})/')
- def get_country(text):
- found = COUNTRY_REGEXP.search(text)
- return found.group(1) if found else found
- def get_email(text):
- return text.rsplit('/', 1)[0]
- def get_income(text):
- if text:
- return float(text.rsplit('/', 1)[-1])
- return 0
- def get_language(text):
- found = LANGUAGE_REGEXP.search(text)
- return found.group(1) if found else found
- def question_1(rows):
- return Counter(map(get_language, rows[0].keys())).most_common(1)
- def question_2(rows):
- persons = set()
- for row in rows:
- persons.update(
- {
- get_email(person) for key, person in row.items()
- if person and get_language(key).lower() == 'python'
- }
- )
- return len(persons)
- def question_3(rows):
- income_per_language = defaultdict(int)
- for row in rows:
- for key, data in row.items():
- if data:
- income_per_language[get_language(key)] += get_income(data)
- return max(
- (income, language) for language, income in income_per_language.items()
- )[-1]
- def question_4(rows):
- cost_per_country = defaultdict(int)
- for row in rows:
- for key, data in row.items():
- if data:
- cost_per_country[get_country(data)] += get_income(data)
- return max(
- (cost, country) for country, cost in cost_per_country.items()
- )[-1]
- if __name__ == '__main__':
- with open(sys.argv[1], 'r') as f:
- csv_dict = csv.DictReader(f, delimiter=';')
- rows = list(csv_dict)
- print(f'1) {question_1(rows)}')
- print(f'2) {question_2(rows)}')
- print(f'3) {question_3(rows)}')
- print(f'4) {question_4(rows)}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement