Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions big-countries.py
Original file line number Diff line number Diff line change
@@ -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"]]

75 changes: 75 additions & 0 deletions customers-who-never-order.py
Original file line number Diff line number Diff line change
@@ -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
57 changes: 57 additions & 0 deletions recyclable-and-low-fat-products.py
Original file line number Diff line number Diff line change
@@ -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']]