Bot - Shreyasi - Assignment Solutions
Bot - Shreyasi - Assignment Solutions
1) Write a Python function that takes a list of records, where each record is a
dictionary containing the name of a city and various types of weather data (e.g.,
temperature, humidity). The function should aggregate this data to provide insights
such as the average temperature and humidity for each city. However, each record
may not contain all types of data. The solution should gracefully handle missing
data
Sol: def aggregate_weather_data(records):
# Initialize dictionaries to store cumulative sums and counts for each city
weather_aggregates = {}
return result
Example input:
weather_records = [
{'city': 'New York', 'temperature': 22, 'humidity': 65},
{'city': 'Los Angeles', 'temperature': 25},
{'city': 'New York', 'temperature': 21, 'humidity': 60},
{'city': 'Los Angeles', 'humidity': 70},
{'city': 'Chicago', 'temperature': 18, 'humidity': 80},
{'city': 'Chicago', 'temperature': 20}
]
print(aggregate_weather_data(weather_records))
output:
{
'New York': {'average_temperature': 21.5, 'average_humidity': 62.5},
'Los Angeles': {'average_temperature': 25.0, 'average_humidity': 70.0},
'Chicago': {'average_temperature': 19.0, 'average_humidity': 80.0}
}
Explaination:
• The function handles missing data (e.g., a record may not have a temperature or
humidity value).
• It calculates the sum and count for each type of data (temperature, humidity),
and then computes the averages based on available data.
return factors
Examples:
print(prime_factorization(60)) # Output: [(2, 2), (3, 1), (5, 1)]
print(prime_factorization(84)) # Output: [(2, 2), (3, 1), (7, 1)]
Explanation:
• The function first handles the factor 2 separately, as it's the only even prime.
• Then, it iterates over odd numbers starting from 3 to check for other prime
factors.
• The process continues until factor * factor exceeds the value of n. If n is still
greater than 2 at the end, it means n itself is prime and is added to the result.
3) Scenario: A table named products contains columns id, name, and price.
Task: Write a SQL query to increase the price of all products by 10% and
display the new prices along with the product names.
Sol: SQL query to increase the price of all products by 10% and display the new
prices along with the product names:
SELECT
name,
price * 1.10 AS new_price
FROM
products;
Explanation:
• SELECT name, price * 1.10 AS new_price: Selects the name of the product and
calculates the new price by multiplying the price by 1.10 (which represents a
10% increase). The result is aliased as new_price.
• FROM products: Specifies the table products to retrieve the data from.
This query only calculates the new price and displays it without updating the original
values in the table. To update permanently update the prices in the table, use the
following UPDATE query:
UPDATE products
SET price = price * 1.10;
This query modifies the price column in the products table by increasing it by 10%.