diff --git a/big-countries.py b/big-countries.py new file mode 100644 index 0000000..3ef341b --- /dev/null +++ b/big-countries.py @@ -0,0 +1,60 @@ +""" + +Table: World + ++-------------+---------+ +| Column Name | Type | ++-------------+---------+ +| name | varchar | +| continent | varchar | +| area | int | +| population | int | +| gdp | bigint | ++-------------+---------+ +name is the primary key (column with unique values) for this table. +Each row of this table gives information about the name of a country, the continent to which it belongs, its area, the population, and its GDP value. + + +A country is big if: + +it has an area of at least three million (i.e., 3000000 km2), or +it has a population of at least twenty-five million (i.e., 25000000). +Write a solution to find the name, population, and area of the big countries. + +Return the result table in any order. + +The result format is in the following example. + + +Example 1: + +Input: +World table: ++-------------+-----------+---------+------------+--------------+ +| name | continent | area | population | gdp | ++-------------+-----------+---------+------------+--------------+ +| Afghanistan | Asia | 652230 | 25500100 | 20343000000 | +| Albania | Europe | 28748 | 2831741 | 12960000000 | +| Algeria | Africa | 2381741 | 37100000 | 188681000000 | +| Andorra | Europe | 468 | 78115 | 3712000000 | +| Angola | Africa | 1246700 | 20609294 | 100990000000 | ++-------------+-----------+---------+------------+--------------+ +Output: ++-------------+------------+---------+ +| name | population | area | ++-------------+------------+---------+ +| Afghanistan | 25500100 | 652230 | +| Algeria | 37100000 | 2381741 | ++-------------+------------+---------+ + +""" + +# This function filters countries based on their area and population thresholds. +# It selects countries with an area of at least 3,000,000 square kilometers or a population of at least 25,000,000. +# The resulting DataFrame contains only the 'name', 'population', and 'area' columns. + +import pandas as pd + +def big_countries(world: pd.DataFrame) -> pd.DataFrame: + return world[(world["area"] >= 3000000) | (world["population"] >= 25000000)][["name", "population", "area"]] + diff --git a/customers-who-never-order.py b/customers-who-never-order.py new file mode 100644 index 0000000..3fd829a --- /dev/null +++ b/customers-who-never-order.py @@ -0,0 +1,75 @@ +""" + +Table: Customers + ++-------------+---------+ +| Column Name | Type | ++-------------+---------+ +| id | int | +| name | varchar | ++-------------+---------+ +id is the primary key (column with unique values) for this table. +Each row of this table indicates the ID and name of a customer. + + +Table: Orders + ++-------------+------+ +| Column Name | Type | ++-------------+------+ +| id | int | +| customerId | int | ++-------------+------+ +id is the primary key (column with unique values) for this table. +customerId is a foreign key (reference columns) of the ID from the Customers table. +Each row of this table indicates the ID of an order and the ID of the customer who ordered it. + + +Write a solution to find all customers who never order anything. + +Return the result table in any order. + +The result format is in the following example. + + + +Example 1: + +Input: + +Customers table: ++----+-------+ +| id | name | ++----+-------+ +| 1 | Joe | +| 2 | Henry | +| 3 | Sam | +| 4 | Max | ++----+-------+ +Orders table: ++----+------------+ +| id | customerId | ++----+------------+ +| 1 | 3 | +| 2 | 1 | ++----+------------+ + +Output: ++-----------+ +| Customers | ++-----------+ +| Henry | +| Max | ++-----------+ + +""" + +# This function identifies customers who have never placed an order by performing a left join between the customers and orders tables. +# It filters out customers whose 'id' does not match any 'customerId' in the orders table (i.e., those with NaN values in 'customerId'). +# Finally, it selects only the 'name' column, renaming it to 'Customers', and returns the resulting DataFrame. + +def find_customers(customers: pd.DataFrame, orders: pd.DataFrame) -> pd.DataFrame: + df = customers.merge(orders, left_on='id', right_on='customerId', how='left') + df = df[df['customerId'].isna()] + df = df[['name']].rename(columns={'name': 'Customers'}) + return df diff --git a/recyclable-and-low-fat-products.py b/recyclable-and-low-fat-products.py new file mode 100644 index 0000000..cb74f67 --- /dev/null +++ b/recyclable-and-low-fat-products.py @@ -0,0 +1,57 @@ +""" + +Table: Products + ++-------------+---------+ +| Column Name | Type | ++-------------+---------+ +| product_id | int | +| low_fats | enum | +| recyclable | enum | ++-------------+---------+ +product_id is the primary key (column with unique values) for this table. +low_fats is an ENUM (category) of type ('Y', 'N') where 'Y' means this product is low fat and 'N' means it is not. +recyclable is an ENUM (category) of types ('Y', 'N') where 'Y' means this product is recyclable and 'N' means it is not. + + +Write a solution to find the ids of products that are both low fat and recyclable. + +Return the result table in any order. + +The result format is in the following example. + + +Example 1: + +Input: +Products table: ++-------------+----------+------------+ +| product_id | low_fats | recyclable | ++-------------+----------+------------+ +| 0 | Y | N | +| 1 | Y | Y | +| 2 | N | Y | +| 3 | Y | Y | +| 4 | N | N | ++-------------+----------+------------+ +Output: ++-------------+ +| product_id | ++-------------+ +| 1 | +| 3 | ++-------------+ +Explanation: Only products 1 and 3 are both low fat and recyclable. + +""" + +# Approach: +# 1. Filter the DataFrame to include only rows where 'low_fats' is 'Y' and 'recyclable' is 'Y'. +# 2. Select only the 'product_id' column from the filtered DataFrame. +# 3. Return the resulting DataFrame containing product IDs that meet both conditions. + +import pandas as pd + +def find_products(products: pd.DataFrame) -> pd.DataFrame: + df = products[(products['low_fats'] == 'Y') & (products['recyclable'] == 'Y')] + return df[['product_id']]