def get_subscribed_emails(table): """ Gets the list of strings with emails of people, who have subscribed to the newsletter. Args: table: lsit of lists Returns: List of strings: email + name """ names_data = common.get_values_from_column(table, 1) e_mail_data = common.get_values_from_column(table, 2) subs_info = common.get_values_from_column(table, 3) records_amount = len(table) row_number_of_sub_customers = [] subscription_score = [] for i in range(records_amount): if int(subs_info[i]) > 0: row_number_of_sub_customers.append(i) for i in row_number_of_sub_customers: subscription_score.append(e_mail_data[i] + ';' + names_data[i]) return subscription_score
def get_persons_closest_to_average(table): """ Counts average birth year of all people from the table. Gets person (or people) who were born closest to the average birth year. Args: table: list of lists with data Returns: List with names (strings) of people closest to the average birth year """ years_list = common.get_values_from_column(table, 2, "int") average_year = common.get_average_value(years_list) lowest_difference = float("inf") for i in range(len(table)): difference = (int(table[i][2]) - average_year) if abs(difference) < lowest_difference: lowest_difference = abs(difference) closest_value = table[i][2] closest_people = [ table[i][1] for i in range(len(table)) if table[i][2] == closest_value ] return closest_people
def get_lowest_price_item_id(table): """ Gets id of item with the lowest price. If there are two items with the same, lowest price, returns first alphabetically. Args: table: list of lists with data Returns: String """ prices_list = common.get_values_from_column(table, 2, "int") sorted_prices_list = common.insertion_sorting(prices_list) lowest_price_games = [ table[i][1] for i in range(len(table)) if int(table[i][2]) == sorted_prices_list[0] ] first_alphabetically = get_first_alphabetically(lowest_price_games) for i in range(len(table)): if first_alphabetically == table[i][1]: item = table[i][0] return item
def get_longest_name_id(table): """ Gets the id of the longest name. If there are more than one longest names, sorts the alphabetically and gets the first one. Args: table: list of lists Returns: string: id of the longest name """ names_data = common.get_values_from_column(table, 1) id_data = common.get_values_from_column(table, 0) longest_string, rows = find_longest_string_in_list(names_data, True) if len(rows) > 1: alphabetical_array = [] for i in range(65, 91): alphabetical_array.append([]) for row_number in rows: for alpha in range(65, 91): if names_data[row_number][0].upper() == chr(alpha): alphabetical_array[alpha - 65].extend( [[names_data[row_number], row_number]]) for first_name in alphabetical_array: if first_name: id_to_return = id_data[first_name[0][1]] break else: id_to_return = id_data[rows[0]] return id_to_return
def add(table): """ Asks user for input and adds it into the table. Args: table: table to add new record to Returns: Table with a new record """ id_storage = common.get_values_from_column(table, 0) id_ = common.generate_random(table) table = manage_data_from_user(table, id_storage, id_, False) return table
def get_oldest_person(table): """ Gets the oldest people from the table. Args: table: list of lists with data Returns: List with names (strings) of the oldest people """ years_list = common.get_values_from_column(table, 2, "int") sorted_years_list = common.insertion_sorting(years_list) oldest_people = [ table[i][1] for i in range(len(table)) if int(table[i][2]) == sorted_years_list[0] ] return oldest_people
def update(table, id_): """ Updates specified record in the table. Ask users for new data. Args: table: list in which record should be updated id_ (str): id of a record to update Returns: table with updated record """ id_storage = common.get_values_from_column(table, 0) if id_ in id_storage: table = manage_data_from_user(table, id_storage, id_, True) # Here u can make changes: else: ui.print_error_message('This option does not exist.') return table