Appearance
question:Write a python function to analyze a list of medication side effects and determine which side effect occurs most frequently. The input will be a list of strings, where each string represents a reported side effect. The function should return a tuple containing the most common side effect and its frequency. If there is a tie, return any one of the most common side effects.Input:- A list of strings, where each string is a side effect.Output:- A tuple containing the most common side effect (string) and its frequency (integer).Example:```pythonside_effects = ["nausea", "headache", "dizziness", "nausea", "fatigue", "headache", "nausea"]# Expected output: ("nausea", 3)```
answer:def analyze_side_effects(side_effects): side_effect_counts = {} # Count the occurrences of each side effect for effect in side_effects: if effect in side_effect_counts: side_effect_counts[effect] += 1 else: side_effect_counts[effect] = 1 # Find the most common side effect and its frequency most_common_effect = None max_frequency = 0 for effect, frequency in side_effect_counts.items(): if frequency > max_frequency: most_common_effect = effect max_frequency = frequency return (most_common_effect, max_frequency)
question:Write a python function to calculate the average number of community service hours per volunteer, given a list of tuples representing the volunteers and their respective hours. Each tuple contains the name of the volunteer as a string and their hours as an integer. The function should return a dictionary where the keys are the volunteer names and the values are their average hours, rounded to two decimal places. If a volunteer appears multiple times in the list, consider all their hours for the average calculation.Input: A list of tuples where each tuple contains a string and an integer. For example: `[('Alice', 10), ('Bob', 15), ('Alice', 5)]`Output: A dictionary where each key is a volunteer's name and the value is their average service hours. For example: `{'Alice': 7.5, 'Bob': 15.0}`Note: Assume that the list contains at least one volunteer and no invalid data.
answer:def calculate_average_hours(volunteer_hours): volunteer_dict = {} for name, hours in volunteer_hours: if name in volunteer_dict: volunteer_dict[name].append(hours) else: volunteer_dict[name] = [hours] average_hours = {} for name, hours_list in volunteer_dict.items(): total_hours = sum(hours_list) num_entries = len(hours_list) average_hours[name] = round(total_hours / num_entries, 2) return average_hours
question:Write a python function to calculate the total time I spend commuting each week, given a list of daily commute times. I wake up early every day to avoid traffic, so my commute times are generally consistent. The function should take a list of integers as input, where each integer represents the commute time in minutes for a single day. The function should return an integer representing the total commute time for the week.Input: A list of integers, where each integer is the commute time in minutes for one day. The list will have exactly 5 elements, representing Monday to Friday.Output: An integer representing the total commute time for the week.Example:```python# Example input: [30, 28, 32, 31, 29] (minutes for Monday to Friday)# Example output: 150```Note: Assume the input list will always contain exactly 5 integers, each representing a valid commute time in minutes.
answer:def calculate_weekly_commute_time(commute_times): total_time = 0 for time in commute_times: total_time += time return total_time
question:Write a python function to calculate the average time it takes for people to evacuate a building during a drill. I have a list of tuples, where each tuple contains the name of the person and the time (in seconds) they took to evacuate. The function should return the average evacuation time rounded to two decimal places.Input: A list of tuples, e.g. `[('Alice', 120), ('Bob', 150), ('Charlie', 90)]`.Output: A float representing the average evacuation time, rounded to two decimal places.Example:```pythonevacuation_times = [('Alice', 120), ('Bob', 150), ('Charlie', 90)]print(calculate_average_evacuation_time(evacuation_times))```Expected output:```120.00```
answer:def calculate_average_evacuation_time(evacuation_times): total_time = 0 num_people = len(evacuation_times) for person, time in evacuation_times: total_time += time average_time = total_time / num_people return round(average_time, 2)
question:Write a python function to calculate the Body Mass Index (BMI) given a list of tuples where each tuple contains a person's weight in kilograms and height in meters. The function should return a list of tuples, where each tuple contains the original input and the calculated BMI, rounded to two decimal places. Input:- A list of tuples. Each tuple contains two floats: the first float is the weight in kilograms and the second float is the height in meters. For example: `[(70, 1.75), (60, 1.6)]`.Output:- A list of tuples. Each tuple should contain the original weight and height values followed by the calculated BMI, rounded to two decimal places. For example: `[(70, 1.75, 22.86), (60, 1.6, 23.44)]`.Example:```python# Inputpeople = [(70, 1.75), (60, 1.6)]# Expected Output# [(70, 1.75, 22.86), (60, 1.6, 23.44)]```
answer:Here's the Python function to calculate the BMI for a list of tuples containing weight and height:```pythondef calculate_bmi(people): bmi_list = [] for weight, height in people: bmi = weight / (height 2) bmi_list.append((weight, height, round(bmi, 2))) return bmi_list```This function takes a list of tuples `people` as input, where each tuple contains a person's weight in kilograms and height in meters. It iterates over the list using a `for` loop, calculating the BMI for each person using the formula `weight / (height 2)`. The calculated BMI is rounded to two decimal places using the `round()` function and appended to a new tuple along with the original weight and height values. Finally, the function returns a list of these new tuples containing the weight, height, and BMI.
question:Write a python function to analyze dialogue lines from a TV drama script and identify which character mentions a specific social issue most frequently. The function should take two inputs: a list of tuples, where each tuple contains a character's name and their line of dialogue (e.g., `("Character A", "We need to talk about climate change.")`), and a string representing the social issue keyword to search for (e.g., "climate change"). The function should return the name of the character who mentions the keyword the most times. If no character mentions the keyword, return `None`. If there's a tie, return a list of character names who are tied.Example Input:```pythondialogues = [ ("Alice", "Climate change is real."), ("Bob", "We need to address climate change."), ("Alice", "Climate change affects everyone."), ("Charlie", "I think climate change is the most pressing issue."), ("Bob", "Have you read the latest report on climate change?")]keyword = "climate change"```Expected Output:```python"Bob"```In the example, Bob mentions "climate change" in two of his lines, which is more than any other character.
answer:def analyze_dialogues(dialogues, keyword): character_mentions = {} # Count mentions of keyword for each character for character, line in dialogues: mentions = line.lower().split().count(keyword.lower()) character_mentions[character] = character_mentions.get(character, 0) + mentions # Find the character(s) with the most mentions max_mentions = max(character_mentions.values()) if character_mentions else 0 top_characters = [character for character, mentions in character_mentions.items() if mentions == max_mentions] # Return the result based on the number of top characters if len(top_characters) == 1: return top_characters[0] elif top_characters: return top_characters else: return None