Skip to content

Commit 45e1b58

Browse files
author
bitoollearner
committed
LeetCode Pyspark Solution
1 parent a186b8f commit 45e1b58

9 files changed

+1171
-63
lines changed

Solved/1783. Grand Slam Titles (Medium)-(Solved).ipynb

Lines changed: 119 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
"cell_type": "markdown",
55
"metadata": {
66
"application/vnd.databricks.v1+cell": {
7-
"cellMetadata": {},
7+
"cellMetadata": {
8+
"byteLimit": 2048000,
9+
"rowLimit": 10000
10+
},
811
"inputWidgets": {},
912
"nuid": "fadf709d-de3d-4bfc-b69f-3b489de68bef",
1013
"showTitle": false,
@@ -21,7 +24,10 @@
2124
"execution_count": 0,
2225
"metadata": {
2326
"application/vnd.databricks.v1+cell": {
24-
"cellMetadata": {},
27+
"cellMetadata": {
28+
"byteLimit": 2048000,
29+
"rowLimit": 10000
30+
},
2531
"inputWidgets": {},
2632
"nuid": "d4a1fef1-aa99-469c-b35f-9b3f3e81c9f7",
2733
"showTitle": false,
@@ -40,7 +46,10 @@
4046
"cell_type": "markdown",
4147
"metadata": {
4248
"application/vnd.databricks.v1+cell": {
43-
"cellMetadata": {},
49+
"cellMetadata": {
50+
"byteLimit": 2048000,
51+
"rowLimit": 10000
52+
},
4453
"inputWidgets": {},
4554
"nuid": "78daa073-80d8-4f7e-9cfa-10f6eb568909",
4655
"showTitle": false,
@@ -116,15 +125,27 @@
116125
"execution_count": 0,
117126
"metadata": {
118127
"application/vnd.databricks.v1+cell": {
119-
"cellMetadata": {},
128+
"cellMetadata": {
129+
"byteLimit": 2048000,
130+
"rowLimit": 10000
131+
},
120132
"inputWidgets": {},
121133
"nuid": "7b78bc27-26cd-4523-8646-7369baf68fcb",
122134
"showTitle": false,
123135
"tableResultSettingsMap": {},
124136
"title": ""
125137
}
126138
},
127-
"outputs": [],
139+
"outputs": [
140+
{
141+
"output_type": "stream",
142+
"name": "stdout",
143+
"output_type": "stream",
144+
"text": [
145+
"+---------+-----------+\n|player_id|player_name|\n+---------+-----------+\n| 1| Nadal|\n| 2| Federer|\n| 3| Novak|\n+---------+-----------+\n\n+----+---------+-------+-------+-------+\n|year|Wimbledon|Fr_open|US_open|Au_open|\n+----+---------+-------+-------+-------+\n|2018| 1| 1| 1| 1|\n|2019| 1| 1| 2| 2|\n|2020| 2| 1| 2| 2|\n+----+---------+-------+-------+-------+\n\n"
146+
]
147+
}
148+
],
128149
"source": [
129150
"players_data_1783 = [\n",
130151
" (1, \"Nadal\"),\n",
@@ -145,15 +166,106 @@
145166
"championships_df_1783 = spark.createDataFrame(championships_data_1783, championships_columns_1783)\n",
146167
"championships_df_1783.show()"
147168
]
169+
},
170+
{
171+
"cell_type": "code",
172+
"execution_count": 0,
173+
"metadata": {
174+
"application/vnd.databricks.v1+cell": {
175+
"cellMetadata": {
176+
"byteLimit": 2048000,
177+
"rowLimit": 10000
178+
},
179+
"inputWidgets": {},
180+
"nuid": "5611be8a-06c7-4531-ab33-cd878081856f",
181+
"showTitle": false,
182+
"tableResultSettingsMap": {},
183+
"title": ""
184+
}
185+
},
186+
"outputs": [],
187+
"source": [
188+
"champ_unpivoted_df_1783 = championships_df_1783\\\n",
189+
" .select( col(\"year\"),\n",
190+
" explode(\n",
191+
" array(\n",
192+
" col(\"Wimbledon\").alias(\"winner\"),\n",
193+
" col(\"Fr_open\").alias(\"winner\"),\n",
194+
" col(\"US_open\").alias(\"winner\"),\n",
195+
" col(\"Au_open\").alias(\"winner\")\n",
196+
" )\n",
197+
" ).alias(\"player_id\"))"
198+
]
199+
},
200+
{
201+
"cell_type": "code",
202+
"execution_count": 0,
203+
"metadata": {
204+
"application/vnd.databricks.v1+cell": {
205+
"cellMetadata": {
206+
"byteLimit": 2048000,
207+
"rowLimit": 10000
208+
},
209+
"inputWidgets": {},
210+
"nuid": "87400052-0296-4464-ac68-f1caa2bdb182",
211+
"showTitle": false,
212+
"tableResultSettingsMap": {},
213+
"title": ""
214+
}
215+
},
216+
"outputs": [],
217+
"source": [
218+
"wins_df_1783 = champ_unpivoted_df_1783\\\n",
219+
" .groupBy(\"player_id\").count()\\\n",
220+
" .withColumnRenamed(\"count\", \"grand_slams_count\")\n"
221+
]
222+
},
223+
{
224+
"cell_type": "code",
225+
"execution_count": 0,
226+
"metadata": {
227+
"application/vnd.databricks.v1+cell": {
228+
"cellMetadata": {
229+
"byteLimit": 2048000,
230+
"rowLimit": 10000
231+
},
232+
"inputWidgets": {},
233+
"nuid": "1437af27-4e1f-4a62-9bb2-583514223fac",
234+
"showTitle": false,
235+
"tableResultSettingsMap": {},
236+
"title": ""
237+
}
238+
},
239+
"outputs": [
240+
{
241+
"output_type": "stream",
242+
"name": "stdout",
243+
"output_type": "stream",
244+
"text": [
245+
"+---------+-----------+-----------------+\n|player_id|player_name|grand_slams_count|\n+---------+-----------+-----------------+\n| 1| Nadal| 7|\n| 2| Federer| 5|\n+---------+-----------+-----------------+\n\n"
246+
]
247+
}
248+
],
249+
"source": [
250+
"wins_df_1783\\\n",
251+
" .join(players_df_1783, on=\"player_id\")\\\n",
252+
" .select(\"player_id\", \"player_name\", \"grand_slams_count\").show()\n"
253+
]
148254
}
149255
],
150256
"metadata": {
151257
"application/vnd.databricks.v1+notebook": {
152-
"computePreferences": null,
258+
"computePreferences": {
259+
"hardware": {
260+
"accelerator": null,
261+
"gpuPoolId": null,
262+
"memory": null
263+
}
264+
},
153265
"dashboards": [],
154266
"environmentMetadata": {
155267
"base_environment": "",
156-
"environment_version": "1"
268+
"environment_version": "2"
157269
},
158270
"inputWidgetPreferences": null,
159271
"language": "python",

Solved/1789. Primary Department for Each Employee (Easy)-(Solved).ipynb

Lines changed: 90 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
"cell_type": "markdown",
55
"metadata": {
66
"application/vnd.databricks.v1+cell": {
7-
"cellMetadata": {},
7+
"cellMetadata": {
8+
"byteLimit": 2048000,
9+
"rowLimit": 10000
10+
},
811
"inputWidgets": {},
912
"nuid": "fadf709d-de3d-4bfc-b69f-3b489de68bef",
1013
"showTitle": false,
@@ -21,7 +24,10 @@
2124
"execution_count": 0,
2225
"metadata": {
2326
"application/vnd.databricks.v1+cell": {
24-
"cellMetadata": {},
27+
"cellMetadata": {
28+
"byteLimit": 2048000,
29+
"rowLimit": 10000
30+
},
2531
"inputWidgets": {},
2632
"nuid": "d4a1fef1-aa99-469c-b35f-9b3f3e81c9f7",
2733
"showTitle": false,
@@ -40,7 +46,10 @@
4046
"cell_type": "markdown",
4147
"metadata": {
4248
"application/vnd.databricks.v1+cell": {
43-
"cellMetadata": {},
49+
"cellMetadata": {
50+
"byteLimit": 2048000,
51+
"rowLimit": 10000
52+
},
4453
"inputWidgets": {},
4554
"nuid": "78daa073-80d8-4f7e-9cfa-10f6eb568909",
4655
"showTitle": false,
@@ -106,15 +115,27 @@
106115
"execution_count": 0,
107116
"metadata": {
108117
"application/vnd.databricks.v1+cell": {
109-
"cellMetadata": {},
118+
"cellMetadata": {
119+
"byteLimit": 2048000,
120+
"rowLimit": 10000
121+
},
110122
"inputWidgets": {},
111123
"nuid": "7b78bc27-26cd-4523-8646-7369baf68fcb",
112124
"showTitle": false,
113125
"tableResultSettingsMap": {},
114126
"title": ""
115127
}
116128
},
117-
"outputs": [],
129+
"outputs": [
130+
{
131+
"output_type": "stream",
132+
"name": "stdout",
133+
"output_type": "stream",
134+
"text": [
135+
"+-----------+-------------+------------+\n|employee_id|department_id|primary_flag|\n+-----------+-------------+------------+\n| 1| 1| N|\n| 2| 1| Y|\n| 2| 2| N|\n| 3| 3| N|\n| 4| 2| N|\n| 4| 3| Y|\n| 4| 4| N|\n+-----------+-------------+------------+\n\n"
136+
]
137+
}
138+
],
118139
"source": [
119140
"employee_data_1789 = [\n",
120141
" (1, 1, \"N\"),\n",
@@ -130,15 +151,77 @@
130151
"employee_df_1789 = spark.createDataFrame(employee_data_1789, employee_columns__1789)\n",
131152
"employee_df_1789.show()\n"
132153
]
154+
},
155+
{
156+
"cell_type": "code",
157+
"execution_count": 0,
158+
"metadata": {
159+
"application/vnd.databricks.v1+cell": {
160+
"cellMetadata": {
161+
"byteLimit": 2048000,
162+
"rowLimit": 10000
163+
},
164+
"inputWidgets": {},
165+
"nuid": "e49c5d56-b553-4979-bef8-3e016284a87f",
166+
"showTitle": false,
167+
"tableResultSettingsMap": {},
168+
"title": ""
169+
}
170+
},
171+
"outputs": [],
172+
"source": [
173+
"emp_count_df_1789 = employee_df_1789\\\n",
174+
" .groupBy(\"employee_id\").agg(count(\"*\").alias(\"dept_count\"))"
175+
]
176+
},
177+
{
178+
"cell_type": "code",
179+
"execution_count": 0,
180+
"metadata": {
181+
"application/vnd.databricks.v1+cell": {
182+
"cellMetadata": {
183+
"byteLimit": 2048000,
184+
"rowLimit": 10000
185+
},
186+
"inputWidgets": {},
187+
"nuid": "353a8e93-7c74-4297-a402-6573e1fdcf1e",
188+
"showTitle": false,
189+
"tableResultSettingsMap": {},
190+
"title": ""
191+
}
192+
},
193+
"outputs": [
194+
{
195+
"output_type": "stream",
196+
"name": "stdout",
197+
"output_type": "stream",
198+
"text": [
199+
"+-----------+-------------+\n|employee_id|department_id|\n+-----------+-------------+\n| 1| 1|\n| 2| 1|\n| 3| 3|\n| 4| 3|\n+-----------+-------------+\n\n"
200+
]
201+
}
202+
],
203+
"source": [
204+
"employee_df_1789\\\n",
205+
" .join(emp_count_df_1789, on=\"employee_id\")\\\n",
206+
" .filter(\n",
207+
" (col(\"primary_flag\") == \"Y\") | (col(\"dept_count\") == 1))\\\n",
208+
" .select(\"employee_id\", \"department_id\").show()"
209+
]
133210
}
134211
],
135212
"metadata": {
136213
"application/vnd.databricks.v1+notebook": {
137-
"computePreferences": null,
214+
"computePreferences": {
215+
"hardware": {
216+
"accelerator": null,
217+
"gpuPoolId": null,
218+
"memory": null
219+
}
220+
},
138221
"dashboards": [],
139222
"environmentMetadata": {
140223
"base_environment": "",
141-
"environment_version": "1"
224+
"environment_version": "2"
142225
},
143226
"inputWidgetPreferences": null,
144227
"language": "python",

0 commit comments

Comments
 (0)