From d2b2d654bff6c7d8d759c5ca2490c6cdf8f576bd Mon Sep 17 00:00:00 2001 From: Rhitik Date: Tue, 24 Oct 2023 20:26:51 +0530 Subject: [PATCH 01/18] Solution for 185. --- .../README.md | 25 +++++++++++++++++++ .../README_EN.md | 25 +++++++++++++++++++ .../Solution.py | 20 +++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 solution/0100-0199/0185.Department Top Three Salaries/Solution.py diff --git a/solution/0100-0199/0185.Department Top Three Salaries/README.md b/solution/0100-0199/0185.Department Top Three Salaries/README.md index 05684ff88ead6..be782f51fd333 100644 --- a/solution/0100-0199/0185.Department Top Three Salaries/README.md +++ b/solution/0100-0199/0185.Department Top Three Salaries/README.md @@ -139,4 +139,29 @@ FROM WHERE rk <= 3; ``` +### **Pandas** + +```python +import pandas as pd + + +def top_three_salaries( + employee: pd.DataFrame, department: pd.DataFrame +) -> pd.DataFrame: + salary_cutoff = ( + employee.drop_duplicates(["salary", "departmentId"]) + .groupby("departmentId")["salary"] + .nlargest(3) + .groupby("departmentId") + .min() + ) + employee["Department"] = department.set_index("id")["name"][ + employee["departmentId"] + ].values + employee["cutoff"] = salary_cutoff[employee["departmentId"]].values + return employee[employee["salary"] >= employee["cutoff"]].rename( + columns={"name": "Employee", "salary": "Salary"} + )[["Department", "Employee", "Salary"]] + +``` diff --git a/solution/0100-0199/0185.Department Top Three Salaries/README_EN.md b/solution/0100-0199/0185.Department Top Three Salaries/README_EN.md index 70e1a60f4099c..a6f0736d831a4 100644 --- a/solution/0100-0199/0185.Department Top Three Salaries/README_EN.md +++ b/solution/0100-0199/0185.Department Top Three Salaries/README_EN.md @@ -135,4 +135,29 @@ FROM WHERE rk <= 3; ``` +### **Pandas** + +```python +import pandas as pd + + +def top_three_salaries( + employee: pd.DataFrame, department: pd.DataFrame +) -> pd.DataFrame: + salary_cutoff = ( + employee.drop_duplicates(["salary", "departmentId"]) + .groupby("departmentId")["salary"] + .nlargest(3) + .groupby("departmentId") + .min() + ) + employee["Department"] = department.set_index("id")["name"][ + employee["departmentId"] + ].values + employee["cutoff"] = salary_cutoff[employee["departmentId"]].values + return employee[employee["salary"] >= employee["cutoff"]].rename( + columns={"name": "Employee", "salary": "Salary"} + )[["Department", "Employee", "Salary"]] + +``` diff --git a/solution/0100-0199/0185.Department Top Three Salaries/Solution.py b/solution/0100-0199/0185.Department Top Three Salaries/Solution.py new file mode 100644 index 0000000000000..df6be5a83d417 --- /dev/null +++ b/solution/0100-0199/0185.Department Top Three Salaries/Solution.py @@ -0,0 +1,20 @@ +import pandas as pd + + +def top_three_salaries( + employee: pd.DataFrame, department: pd.DataFrame +) -> pd.DataFrame: + salary_cutoff = ( + employee.drop_duplicates(["salary", "departmentId"]) + .groupby("departmentId")["salary"] + .nlargest(3) + .groupby("departmentId") + .min() + ) + employee["Department"] = department.set_index("id")["name"][ + employee["departmentId"] + ].values + employee["cutoff"] = salary_cutoff[employee["departmentId"]].values + return employee[employee["salary"] >= employee["cutoff"]].rename( + columns={"name": "Employee", "salary": "Salary"} + )[["Department", "Employee", "Salary"]] From bd8bdd15828d0ae2b12ba37ba5e459b74afc5ad9 Mon Sep 17 00:00:00 2001 From: nrhitik Date: Tue, 24 Oct 2023 14:58:55 +0000 Subject: [PATCH 02/18] style: format code and docs with prettier --- solution/0100-0199/0185.Department Top Three Salaries/README.md | 1 + .../0100-0199/0185.Department Top Three Salaries/README_EN.md | 1 + 2 files changed, 2 insertions(+) diff --git a/solution/0100-0199/0185.Department Top Three Salaries/README.md b/solution/0100-0199/0185.Department Top Three Salaries/README.md index be782f51fd333..814688a39ccab 100644 --- a/solution/0100-0199/0185.Department Top Three Salaries/README.md +++ b/solution/0100-0199/0185.Department Top Three Salaries/README.md @@ -164,4 +164,5 @@ def top_three_salaries( )[["Department", "Employee", "Salary"]] ``` + diff --git a/solution/0100-0199/0185.Department Top Three Salaries/README_EN.md b/solution/0100-0199/0185.Department Top Three Salaries/README_EN.md index a6f0736d831a4..7923dd975a7d1 100644 --- a/solution/0100-0199/0185.Department Top Three Salaries/README_EN.md +++ b/solution/0100-0199/0185.Department Top Three Salaries/README_EN.md @@ -160,4 +160,5 @@ def top_three_salaries( )[["Department", "Employee", "Salary"]] ``` + From 655ee0fa5ef4ce0ade88c9ea7c45a40fa8cea9e6 Mon Sep 17 00:00:00 2001 From: Rhitik Date: Tue, 24 Oct 2023 20:47:32 +0530 Subject: [PATCH 03/18] Solution for 196. --- .../0196.Delete Duplicate Emails/README.md | 15 +++++++++++++++ .../0196.Delete Duplicate Emails/README_EN.md | 15 +++++++++++++++ .../0196.Delete Duplicate Emails/Solution.py | 9 +++++++++ 3 files changed, 39 insertions(+) create mode 100644 solution/0100-0199/0196.Delete Duplicate Emails/Solution.py diff --git a/solution/0100-0199/0196.Delete Duplicate Emails/README.md b/solution/0100-0199/0196.Delete Duplicate Emails/README.md index f8f5d6ad31927..69ac60b8cb180 100644 --- a/solution/0100-0199/0196.Delete Duplicate Emails/README.md +++ b/solution/0100-0199/0196.Delete Duplicate Emails/README.md @@ -97,4 +97,19 @@ WHERE p1.id < p2.id; ``` +### **Python** + +```python +import pandas as pd + + +# Modify Person in place +def delete_duplicate_emails(person: pd.DataFrame) -> None: + # Sort the rows based on id (Ascending order) + person.sort_values(by="id", ascending=True, inplace=True) + # Drop the duplicates based on email. + person.drop_duplicates(subset="email", keep="first", inplace=True) + +``` + diff --git a/solution/0100-0199/0196.Delete Duplicate Emails/README_EN.md b/solution/0100-0199/0196.Delete Duplicate Emails/README_EN.md index a56f075b00e85..ccef471e1ca44 100644 --- a/solution/0100-0199/0196.Delete Duplicate Emails/README_EN.md +++ b/solution/0100-0199/0196.Delete Duplicate Emails/README_EN.md @@ -93,4 +93,19 @@ WHERE p1.id < p2.id; ``` +### **Python** + +```python +import pandas as pd + + +# Modify Person in place +def delete_duplicate_emails(person: pd.DataFrame) -> None: + # Sort the rows based on id (Ascending order) + person.sort_values(by="id", ascending=True, inplace=True) + # Drop the duplicates based on email. + person.drop_duplicates(subset="email", keep="first", inplace=True) + +``` + diff --git a/solution/0100-0199/0196.Delete Duplicate Emails/Solution.py b/solution/0100-0199/0196.Delete Duplicate Emails/Solution.py new file mode 100644 index 0000000000000..50279ed1fd1ec --- /dev/null +++ b/solution/0100-0199/0196.Delete Duplicate Emails/Solution.py @@ -0,0 +1,9 @@ +import pandas as pd + + +# Modify Person in place +def delete_duplicate_emails(person: pd.DataFrame) -> None: + # Sort the rows based on id (Ascending order) + person.sort_values(by="id", ascending=True, inplace=True) + # Drop the duplicates based on email. + person.drop_duplicates(subset="email", keep="first", inplace=True) From 4f1367d9a20c66080880a03aab7e7d82b6f9eda6 Mon Sep 17 00:00:00 2001 From: Rhitik V Nimbalkar <74248352+nrhitik@users.noreply.github.com> Date: Wed, 25 Oct 2023 10:01:53 +0530 Subject: [PATCH 04/18] Update README_EN.md --- solution/0100-0199/0196.Delete Duplicate Emails/README_EN.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solution/0100-0199/0196.Delete Duplicate Emails/README_EN.md b/solution/0100-0199/0196.Delete Duplicate Emails/README_EN.md index ccef471e1ca44..5a7948ae8cedc 100644 --- a/solution/0100-0199/0196.Delete Duplicate Emails/README_EN.md +++ b/solution/0100-0199/0196.Delete Duplicate Emails/README_EN.md @@ -93,7 +93,7 @@ WHERE p1.id < p2.id; ``` -### **Python** +### **Pandas** ```python import pandas as pd From 496e4a8a571a2c9773fe0a83eb44f452403811d5 Mon Sep 17 00:00:00 2001 From: Rhitik V Nimbalkar <74248352+nrhitik@users.noreply.github.com> Date: Wed, 25 Oct 2023 10:02:32 +0530 Subject: [PATCH 05/18] Update README.md --- solution/0100-0199/0196.Delete Duplicate Emails/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solution/0100-0199/0196.Delete Duplicate Emails/README.md b/solution/0100-0199/0196.Delete Duplicate Emails/README.md index 69ac60b8cb180..f9c770024b842 100644 --- a/solution/0100-0199/0196.Delete Duplicate Emails/README.md +++ b/solution/0100-0199/0196.Delete Duplicate Emails/README.md @@ -97,7 +97,7 @@ WHERE p1.id < p2.id; ``` -### **Python** +### **Pandas** ```python import pandas as pd From ff0513660a814a9cb59167bde5a57617159baa21 Mon Sep 17 00:00:00 2001 From: Rhitik Date: Wed, 25 Oct 2023 16:27:35 +0530 Subject: [PATCH 06/18] Solution for 197. --- .../0100-0199/0197.Rising Temperature/README.md | 13 +++++++++++++ .../0100-0199/0197.Rising Temperature/README_EN.md | 13 +++++++++++++ .../0100-0199/0197.Rising Temperature/Solution.py | 8 ++++++++ 3 files changed, 34 insertions(+) create mode 100644 solution/0100-0199/0197.Rising Temperature/Solution.py diff --git a/solution/0100-0199/0197.Rising Temperature/README.md b/solution/0100-0199/0197.Rising Temperature/README.md index c7f89a1be25ce..e95462f3421e0 100644 --- a/solution/0100-0199/0197.Rising Temperature/README.md +++ b/solution/0100-0199/0197.Rising Temperature/README.md @@ -86,5 +86,18 @@ FROM JOIN Weather AS w2 ON SUBDATE(w1.recordDate, 1) = w2.recordDate AND w1.temperature > w2.temperature; ``` +### **Pandas** + +```python +import pandas as pd + + +def rising_temperature(weather: pd.DataFrame) -> pd.DataFrame: + weather.sort_values(by="recordDate", inplace=True) + return weather[ + (weather.temperature.diff() > 0) & (weather.recordDate.diff().dt.days == 1) + ][["id"]] + +``` diff --git a/solution/0100-0199/0197.Rising Temperature/README_EN.md b/solution/0100-0199/0197.Rising Temperature/README_EN.md index a346c9db29333..ec22639017cfc 100644 --- a/solution/0100-0199/0197.Rising Temperature/README_EN.md +++ b/solution/0100-0199/0197.Rising Temperature/README_EN.md @@ -79,5 +79,18 @@ FROM JOIN Weather AS w2 ON SUBDATE(w1.recordDate, 1) = w2.recordDate AND w1.temperature > w2.temperature; ``` +### **Pandas** + +```python +import pandas as pd + + +def rising_temperature(weather: pd.DataFrame) -> pd.DataFrame: + weather.sort_values(by="recordDate", inplace=True) + return weather[ + (weather.temperature.diff() > 0) & (weather.recordDate.diff().dt.days == 1) + ][["id"]] + +``` diff --git a/solution/0100-0199/0197.Rising Temperature/Solution.py b/solution/0100-0199/0197.Rising Temperature/Solution.py new file mode 100644 index 0000000000000..0dcb7fe533075 --- /dev/null +++ b/solution/0100-0199/0197.Rising Temperature/Solution.py @@ -0,0 +1,8 @@ +import pandas as pd + + +def rising_temperature(weather: pd.DataFrame) -> pd.DataFrame: + weather.sort_values(by="recordDate", inplace=True) + return weather[ + (weather.temperature.diff() > 0) & (weather.recordDate.diff().dt.days == 1) + ][["id"]] From ebeaba3300eebf61ef288a8ebf921b9f7b91a789 Mon Sep 17 00:00:00 2001 From: Rhitik Date: Wed, 25 Oct 2023 16:38:49 +0530 Subject: [PATCH 07/18] Solution for 262. --- .../0200-0299/0262.Trips and Users/README.md | 49 +++++++++++++++++++ .../0262.Trips and Users/README_EN.md | 49 +++++++++++++++++++ .../0262.Trips and Users/Solution.py | 43 ++++++++++++++++ 3 files changed, 141 insertions(+) create mode 100644 solution/0200-0299/0262.Trips and Users/Solution.py diff --git a/solution/0200-0299/0262.Trips and Users/README.md b/solution/0200-0299/0262.Trips and Users/README.md index 11cdb315a09d9..c25fa534548f1 100644 --- a/solution/0200-0299/0262.Trips and Users/README.md +++ b/solution/0200-0299/0262.Trips and Users/README.md @@ -140,4 +140,53 @@ WHERE request_at BETWEEN '2013-10-01' AND '2013-10-03' GROUP BY request_at; ``` +### **Pandas** + +```python +import pandas as pd + + +def trips_and_users(trips: pd.DataFrame, users: pd.DataFrame) -> pd.DataFrame: + # 1) temporal filtering + trips = trips[trips["request_at"].between("2013-10-01", "2013-10-03")].rename( + columns={"request_at": "Day"} + ) + + # 2) filtering based not banned + # 2.1) mappning the column 'banned' to `client_id` and `driver_id` + df_client = ( + pd.merge(trips, users, left_on="client_id", right_on="users_id", how="left") + .drop(["users_id", "role"], axis=1) + .rename(columns={"banned": "banned_client"}) + ) + df_driver = ( + pd.merge(trips, users, left_on="driver_id", right_on="users_id", how="left") + .drop(["users_id", "role"], axis=1) + .rename(columns={"banned": "banned_driver"}) + ) + df = pd.merge( + df_client, + df_driver, + left_on=["id", "driver_id", "client_id", "city_id", "status", "Day"], + right_on=["id", "driver_id", "client_id", "city_id", "status", "Day"], + how="left", + ) + # 2.2) filtering based on not banned + df = df[(df["banned_client"] == "No") & (df["banned_driver"] == "No")] + + # 3) counting the cancelled and total trips per day + df["status_cancelled"] = df["status"].str.contains("cancelled") + df = df[["Day", "status_cancelled"]] + df = df.groupby("Day").agg( + {"status_cancelled": [("total_cancelled", "sum"), ("total", "count")]} + ) + df.columns = df.columns.droplevel() + df = df.reset_index() + + # 4) calculating the ratio + df["Cancellation Rate"] = (df["total_cancelled"] / df["total"]).round(2) + return df[["Day", "Cancellation Rate"]] + +``` + diff --git a/solution/0200-0299/0262.Trips and Users/README_EN.md b/solution/0200-0299/0262.Trips and Users/README_EN.md index 440adce8e9de5..87762ba27cc4b 100644 --- a/solution/0200-0299/0262.Trips and Users/README_EN.md +++ b/solution/0200-0299/0262.Trips and Users/README_EN.md @@ -127,4 +127,53 @@ WHERE request_at BETWEEN '2013-10-01' AND '2013-10-03' GROUP BY request_at; ``` +### **Pandas** + +```python +import pandas as pd + + +def trips_and_users(trips: pd.DataFrame, users: pd.DataFrame) -> pd.DataFrame: + # 1) temporal filtering + trips = trips[trips["request_at"].between("2013-10-01", "2013-10-03")].rename( + columns={"request_at": "Day"} + ) + + # 2) filtering based not banned + # 2.1) mappning the column 'banned' to `client_id` and `driver_id` + df_client = ( + pd.merge(trips, users, left_on="client_id", right_on="users_id", how="left") + .drop(["users_id", "role"], axis=1) + .rename(columns={"banned": "banned_client"}) + ) + df_driver = ( + pd.merge(trips, users, left_on="driver_id", right_on="users_id", how="left") + .drop(["users_id", "role"], axis=1) + .rename(columns={"banned": "banned_driver"}) + ) + df = pd.merge( + df_client, + df_driver, + left_on=["id", "driver_id", "client_id", "city_id", "status", "Day"], + right_on=["id", "driver_id", "client_id", "city_id", "status", "Day"], + how="left", + ) + # 2.2) filtering based on not banned + df = df[(df["banned_client"] == "No") & (df["banned_driver"] == "No")] + + # 3) counting the cancelled and total trips per day + df["status_cancelled"] = df["status"].str.contains("cancelled") + df = df[["Day", "status_cancelled"]] + df = df.groupby("Day").agg( + {"status_cancelled": [("total_cancelled", "sum"), ("total", "count")]} + ) + df.columns = df.columns.droplevel() + df = df.reset_index() + + # 4) calculating the ratio + df["Cancellation Rate"] = (df["total_cancelled"] / df["total"]).round(2) + return df[["Day", "Cancellation Rate"]] + +``` + diff --git a/solution/0200-0299/0262.Trips and Users/Solution.py b/solution/0200-0299/0262.Trips and Users/Solution.py new file mode 100644 index 0000000000000..2558b24f0a130 --- /dev/null +++ b/solution/0200-0299/0262.Trips and Users/Solution.py @@ -0,0 +1,43 @@ +import pandas as pd + + +def trips_and_users(trips: pd.DataFrame, users: pd.DataFrame) -> pd.DataFrame: + # 1) temporal filtering + trips = trips[trips["request_at"].between("2013-10-01", "2013-10-03")].rename( + columns={"request_at": "Day"} + ) + + # 2) filtering based not banned + # 2.1) mappning the column 'banned' to `client_id` and `driver_id` + df_client = ( + pd.merge(trips, users, left_on="client_id", right_on="users_id", how="left") + .drop(["users_id", "role"], axis=1) + .rename(columns={"banned": "banned_client"}) + ) + df_driver = ( + pd.merge(trips, users, left_on="driver_id", right_on="users_id", how="left") + .drop(["users_id", "role"], axis=1) + .rename(columns={"banned": "banned_driver"}) + ) + df = pd.merge( + df_client, + df_driver, + left_on=["id", "driver_id", "client_id", "city_id", "status", "Day"], + right_on=["id", "driver_id", "client_id", "city_id", "status", "Day"], + how="left", + ) + # 2.2) filtering based on not banned + df = df[(df["banned_client"] == "No") & (df["banned_driver"] == "No")] + + # 3) counting the cancelled and total trips per day + df["status_cancelled"] = df["status"].str.contains("cancelled") + df = df[["Day", "status_cancelled"]] + df = df.groupby("Day").agg( + {"status_cancelled": [("total_cancelled", "sum"), ("total", "count")]} + ) + df.columns = df.columns.droplevel() + df = df.reset_index() + + # 4) calculating the ratio + df["Cancellation Rate"] = (df["total_cancelled"] / df["total"]).round(2) + return df[["Day", "Cancellation Rate"]] From eb12672cc98b06b324180646701e783003216302 Mon Sep 17 00:00:00 2001 From: nrhitik Date: Wed, 25 Oct 2023 11:10:51 +0000 Subject: [PATCH 08/18] style: format code and docs with prettier --- solution/0100-0199/0197.Rising Temperature/README.md | 1 + solution/0100-0199/0197.Rising Temperature/README_EN.md | 1 + 2 files changed, 2 insertions(+) diff --git a/solution/0100-0199/0197.Rising Temperature/README.md b/solution/0100-0199/0197.Rising Temperature/README.md index e95462f3421e0..bd685ffa0736d 100644 --- a/solution/0100-0199/0197.Rising Temperature/README.md +++ b/solution/0100-0199/0197.Rising Temperature/README.md @@ -86,6 +86,7 @@ FROM JOIN Weather AS w2 ON SUBDATE(w1.recordDate, 1) = w2.recordDate AND w1.temperature > w2.temperature; ``` + ### **Pandas** ```python diff --git a/solution/0100-0199/0197.Rising Temperature/README_EN.md b/solution/0100-0199/0197.Rising Temperature/README_EN.md index ec22639017cfc..97c3c75a8fabc 100644 --- a/solution/0100-0199/0197.Rising Temperature/README_EN.md +++ b/solution/0100-0199/0197.Rising Temperature/README_EN.md @@ -79,6 +79,7 @@ FROM JOIN Weather AS w2 ON SUBDATE(w1.recordDate, 1) = w2.recordDate AND w1.temperature > w2.temperature; ``` + ### **Pandas** ```python From 0a9336a4ecd39107e52f742f7f495dab5fb2e8a1 Mon Sep 17 00:00:00 2001 From: Rhitik Date: Wed, 25 Oct 2023 16:54:55 +0530 Subject: [PATCH 09/18] Solution for 319. --- solution/0300-0399/0319.Bulb Switcher/README.md | 3 +++ solution/0300-0399/0319.Bulb Switcher/README_EN.md | 3 +++ solution/0300-0399/0319.Bulb Switcher/Solution.py | 3 +++ 3 files changed, 9 insertions(+) create mode 100644 solution/0300-0399/0319.Bulb Switcher/Solution.py diff --git a/solution/0300-0399/0319.Bulb Switcher/README.md b/solution/0300-0399/0319.Bulb Switcher/README.md index 0033359fc94ec..69c05d326e9f9 100644 --- a/solution/0300-0399/0319.Bulb Switcher/README.md +++ b/solution/0300-0399/0319.Bulb Switcher/README.md @@ -63,6 +63,9 @@ ```python +class Solution: + def bulbSwitch(self, n: int) -> int: + return int(n ** (1 / 2)) ``` diff --git a/solution/0300-0399/0319.Bulb Switcher/README_EN.md b/solution/0300-0399/0319.Bulb Switcher/README_EN.md index 0e75923efc308..d0ae74a07b766 100644 --- a/solution/0300-0399/0319.Bulb Switcher/README_EN.md +++ b/solution/0300-0399/0319.Bulb Switcher/README_EN.md @@ -50,6 +50,9 @@ So you should return 1 because there is only one bulb is on. ### **Python3** ```python +class Solution: + def bulbSwitch(self, n: int) -> int: + return int(n ** (1 / 2)) ``` diff --git a/solution/0300-0399/0319.Bulb Switcher/Solution.py b/solution/0300-0399/0319.Bulb Switcher/Solution.py new file mode 100644 index 0000000000000..2bbf692999530 --- /dev/null +++ b/solution/0300-0399/0319.Bulb Switcher/Solution.py @@ -0,0 +1,3 @@ +class Solution: + def bulbSwitch(self, n: int) -> int: + return int(n ** (1 / 2)) From 5594e7368ec0a1a58caff56d88817f4a96f52043 Mon Sep 17 00:00:00 2001 From: Rhitik Date: Thu, 26 Oct 2023 00:41:25 +0530 Subject: [PATCH 10/18] Solution for 332. --- .../0332.Reconstruct Itinerary/README.md | 17 +++++++++++++++++ .../0332.Reconstruct Itinerary/README_EN.md | 17 +++++++++++++++++ .../0332.Reconstruct Itinerary/Solution.py | 17 +++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 solution/0300-0399/0332.Reconstruct Itinerary/Solution.py diff --git a/solution/0300-0399/0332.Reconstruct Itinerary/README.md b/solution/0300-0399/0332.Reconstruct Itinerary/README.md index 10906a75fd998..a84651a75af5e 100644 --- a/solution/0300-0399/0332.Reconstruct Itinerary/README.md +++ b/solution/0300-0399/0332.Reconstruct Itinerary/README.md @@ -57,6 +57,23 @@ ```python +class Solution: + def findItinerary(self, tickets: List[List[str]]) -> List[str]: + graph = defaultdict(list) + + for src, dst in sorted(tickets, reverse=True): + graph[src].append(dst) + + itinerary = [] + + def dfs(airport): + while graph[airport]: + dfs(graph[airport].pop()) + itinerary.append(airport) + + dfs("JFK") + + return itinerary[::-1] ``` diff --git a/solution/0300-0399/0332.Reconstruct Itinerary/README_EN.md b/solution/0300-0399/0332.Reconstruct Itinerary/README_EN.md index 99c61037d1859..2728db5195bf3 100644 --- a/solution/0300-0399/0332.Reconstruct Itinerary/README_EN.md +++ b/solution/0300-0399/0332.Reconstruct Itinerary/README_EN.md @@ -49,6 +49,23 @@ ### **Python3** ```python +class Solution: + def findItinerary(self, tickets: List[List[str]]) -> List[str]: + graph = defaultdict(list) + + for src, dst in sorted(tickets, reverse=True): + graph[src].append(dst) + + itinerary = [] + + def dfs(airport): + while graph[airport]: + dfs(graph[airport].pop()) + itinerary.append(airport) + + dfs("JFK") + + return itinerary[::-1] ``` diff --git a/solution/0300-0399/0332.Reconstruct Itinerary/Solution.py b/solution/0300-0399/0332.Reconstruct Itinerary/Solution.py new file mode 100644 index 0000000000000..8b6c452a44ca2 --- /dev/null +++ b/solution/0300-0399/0332.Reconstruct Itinerary/Solution.py @@ -0,0 +1,17 @@ +class Solution: + def findItinerary(self, tickets: List[List[str]]) -> List[str]: + graph = defaultdict(list) + + for src, dst in sorted(tickets, reverse=True): + graph[src].append(dst) + + itinerary = [] + + def dfs(airport): + while graph[airport]: + dfs(graph[airport].pop()) + itinerary.append(airport) + + dfs("JFK") + + return itinerary[::-1] From df3b19cdba28dc6130f0c7021c311010241b6544 Mon Sep 17 00:00:00 2001 From: Rhitik Date: Thu, 26 Oct 2023 00:54:22 +0530 Subject: [PATCH 11/18] Solution for 468. --- .../0468.Validate IP Address/README.md | 27 +++++++++++++++++++ .../0468.Validate IP Address/README_EN.md | 27 +++++++++++++++++++ .../0468.Validate IP Address/Solution.py | 27 +++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 solution/0400-0499/0468.Validate IP Address/Solution.py diff --git a/solution/0400-0499/0468.Validate IP Address/README.md b/solution/0400-0499/0468.Validate IP Address/README.md index bdbc0a18b6817..b7ac66790bc7d 100644 --- a/solution/0400-0499/0468.Validate IP Address/README.md +++ b/solution/0400-0499/0468.Validate IP Address/README.md @@ -65,6 +65,33 @@ ```python +class Solution: + def validIPAddress(self, IP: str) -> str: + if "." in IP: + segments = IP.split(".") + if len(segments) != 4: + return "Neither" + for segment in segments: + if ( + not segment.isdigit() + or not 0 <= int(segment) <= 255 + or (segment[0] == "0" and len(segment) > 1) + ): + return "Neither" + return "IPv4" + elif ":" in IP: + segments = IP.split(":") + if len(segments) != 8: + return "Neither" + for segment in segments: + if ( + not segment + or len(segment) > 4 + or not all(c in string.hexdigits for c in segment) + ): + return "Neither" + return "IPv6" + return "Neither" ``` diff --git a/solution/0400-0499/0468.Validate IP Address/README_EN.md b/solution/0400-0499/0468.Validate IP Address/README_EN.md index ff669c0532c13..d3e199ec15f95 100644 --- a/solution/0400-0499/0468.Validate IP Address/README_EN.md +++ b/solution/0400-0499/0468.Validate IP Address/README_EN.md @@ -57,6 +57,33 @@ ### **Python3** ```python +class Solution: + def validIPAddress(self, IP: str) -> str: + if "." in IP: + segments = IP.split(".") + if len(segments) != 4: + return "Neither" + for segment in segments: + if ( + not segment.isdigit() + or not 0 <= int(segment) <= 255 + or (segment[0] == "0" and len(segment) > 1) + ): + return "Neither" + return "IPv4" + elif ":" in IP: + segments = IP.split(":") + if len(segments) != 8: + return "Neither" + for segment in segments: + if ( + not segment + or len(segment) > 4 + or not all(c in string.hexdigits for c in segment) + ): + return "Neither" + return "IPv6" + return "Neither" ``` diff --git a/solution/0400-0499/0468.Validate IP Address/Solution.py b/solution/0400-0499/0468.Validate IP Address/Solution.py new file mode 100644 index 0000000000000..a23147565687f --- /dev/null +++ b/solution/0400-0499/0468.Validate IP Address/Solution.py @@ -0,0 +1,27 @@ +class Solution: + def validIPAddress(self, IP: str) -> str: + if "." in IP: + segments = IP.split(".") + if len(segments) != 4: + return "Neither" + for segment in segments: + if ( + not segment.isdigit() + or not 0 <= int(segment) <= 255 + or (segment[0] == "0" and len(segment) > 1) + ): + return "Neither" + return "IPv4" + elif ":" in IP: + segments = IP.split(":") + if len(segments) != 8: + return "Neither" + for segment in segments: + if ( + not segment + or len(segment) > 4 + or not all(c in string.hexdigits for c in segment) + ): + return "Neither" + return "IPv6" + return "Neither" From 9b037e3fddc28d42c9c268b2aeb901f484e079f1 Mon Sep 17 00:00:00 2001 From: Rhitik Date: Thu, 26 Oct 2023 10:50:48 +0530 Subject: [PATCH 12/18] Solution for 476. --- solution/0400-0499/0476.Number Complement/README.md | 7 +++++++ solution/0400-0499/0476.Number Complement/README_EN.md | 7 +++++++ solution/0400-0499/0476.Number Complement/Solution.py | 3 +++ 3 files changed, 17 insertions(+) create mode 100644 solution/0400-0499/0476.Number Complement/Solution.py diff --git a/solution/0400-0499/0476.Number Complement/README.md b/solution/0400-0499/0476.Number Complement/README.md index c2e81402fcfca..61af294e8da26 100644 --- a/solution/0400-0499/0476.Number Complement/README.md +++ b/solution/0400-0499/0476.Number Complement/README.md @@ -72,6 +72,13 @@ class Solution: return ans ``` +```python +class Solution: + def findComplement(self, num: int) -> int: + return num ^ (2 ** (len(bin(num)[2:])) - 1) + +``` + ### **Java** diff --git a/solution/0400-0499/0476.Number Complement/README_EN.md b/solution/0400-0499/0476.Number Complement/README_EN.md index 8cd5b20cc55f2..2a17fdaa2c22f 100644 --- a/solution/0400-0499/0476.Number Complement/README_EN.md +++ b/solution/0400-0499/0476.Number Complement/README_EN.md @@ -60,6 +60,13 @@ class Solution: return ans ``` +```python +class Solution: + def findComplement(self, num: int) -> int: + return num ^ (2 ** (len(bin(num)[2:])) - 1) + +``` + ### **Java** ```java diff --git a/solution/0400-0499/0476.Number Complement/Solution.py b/solution/0400-0499/0476.Number Complement/Solution.py new file mode 100644 index 0000000000000..7156b7276f47c --- /dev/null +++ b/solution/0400-0499/0476.Number Complement/Solution.py @@ -0,0 +1,3 @@ +class Solution: + def findComplement(self, num: int) -> int: + return num ^ (2 ** (len(bin(num)[2:])) - 1) From 7dd0e49bd6197d9e570f516ac901c9991d9abcf0 Mon Sep 17 00:00:00 2001 From: Rhitik Date: Thu, 26 Oct 2023 10:54:46 +0530 Subject: [PATCH 13/18] Solution for 478. --- .../0478.Generate Random Point in a Circle/README.md | 12 ++++++++++++ .../README_EN.md | 12 ++++++++++++ .../Solution.py | 12 ++++++++++++ 3 files changed, 36 insertions(+) create mode 100644 solution/0400-0499/0478.Generate Random Point in a Circle/Solution.py diff --git a/solution/0400-0499/0478.Generate Random Point in a Circle/README.md b/solution/0400-0499/0478.Generate Random Point in a Circle/README.md index d4636e1e7bbaa..61cd82a7ea718 100644 --- a/solution/0400-0499/0478.Generate Random Point in a Circle/README.md +++ b/solution/0400-0499/0478.Generate Random Point in a Circle/README.md @@ -51,6 +51,18 @@ solution.randPoint ();//返回[0.36572,0.17248] ```python +class Solution: + def __init__(self, radius: float, x_center: float, y_center: float): + self.radius = radius + self.x_center = x_center + self.y_center = y_center + + def randPoint(self) -> List[float]: + length = math.sqrt(random.uniform(0, self.radius**2)) + degree = random.uniform(0, 1) * 2 * math.pi + x = self.x_center + length * math.cos(degree) + y = self.y_center + length * math.sin(degree) + return [x, y] ``` diff --git a/solution/0400-0499/0478.Generate Random Point in a Circle/README_EN.md b/solution/0400-0499/0478.Generate Random Point in a Circle/README_EN.md index b39d6e4114f4d..9f5ef9486e426 100644 --- a/solution/0400-0499/0478.Generate Random Point in a Circle/README_EN.md +++ b/solution/0400-0499/0478.Generate Random Point in a Circle/README_EN.md @@ -46,6 +46,18 @@ solution.randPoint(); // return [0.36572, 0.17248] ### **Python3** ```python +class Solution: + def __init__(self, radius: float, x_center: float, y_center: float): + self.radius = radius + self.x_center = x_center + self.y_center = y_center + + def randPoint(self) -> List[float]: + length = math.sqrt(random.uniform(0, self.radius**2)) + degree = random.uniform(0, 1) * 2 * math.pi + x = self.x_center + length * math.cos(degree) + y = self.y_center + length * math.sin(degree) + return [x, y] ``` diff --git a/solution/0400-0499/0478.Generate Random Point in a Circle/Solution.py b/solution/0400-0499/0478.Generate Random Point in a Circle/Solution.py new file mode 100644 index 0000000000000..c793c1a433b4f --- /dev/null +++ b/solution/0400-0499/0478.Generate Random Point in a Circle/Solution.py @@ -0,0 +1,12 @@ +class Solution: + def __init__(self, radius: float, x_center: float, y_center: float): + self.radius = radius + self.x_center = x_center + self.y_center = y_center + + def randPoint(self) -> List[float]: + length = math.sqrt(random.uniform(0, self.radius**2)) + degree = random.uniform(0, 1) * 2 * math.pi + x = self.x_center + length * math.cos(degree) + y = self.y_center + length * math.sin(degree) + return [x, y] From 1552bce468a00db7a41634227fe1b21d78d71af8 Mon Sep 17 00:00:00 2001 From: Rhitik Date: Thu, 26 Oct 2023 19:02:12 +0530 Subject: [PATCH 14/18] Solution for 511. --- .../0500-0599/0511.Game Play Analysis I/README.md | 15 +++++++++++++++ .../0511.Game Play Analysis I/README_EN.md | 15 +++++++++++++++ .../0511.Game Play Analysis I/Solution.py | 9 +++++++++ 3 files changed, 39 insertions(+) create mode 100644 solution/0500-0599/0511.Game Play Analysis I/Solution.py diff --git a/solution/0500-0599/0511.Game Play Analysis I/README.md b/solution/0500-0599/0511.Game Play Analysis I/README.md index 5ca0090658796..94a674b1641d1 100644 --- a/solution/0500-0599/0511.Game Play Analysis I/README.md +++ b/solution/0500-0599/0511.Game Play Analysis I/README.md @@ -69,4 +69,19 @@ FROM Activity GROUP BY 1; ``` +### **pandas** + +```python +import pandas as pd + + +def game_analysis(activity: pd.DataFrame) -> pd.DataFrame: + return ( + activity.groupby("player_id") + .agg(first_login=("event_date", "min")) + .reset_index() + ) + +``` + diff --git a/solution/0500-0599/0511.Game Play Analysis I/README_EN.md b/solution/0500-0599/0511.Game Play Analysis I/README_EN.md index 1b8d9388d0424..d4f37f4bf2ad8 100644 --- a/solution/0500-0599/0511.Game Play Analysis I/README_EN.md +++ b/solution/0500-0599/0511.Game Play Analysis I/README_EN.md @@ -70,4 +70,19 @@ FROM Activity GROUP BY 1; ``` +### **pandas** + +```python +import pandas as pd + + +def game_analysis(activity: pd.DataFrame) -> pd.DataFrame: + return ( + activity.groupby("player_id") + .agg(first_login=("event_date", "min")) + .reset_index() + ) + +``` + diff --git a/solution/0500-0599/0511.Game Play Analysis I/Solution.py b/solution/0500-0599/0511.Game Play Analysis I/Solution.py new file mode 100644 index 0000000000000..3d42f06ede95a --- /dev/null +++ b/solution/0500-0599/0511.Game Play Analysis I/Solution.py @@ -0,0 +1,9 @@ +import pandas as pd + + +def game_analysis(activity: pd.DataFrame) -> pd.DataFrame: + return ( + activity.groupby("player_id") + .agg(first_login=("event_date", "min")) + .reset_index() + ) From 49bf92b9d7ed0dec147d9fb9985e1e8c9920efde Mon Sep 17 00:00:00 2001 From: Rhitik Date: Thu, 26 Oct 2023 19:11:00 +0530 Subject: [PATCH 15/18] Solution for 550. --- .../0550.Game Play Analysis IV/README.md | 17 +++++++++++++++++ .../0550.Game Play Analysis IV/README_EN.md | 18 ++++++++++++++++++ .../0550.Game Play Analysis IV/Solution.py | 11 +++++++++++ 3 files changed, 46 insertions(+) create mode 100644 solution/0500-0599/0550.Game Play Analysis IV/Solution.py diff --git a/solution/0500-0599/0550.Game Play Analysis IV/README.md b/solution/0500-0599/0550.Game Play Analysis IV/README.md index 2d748996035a1..808edb26fec06 100644 --- a/solution/0500-0599/0550.Game Play Analysis IV/README.md +++ b/solution/0500-0599/0550.Game Play Analysis IV/README.md @@ -107,4 +107,21 @@ FROM T WHERE rk = 1; ``` +### **pandas** + +```python +import pandas as pd + + +def gameplay_analysis(activity: pd.DataFrame) -> pd.DataFrame: + activity["first"] = activity.groupby("player_id").event_date.transform(min) + activity_2nd_day = activity.loc[ + activity["first"] + pd.DateOffset(1) == activity["event_date"] + ] + return pd.DataFrame( + {"fraction": [round(len(activity_2nd_day) / activity.player_id.nunique(), 2)]} + ) + +``` + diff --git a/solution/0500-0599/0550.Game Play Analysis IV/README_EN.md b/solution/0500-0599/0550.Game Play Analysis IV/README_EN.md index d26b4e381e0be..2db4d3f46c5c7 100644 --- a/solution/0500-0599/0550.Game Play Analysis IV/README_EN.md +++ b/solution/0500-0599/0550.Game Play Analysis IV/README_EN.md @@ -102,4 +102,22 @@ FROM T WHERE rk = 1; ``` +### **pandas** + +```python +import pandas as pd + + +def gameplay_analysis(activity: pd.DataFrame) -> pd.DataFrame: + activity["first"] = activity.groupby("player_id").event_date.transform(min) + activity_2nd_day = activity.loc[ + activity["first"] + pd.DateOffset(1) == activity["event_date"] + ] + return pd.DataFrame( + {"fraction": [round(len(activity_2nd_day) / activity.player_id.nunique(), 2)]} + ) + +``` + + diff --git a/solution/0500-0599/0550.Game Play Analysis IV/Solution.py b/solution/0500-0599/0550.Game Play Analysis IV/Solution.py new file mode 100644 index 0000000000000..d59eb5b79eb33 --- /dev/null +++ b/solution/0500-0599/0550.Game Play Analysis IV/Solution.py @@ -0,0 +1,11 @@ +import pandas as pd + + +def gameplay_analysis(activity: pd.DataFrame) -> pd.DataFrame: + activity["first"] = activity.groupby("player_id").event_date.transform(min) + activity_2nd_day = activity.loc[ + activity["first"] + pd.DateOffset(1) == activity["event_date"] + ] + return pd.DataFrame( + {"fraction": [round(len(activity_2nd_day) / activity.player_id.nunique(), 2)]} + ) From 620c3ff566b47d9100a4456ec190f186d023a848 Mon Sep 17 00:00:00 2001 From: nrhitik Date: Thu, 26 Oct 2023 15:04:36 +0000 Subject: [PATCH 16/18] style: format code and docs with prettier --- solution/0500-0599/0550.Game Play Analysis IV/README.md | 2 +- solution/0500-0599/0550.Game Play Analysis IV/README_EN.md | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/solution/0500-0599/0550.Game Play Analysis IV/README.md b/solution/0500-0599/0550.Game Play Analysis IV/README.md index 808edb26fec06..6ff749005c789 100644 --- a/solution/0500-0599/0550.Game Play Analysis IV/README.md +++ b/solution/0500-0599/0550.Game Play Analysis IV/README.md @@ -121,7 +121,7 @@ def gameplay_analysis(activity: pd.DataFrame) -> pd.DataFrame: return pd.DataFrame( {"fraction": [round(len(activity_2nd_day) / activity.player_id.nunique(), 2)]} ) - + ``` diff --git a/solution/0500-0599/0550.Game Play Analysis IV/README_EN.md b/solution/0500-0599/0550.Game Play Analysis IV/README_EN.md index 2db4d3f46c5c7..01fc9ee34de32 100644 --- a/solution/0500-0599/0550.Game Play Analysis IV/README_EN.md +++ b/solution/0500-0599/0550.Game Play Analysis IV/README_EN.md @@ -116,8 +116,7 @@ def gameplay_analysis(activity: pd.DataFrame) -> pd.DataFrame: return pd.DataFrame( {"fraction": [round(len(activity_2nd_day) / activity.player_id.nunique(), 2)]} ) - -``` +``` From ae18e5900252182769f57e59614b225aa8fef60f Mon Sep 17 00:00:00 2001 From: Rhitik V Nimbalkar <74248352+nrhitik@users.noreply.github.com> Date: Thu, 26 Oct 2023 20:53:07 +0530 Subject: [PATCH 17/18] Update README.md --- solution/0500-0599/0511.Game Play Analysis I/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solution/0500-0599/0511.Game Play Analysis I/README.md b/solution/0500-0599/0511.Game Play Analysis I/README.md index 94a674b1641d1..67a127298ceb5 100644 --- a/solution/0500-0599/0511.Game Play Analysis I/README.md +++ b/solution/0500-0599/0511.Game Play Analysis I/README.md @@ -69,7 +69,7 @@ FROM Activity GROUP BY 1; ``` -### **pandas** +### **Pandas** ```python import pandas as pd From 753e41fe3b56fee8648e7fb721fbe2a9c0e43e28 Mon Sep 17 00:00:00 2001 From: Rhitik V Nimbalkar <74248352+nrhitik@users.noreply.github.com> Date: Thu, 26 Oct 2023 20:53:47 +0530 Subject: [PATCH 18/18] Update README_EN.md --- solution/0500-0599/0511.Game Play Analysis I/README_EN.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solution/0500-0599/0511.Game Play Analysis I/README_EN.md b/solution/0500-0599/0511.Game Play Analysis I/README_EN.md index d4f37f4bf2ad8..0780af95f1522 100644 --- a/solution/0500-0599/0511.Game Play Analysis I/README_EN.md +++ b/solution/0500-0599/0511.Game Play Analysis I/README_EN.md @@ -70,7 +70,7 @@ FROM Activity GROUP BY 1; ``` -### **pandas** +### **Pandas** ```python import pandas as pd