Skip to content

Commit bca1030

Browse files
author
bitoollearner
committed
Leetcode Pyspark Solution
1 parent 201fbe4 commit bca1030

4 files changed

+480
-28
lines changed

Solved/1350. Students With Invalid Departments (Easy)-(Solved).ipynb

Lines changed: 69 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": "516a210c-988a-43a8-9004-cb03ad7c2481",
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": "9333a394-5bb0-4f1f-8f10-60b56c9fb68e",
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": "b718c875-1030-4207-a68c-5fad8635212a",
4655
"showTitle": false,
@@ -121,15 +130,27 @@
121130
"execution_count": 0,
122131
"metadata": {
123132
"application/vnd.databricks.v1+cell": {
124-
"cellMetadata": {},
133+
"cellMetadata": {
134+
"byteLimit": 2048000,
135+
"rowLimit": 10000
136+
},
125137
"inputWidgets": {},
126138
"nuid": "01e4d778-a8a5-48a7-8ebb-7033f0b84d62",
127139
"showTitle": false,
128140
"tableResultSettingsMap": {},
129141
"title": ""
130142
}
131143
},
132-
"outputs": [],
144+
"outputs": [
145+
{
146+
"output_type": "stream",
147+
"name": "stdout",
148+
"output_type": "stream",
149+
"text": [
150+
"+---+--------------------+\n| id| name|\n+---+--------------------+\n| 1|Electrical Engine...|\n| 7|Computer Engineering|\n| 13|Business Administ...|\n+---+--------------------+\n\n+---+--------+-------------+\n| id| name|department_id|\n+---+--------+-------------+\n| 23| Alice| 1|\n| 1| Bob| 7|\n| 5|Jennifer| 13|\n| 2| John| 14|\n| 4| Jasmine| 77|\n| 3| Steve| 74|\n| 6| Luis| 1|\n| 8|Jonathan| 7|\n| 7| Daiana| 33|\n| 11|Madelynn| 1|\n+---+--------+-------------+\n\n"
151+
]
152+
}
153+
],
133154
"source": [
134155
"departments_data_1350 = [\n",
135156
" (1, \"Electrical Engineering\"),\n",
@@ -151,15 +172,56 @@
151172
"students_df_1350 = spark.createDataFrame(students_data_1350, students_columns_1350)\n",
152173
"students_df_1350.show()"
153174
]
175+
},
176+
{
177+
"cell_type": "code",
178+
"execution_count": 0,
179+
"metadata": {
180+
"application/vnd.databricks.v1+cell": {
181+
"cellMetadata": {
182+
"byteLimit": 2048000,
183+
"rowLimit": 10000
184+
},
185+
"inputWidgets": {},
186+
"nuid": "a60d7a29-4a68-4d7f-97fd-3a80fa9ca61a",
187+
"showTitle": false,
188+
"tableResultSettingsMap": {},
189+
"title": ""
190+
}
191+
},
192+
"outputs": [
193+
{
194+
"output_type": "stream",
195+
"name": "stdout",
196+
"output_type": "stream",
197+
"text": [
198+
"+---+-------+\n| id| name|\n+---+-------+\n| 2| John|\n| 4|Jasmine|\n| 3| Steve|\n| 7| Daiana|\n+---+-------+\n\n"
199+
]
200+
}
201+
],
202+
"source": [
203+
"students_df_1350\\\n",
204+
" .join(\n",
205+
" departments_df_1350,\n",
206+
" students_df_1350.department_id == departments_df_1350.id,\n",
207+
" how=\"left_anti\"\n",
208+
" ).select(\"id\", \"name\").show()"
209+
]
154210
}
155211
],
156212
"metadata": {
157213
"application/vnd.databricks.v1+notebook": {
158-
"computePreferences": null,
214+
"computePreferences": {
215+
"hardware": {
216+
"accelerator": null,
217+
"gpuPoolId": null,
218+
"memory": null
219+
}
220+
},
159221
"dashboards": [],
160222
"environmentMetadata": {
161223
"base_environment": "",
162-
"environment_version": "1"
224+
"environment_version": "2"
163225
},
164226
"inputWidgetPreferences": null,
165227
"language": "python",

Solved/1355. Activity Participants (Medium)-(Solved).ipynb

Lines changed: 135 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": "516a210c-988a-43a8-9004-cb03ad7c2481",
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": "9333a394-5bb0-4f1f-8f10-60b56c9fb68e",
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": "b718c875-1030-4207-a68c-5fad8635212a",
4655
"showTitle": false,
@@ -119,15 +128,27 @@
119128
"execution_count": 0,
120129
"metadata": {
121130
"application/vnd.databricks.v1+cell": {
122-
"cellMetadata": {},
131+
"cellMetadata": {
132+
"byteLimit": 2048000,
133+
"rowLimit": 10000
134+
},
123135
"inputWidgets": {},
124136
"nuid": "01e4d778-a8a5-48a7-8ebb-7033f0b84d62",
125137
"showTitle": false,
126138
"tableResultSettingsMap": {},
127139
"title": ""
128140
}
129141
},
130-
"outputs": [],
142+
"outputs": [
143+
{
144+
"output_type": "stream",
145+
"name": "stdout",
146+
"output_type": "stream",
147+
"text": [
148+
"+---+-----------+------------+\n| id| name| activity|\n+---+-----------+------------+\n| 1|Jonathan D.| Eating|\n| 2| Jade W.| Singing|\n| 3| Victor J.| Singing|\n| 4| Elvis Q.| Eating|\n| 5| Daniel A.| Eating|\n| 6| Bob B.|Horse Riding|\n+---+-----------+------------+\n\n+---+------------+\n| id| name|\n+---+------------+\n| 1| Eating|\n| 2| Singing|\n| 3|Horse Riding|\n+---+------------+\n\n"
149+
]
150+
}
151+
],
131152
"source": [
132153
"friends_data_1355 = [\n",
133154
" (1, \"Jonathan D.\", \"Eating\"),\n",
@@ -150,15 +171,122 @@
150171
"activities_df_1355 = spark.createDataFrame(activities_data_1355, activities_columns_1355)\n",
151172
"activities_df_1355.show()"
152173
]
174+
},
175+
{
176+
"cell_type": "code",
177+
"execution_count": 0,
178+
"metadata": {
179+
"application/vnd.databricks.v1+cell": {
180+
"cellMetadata": {
181+
"byteLimit": 2048000,
182+
"rowLimit": 10000
183+
},
184+
"inputWidgets": {},
185+
"nuid": "2c20b089-1230-43b5-b947-17ab788c7693",
186+
"showTitle": false,
187+
"tableResultSettingsMap": {},
188+
"title": ""
189+
}
190+
},
191+
"outputs": [],
192+
"source": [
193+
"activity_counts_df_1355 = friends_df_1355.groupBy(\"activity\").agg(count(\"*\").alias(\"participants\"))"
194+
]
195+
},
196+
{
197+
"cell_type": "code",
198+
"execution_count": 0,
199+
"metadata": {
200+
"application/vnd.databricks.v1+cell": {
201+
"cellMetadata": {
202+
"byteLimit": 2048000,
203+
"rowLimit": 10000
204+
},
205+
"inputWidgets": {},
206+
"nuid": "dfecf9e8-2fbc-4bd1-99ca-530a93ed409d",
207+
"showTitle": false,
208+
"tableResultSettingsMap": {},
209+
"title": ""
210+
}
211+
},
212+
"outputs": [],
213+
"source": [
214+
"max_count_1355 = activity_counts_df_1355.agg(max(\"participants\")).collect()[0][0]\n",
215+
"min_count_1355 = activity_counts_df_1355.agg(min(\"participants\")).collect()[0][0]"
216+
]
217+
},
218+
{
219+
"cell_type": "code",
220+
"execution_count": 0,
221+
"metadata": {
222+
"application/vnd.databricks.v1+cell": {
223+
"cellMetadata": {
224+
"byteLimit": 2048000,
225+
"rowLimit": 10000
226+
},
227+
"inputWidgets": {},
228+
"nuid": "c29ddf9f-dfb6-4433-a361-9a0bac0d0a36",
229+
"showTitle": false,
230+
"tableResultSettingsMap": {},
231+
"title": ""
232+
}
233+
},
234+
"outputs": [],
235+
"source": [
236+
"filtered_df_1355 = activity_counts_df_1355\\\n",
237+
" .filter(\n",
238+
" (col(\"participants\") != max_count_1355) & (col(\"participants\") != min_count_1355)\n",
239+
" )"
240+
]
241+
},
242+
{
243+
"cell_type": "code",
244+
"execution_count": 0,
245+
"metadata": {
246+
"application/vnd.databricks.v1+cell": {
247+
"cellMetadata": {
248+
"byteLimit": 2048000,
249+
"rowLimit": 10000
250+
},
251+
"inputWidgets": {},
252+
"nuid": "5bf61f93-6fa4-4104-814e-3b68e27f43e9",
253+
"showTitle": false,
254+
"tableResultSettingsMap": {},
255+
"title": ""
256+
}
257+
},
258+
"outputs": [
259+
{
260+
"output_type": "stream",
261+
"name": "stdout",
262+
"output_type": "stream",
263+
"text": [
264+
"+--------+\n|activity|\n+--------+\n| Singing|\n+--------+\n\n"
265+
]
266+
}
267+
],
268+
"source": [
269+
"filtered_df_1355\\\n",
270+
" .join(\n",
271+
" activities_df_1355,filtered_df_1355.activity == activities_df_1355.name,how=\"inner\"\n",
272+
" )\\\n",
273+
" .select(\"name\").withColumnRenamed(\"name\", \"activity\").show()"
274+
]
153275
}
154276
],
155277
"metadata": {
156278
"application/vnd.databricks.v1+notebook": {
157-
"computePreferences": null,
279+
"computePreferences": {
280+
"hardware": {
281+
"accelerator": null,
282+
"gpuPoolId": null,
283+
"memory": null
284+
}
285+
},
158286
"dashboards": [],
159287
"environmentMetadata": {
160288
"base_environment": "",
161-
"environment_version": "1"
289+
"environment_version": "2"
162290
},
163291
"inputWidgetPreferences": null,
164292
"language": "python",

0 commit comments

Comments
 (0)