Appearance
question:Write a python function to categorize different types of tea based on their health benefits. The function should take a list of dictionaries as input, where each dictionary contains the name of the tea and a list of its associated health benefits. The output should be a dictionary where the keys are the unique health benefits and the values are lists of tea names that provide those benefits.Input:- A list of dictionaries, each with the following structure: ```python [ {"name": "Green Tea", "benefits": ["boosts metabolism", "rich in antioxidants"]}, {"name": "Black Tea", "benefits": ["improves heart health", "rich in antioxidants"]}, {"name": "Chamomile Tea", "benefits": ["aids sleep", "reduces stress"]}, # more tea entries... ] ```Expected Output:- A dictionary where each key is a unique health benefit, and the value is a list of tea names that offer this benefit. For example: ```python { "boosts metabolism": ["Green Tea"], "rich in antioxidants": ["Green Tea", "Black Tea"], "improves heart health": ["Black Tea"], "aids sleep": ["Chamomile Tea"], "reduces stress": ["Chamomile Tea"] } ```
answer:def categorize_teas(tea_list): benefit_dict = {} for tea in tea_list: name = tea["name"] benefits = tea["benefits"] for benefit in benefits: if benefit in benefit_dict: benefit_dict[benefit].append(name) else: benefit_dict[benefit] = [name] return benefit_dict
question:Write a Python function to calculate the expected monetary value (EMV) of a natural resource contract, given a list of possible outcomes with their associated probabilities and monetary values. The function should take two arguments: a list of tuples where each tuple contains two elements, a probability (a float between 0 and 1) and a monetary value (an integer or float). The function should return the EMV, which is calculated as the sum of the products of each probability and its corresponding monetary value.Input:- A list of tuples, each containing: - A probability (float) - A monetary value (int or float)Output:- A float representing the expected monetary value.Example:```python# Example inputoutcomes = [(0.3, 100000), (0.5, 200000), (0.2, -50000)]# Expected output# EMV = (0.3 * 100000) + (0.5 * 200000) + (0.2 * -50000) = 125000.0```Please ensure your function handles the case where the sum of probabilities does not equal 1 by normalizing them before calculating the EMV.
answer:def calculate_emv(outcomes): total_prob = sum(p for p, _ in outcomes) if total_prob != 1: outcomes = [(p / total_prob, v) for p, v in outcomes] emv = sum(p * v for p, v in outcomes) return emv
question:Write a python function to calculate the savings in fuel costs when switching from a traditional gasoline car to a hybrid car over a given distance. The function should take the following inputs:1. `distance`: The total distance to be traveled in miles (an integer).2. `gasoline_mpg`: The miles per gallon (mpg) efficiency of the gasoline car (a float).3. `hybrid_mpg`: The miles per gallon (mpg) efficiency of the hybrid car (a float).4. `gas_price`: The price of one gallon of gasoline in dollars (a float).The function should return the savings in dollars as a float, rounded to two decimal places. The savings are calculated as the difference in fuel costs between the gasoline car and the hybrid car over the specified distance.Example:```pythoncalculate_savings(1000, 25.0, 50.0, 3.50)```Expected Output:```70.00```This output represents the savings of 70.00 when traveling 1000 miles using a hybrid car with 50.0 mpg compared to a gasoline car with 25.0 mpg, given the gas price is 3.50 per gallon.
answer:def calculate_savings(distance, gasoline_mpg, hybrid_mpg, gas_price): gasoline_gallons = distance / gasoline_mpg hybrid_gallons = distance / hybrid_mpg gasoline_cost = gasoline_gallons * gas_price hybrid_cost = hybrid_gallons * gas_price savings = gasoline_cost - hybrid_cost return round(savings, 2)
question:Write a python function to format product prices on an e-commerce platform. The function should take a list of prices as input and return a list of strings, where each price is formatted to two decimal places and prefixed with a dollar sign. Assume the input is a list of floats representing the prices of different products. The expected output is a list of strings where each price is formatted as a currency string. Example:Input:```python[19.99, 5, 100.5, 3.259]```Expected Output:```python['19.99', '5.00', '100.50', '3.26']```
answer:def format_prices(prices): formatted_prices = [] for price in prices: formatted_price = "{:.2f}".format(price) formatted_prices.append(formatted_price) return formatted_prices
question:Write a python function to simulate a simple customer service interaction. The function should take as input a string representing a customer's query and a dictionary mapping common queries to canned responses. The function should return the appropriate canned response if the query matches one of the keys in the dictionary. If the query does not match any of the keys, the function should return a default response like "I'm sorry, I don't have the information you're looking for."Input:- A string `query` representing the customer's question.- A dictionary `responses` where keys are common query strings and values are the corresponding response strings.Output:- A string representing the response to the customer's query.Example:```pythonquery = "What are your business hours?"responses = { "What are your business hours?": "Our business hours are from 9 AM to 5 PM, Monday to Friday.", "How can I reset my password?": "To reset your password, please click on 'Forgot Password' on the login page."}# Expected output: "Our business hours are from 9 AM to 5 PM, Monday to Friday."```
answer:def customer_service(query, responses): for key in responses: if query == key: return responses[key] return "I'm sorry, I don't have the information you're looking for."
question:Write a Python function to calculate the average session duration from a list of session durations. Each session duration is given in seconds. The function should take a list of integers as input, where each integer represents the session duration in seconds. The output should be a float representing the average session duration in seconds.Example:Input: [120, 150, 180, 200]Output: 162.5Note: Ensure that the function handles an empty list case by returning 0.0.
answer:def calculate_average_session_duration(session_durations): if not session_durations: return 0.0 total_duration = sum(session_durations) num_sessions = len(session_durations) average_duration = total_duration / num_sessions return average_duration