Appearance
question:Write a python function to calculate the total loan amount remaining after multiple borrowers have made partial repayments. Each borrower starts with a specific loan amount, and partial repayments are recorded in a list of dictionaries. Each dictionary contains the borrower's ID and the repayment amount. The function should take two inputs: a dictionary where keys are borrower IDs and values are their initial loan amounts, and a list of dictionaries where each dictionary has keys "borrower_id" and "repayment" indicating the borrower's ID and the repayment amount, respectively. The function should return a dictionary with borrower IDs as keys and their remaining loan amounts as values.Input:1. A dictionary with borrower IDs as keys and their initial loan amounts as values. Example: ```python { "borrower_1": 1000, "borrower_2": 1500, "borrower_3": 2000 } ```2. A list of dictionaries, each containing a "borrower_id" key and a "repayment" key. Example: ```python [ {"borrower_id": "borrower_1", "repayment": 200}, {"borrower_id": "borrower_2", "repayment": 300}, {"borrower_id": "borrower_1", "repayment": 100}, {"borrower_id": "borrower_3", "repayment": 500} ] ```Expected Output:A dictionary with borrower IDs as keys and their remaining loan amounts as values. Example:```python{ "borrower_1": 700, "borrower_2": 1200, "borrower_3": 1500}```
answer:def calculate_remaining_loan(initial_loans, repayments): remaining_loans = initial_loans.copy() for repayment in repayments: borrower_id = repayment["borrower_id"] repayment_amount = repayment["repayment"] if borrower_id in remaining_loans: remaining_loans[borrower_id] -= repayment_amount else: print(f"Borrower {borrower_id} not found in initial loans.") return remaining_loans
question:Write a python function to calculate the total number of mantras chanted during a temple ceremony. You are given a list of tuples where each tuple contains two elements: the name of the mantra (a string) and the number of times it was chanted (an integer). Your task is to sum up all the counts of mantras and return the total number as an integer.Input: A list of tuples, where each tuple consists of a string and an integer.Output: An integer representing the total number of mantras chanted.Example:```pythonmantras = [("Gayatri", 108), ("Maha Mrityunjaya", 54), ("Gayatri", 108)]```For the above input, the expected output is `270` since 108 + 54 + 108 = 270.
answer:def total_mantras_chanted(mantras): total = 0 for mantra, count in mantras: total += count return total
question:Write a python function to calculate the average annual growth rate of a historical population dataset. The function should take a list of tuples as input, where each tuple contains a year and the population for that year (e.g., [(1800, 1000), (1850, 1200), (1900, 1500)]). The function should return a float representing the average annual growth rate as a percentage.The average annual growth rate can be calculated using the formula:[text{Average Annual Growth Rate (%)} = left( left( frac{text{End Population}}{text{Start Population}} right)^{frac{1}{text{Number of Years}}} - 1 right) times 100]Please ensure that the list is sorted by year before performing calculations. If the list is empty or contains fewer than two elements, the function should return 0.Example:Input: [(1800, 1000), (1850, 1200), (1900, 1500)]Output: 0.382This output is a rounded representation of the average annual growth rate percentage over the given period.
answer:def calculate_avg_growth_rate(population_data): # Sort the population data by year population_data.sort(key=lambda x: x[0]) # Check if the list is empty or has fewer than two elements if len(population_data) < 2: return 0.0 # Calculate the total number of years start_year, start_population = population_data[0] end_year, end_population = population_data[-1] total_years = end_year - start_year # Calculate the average annual growth rate growth_rate = (end_population / start_population) (1 / total_years) - 1 avg_growth_rate = growth_rate * 100 return round(avg_growth_rate, 3)
question:Write a python function to determine the most frequently occurring word in a given list of strings. The function should take a list of strings as input and return the word that appears most frequently. If there is a tie (i.e., multiple words appear with the same highest frequency), return any of the tied words.Input: A list of strings, where each string represents a word.Output: A string representing the most frequently occurring word.Example:```pythonwords = ["apple", "banana", "apple", "orange", "banana", "banana"]# Expected output: "banana"```Note: You can assume that the input list will always contain at least one word.
answer:def most_frequent_word(words): word_count = {} # Count the frequency of each word for word in words: word_count[word] = word_count.get(word, 0) + 1 # Find the word with the highest frequency max_freq = max(word_count.values()) most_frequent = [word for word, freq in word_count.items() if freq == max_freq] return most_frequent[0]
question:Write a python function to calculate the future value of a retirement fund given an initial investment, an annual interest rate, the number of years the investment will grow, and an optional parameter specifying additional annual contributions. The function should have the following signature:```pythondef calculate_future_value(initial_investment: float, annual_interest_rate: float, years: int, annual_contribution: float = 0) -> float: pass```Inputs:- `initial_investment`: A float representing the initial amount of money invested in the fund.- `annual_interest_rate`: A float representing the annual interest rate as a percentage (e.g., 5 for 5%).- `years`: An integer representing the number of years the money will be invested.- `annual_contribution`: An optional float representing additional money added to the fund at the end of each year. Default is 0.Expected Output:- A float representing the future value of the retirement fund after the specified number of years.Example:```python# Example usage:future_value = calculate_future_value(10000, 5, 20, 1000)print(future_value) # Expected output should be the future value of the fund after 20 years```In this example, the initial investment is 10,000 with an annual interest rate of 5%, the investment period is 20 years, and there is an additional 1,000 contributed at the end of each year.
answer:```pythondef calculate_future_value(initial_investment: float, annual_interest_rate: float, years: int, annual_contribution: float = 0) -> float: future_value = initial_investment for year in range(years): future_value = future_value * (1 + annual_interest_rate / 100) + annual_contribution return future_value```
question:Write a python function to simulate the detection of cosmic particles using a list of energy readings. The function should take a list of integers, where each integer represents the energy level of a detected particle. The function should then return a dictionary with two keys: 'count' and 'average_energy'. The 'count' key should map to the total number of particles detected (i.e., the length of the list), while the 'average_energy' key should map to the average energy of the detected particles (i.e., the sum of the energies divided by the number of particles).For example, given the input list `[100, 200, 150, 300]`, the function should return `{'count': 4, 'average_energy': 187.5}`.Please ensure the function handles the case where the input list is empty by returning `{'count': 0, 'average_energy': 0}` in such scenarios.
answer:def detect_cosmic_particles(energy_readings): particle_count = len(energy_readings) if particle_count == 0: return {'count': 0, 'average_energy': 0} total_energy = sum(energy_readings) average_energy = total_energy / particle_count return {'count': particle_count, 'average_energy': average_energy}