0% found this document useful (0 votes)
35 views5 pages

Bot - Shreyasi - Assignment Solutions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views5 pages

Bot - Shreyasi - Assignment Solutions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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 = {}

for record in records:


city = record['city']

# Initialize the city's data if it doesn't exist in the aggregate dictionary


if city not in weather_aggregates:
weather_aggregates[city] = {
'temperature_sum': 0,
'humidity_sum': 0,
'temperature_count': 0,
'humidity_count': 0
}

# Accumulate temperature data if present


if 'temperature' in record and record['temperature'] is not None:
weather_aggregates[city]['temperature_sum'] += record['temperature']
weather_aggregates[city]['temperature_count'] += 1

# Accumulate humidity data if present


if 'humidity' in record and record['humidity'] is not None:
weather_aggregates[city]['humidity_sum'] += record['humidity']
weather_aggregates[city]['humidity_count'] += 1

# Compute averages for each city


result = {}
for city, data in weather_aggregates.items():
avg_temperature = (data['temperature_sum'] / data['temperature_count']
if data['temperature_count'] > 0 else None)
avg_humidity = (data['humidity_sum'] / data['humidity_count']
if data['humidity_count'] > 0 else None)
result[city] = {
'average_temperature': avg_temperature,
'average_humidity': avg_humidity
}

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.

2) Write a Python function to perform prime factorization of a given integer. The


function should return a list of tuples, where each tuple contains a prime factor
and its corresponding exponent. For example, given the integer 60, the function
should return [(2, 2), (3, 1), (5, 1)] since 60 = 2^2 * 3^1 * 5^1.
Sol: def prime_factorization(n):
factors = []
# Check for the number of 2s that divide n
count = 0
while n % 2 == 0:
n //= 2
count += 1
if count > 0:
factors.append((2, count))

# Check for odd factors starting from 3


factor = 3
while factor * factor <= n:
count = 0
while n % factor == 0:
n //= factor
count += 1
if count > 0:
factors.append((factor, count))
factor += 2

# If n is still greater than 2, it must be a prime


if n > 2:
factors.append((n, 1))

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.

Data Manipulation Challenge:

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%.

You might also like