Skip to content

Commit f258e79

Browse files
author
bitoollearner
committed
LeetCode Pyspark Solution
1 parent 8b41f0e commit f258e79

10 files changed

+1850
-70
lines changed

Solved/2362. Generate the Invoice (Hard)-(Solved).ipynb

Lines changed: 133 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": "4e9d71b1-88e9-445c-820a-b842e217a4e7",
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": "e6624c4d-8cb8-4ac4-8c72-81d1b4ecdb57",
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": "c3788fe5-f4c9-420f-ad72-ec1a39310a2a",
4655
"showTitle": false,
@@ -117,15 +126,27 @@
117126
"execution_count": 0,
118127
"metadata": {
119128
"application/vnd.databricks.v1+cell": {
120-
"cellMetadata": {},
129+
"cellMetadata": {
130+
"byteLimit": 2048000,
131+
"rowLimit": 10000
132+
},
121133
"inputWidgets": {},
122134
"nuid": "137536ea-9bec-4171-8dc2-4bf8b0d771b4",
123135
"showTitle": false,
124136
"tableResultSettingsMap": {},
125137
"title": ""
126138
}
127139
},
128-
"outputs": [],
140+
"outputs": [
141+
{
142+
"output_type": "stream",
143+
"name": "stdout",
144+
"output_type": "stream",
145+
"text": [
146+
"+----------+-----+\n|product_id|price|\n+----------+-----+\n| 1| 100|\n| 2| 200|\n+----------+-----+\n\n+----------+----------+--------+\n|invoice_id|product_id|quantity|\n+----------+----------+--------+\n| 1| 1| 2|\n| 3| 2| 1|\n| 2| 2| 3|\n| 2| 1| 4|\n| 4| 1| 10|\n+----------+----------+--------+\n\n"
147+
]
148+
}
149+
],
129150
"source": [
130151
"products_data_2362 = [\n",
131152
" (1, 100),\n",
@@ -148,15 +169,120 @@
148169
"purchases_df_2362 = spark.createDataFrame(purchases_data_2362, purchases_columns_2362)\n",
149170
"purchases_df_2362.show()\n"
150171
]
172+
},
173+
{
174+
"cell_type": "code",
175+
"execution_count": 0,
176+
"metadata": {
177+
"application/vnd.databricks.v1+cell": {
178+
"cellMetadata": {
179+
"byteLimit": 2048000,
180+
"rowLimit": 10000
181+
},
182+
"inputWidgets": {},
183+
"nuid": "4645e803-c679-4c17-aba0-99aea4e7b4cc",
184+
"showTitle": false,
185+
"tableResultSettingsMap": {},
186+
"title": ""
187+
}
188+
},
189+
"outputs": [],
190+
"source": [
191+
"invoice_df_2362 = purchases_df_2362.join(products_df_2362, on=\"product_id\")\\\n",
192+
" .withColumn(\"price\", col(\"price\") * col(\"quantity\")) "
193+
]
194+
},
195+
{
196+
"cell_type": "code",
197+
"execution_count": 0,
198+
"metadata": {
199+
"application/vnd.databricks.v1+cell": {
200+
"cellMetadata": {
201+
"byteLimit": 2048000,
202+
"rowLimit": 10000
203+
},
204+
"inputWidgets": {},
205+
"nuid": "004813ed-12f6-4b6d-9122-4103befe3f15",
206+
"showTitle": false,
207+
"tableResultSettingsMap": {},
208+
"title": ""
209+
}
210+
},
211+
"outputs": [],
212+
"source": [
213+
"total_invoice_df_2362 = invoice_df_2362\\\n",
214+
" .groupBy(\"invoice_id\").agg(sum(\"price\").alias(\"total_price\"))"
215+
]
216+
},
217+
{
218+
"cell_type": "code",
219+
"execution_count": 0,
220+
"metadata": {
221+
"application/vnd.databricks.v1+cell": {
222+
"cellMetadata": {
223+
"byteLimit": 2048000,
224+
"rowLimit": 10000
225+
},
226+
"inputWidgets": {},
227+
"nuid": "78dc4937-f5a1-4c35-83de-a2114d81c4e9",
228+
"showTitle": false,
229+
"tableResultSettingsMap": {},
230+
"title": ""
231+
}
232+
},
233+
"outputs": [],
234+
"source": [
235+
"max_invoice_id_2362 = total_invoice_df_2362\\\n",
236+
" .orderBy(desc(\"total_price\"), asc(\"invoice_id\")).limit(1)\\\n",
237+
" .select(\"invoice_id\")"
238+
]
239+
},
240+
{
241+
"cell_type": "code",
242+
"execution_count": 0,
243+
"metadata": {
244+
"application/vnd.databricks.v1+cell": {
245+
"cellMetadata": {
246+
"byteLimit": 2048000,
247+
"rowLimit": 10000
248+
},
249+
"inputWidgets": {},
250+
"nuid": "4ae26f5e-47ad-48b6-9711-9aec055b11c5",
251+
"showTitle": false,
252+
"tableResultSettingsMap": {},
253+
"title": ""
254+
}
255+
},
256+
"outputs": [
257+
{
258+
"output_type": "stream",
259+
"name": "stdout",
260+
"output_type": "stream",
261+
"text": [
262+
"+----------+--------+-----+\n|product_id|quantity|price|\n+----------+--------+-----+\n| 2| 3| 600|\n| 1| 4| 400|\n+----------+--------+-----+\n\n"
263+
]
264+
}
265+
],
266+
"source": [
267+
"invoice_df_2362\\\n",
268+
" .join(max_invoice_id_2362, on=\"invoice_id\")\\\n",
269+
" .select(\"product_id\", \"quantity\", \"price\").show()"
270+
]
151271
}
152272
],
153273
"metadata": {
154274
"application/vnd.databricks.v1+notebook": {
155-
"computePreferences": null,
275+
"computePreferences": {
276+
"hardware": {
277+
"accelerator": null,
278+
"gpuPoolId": null,
279+
"memory": null
280+
}
281+
},
156282
"dashboards": [],
157283
"environmentMetadata": {
158284
"base_environment": "",
159-
"environment_version": "1"
285+
"environment_version": "2"
160286
},
161287
"inputWidgetPreferences": null,
162288
"language": "python",

Solved/2372. Calculate the Influence of Each Salesperson (Medium)-(Solved).ipynb

Lines changed: 118 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": "4e9d71b1-88e9-445c-820a-b842e217a4e7",
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": "e6624c4d-8cb8-4ac4-8c72-81d1b4ecdb57",
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": "c3788fe5-f4c9-420f-ad72-ec1a39310a2a",
4655
"showTitle": false,
@@ -147,15 +156,27 @@
147156
"execution_count": 0,
148157
"metadata": {
149158
"application/vnd.databricks.v1+cell": {
150-
"cellMetadata": {},
159+
"cellMetadata": {
160+
"byteLimit": 2048000,
161+
"rowLimit": 10000
162+
},
151163
"inputWidgets": {},
152164
"nuid": "137536ea-9bec-4171-8dc2-4bf8b0d771b4",
153165
"showTitle": false,
154166
"tableResultSettingsMap": {},
155167
"title": ""
156168
}
157169
},
158-
"outputs": [],
170+
"outputs": [
171+
{
172+
"output_type": "stream",
173+
"name": "stdout",
174+
"output_type": "stream",
175+
"text": [
176+
"+--------------+-----+\n|salesperson_id| name|\n+--------------+-----+\n| 1|Alice|\n| 2| Bob|\n| 3|Jerry|\n+--------------+-----+\n\n+-----------+--------------+\n|customer_id|salesperson_id|\n+-----------+--------------+\n| 1| 1|\n| 2| 1|\n| 3| 2|\n+-----------+--------------+\n\n+-------+-----------+-----+\n|sale_id|customer_id|price|\n+-------+-----------+-----+\n| 1| 2| 892|\n| 2| 1| 354|\n| 3| 3| 988|\n| 4| 3| 856|\n+-------+-----------+-----+\n\n"
177+
]
178+
}
179+
],
159180
"source": [
160181
"salesperson_data_2372 = [\n",
161182
" (1, \"Alice\"),\n",
@@ -188,15 +209,105 @@
188209
"sales_df_2372 = spark.createDataFrame(sales_data_2372, sales_columns_2372)\n",
189210
"sales_df_2372.show()"
190211
]
212+
},
213+
{
214+
"cell_type": "code",
215+
"execution_count": 0,
216+
"metadata": {
217+
"application/vnd.databricks.v1+cell": {
218+
"cellMetadata": {
219+
"byteLimit": 2048000,
220+
"rowLimit": 10000
221+
},
222+
"inputWidgets": {},
223+
"nuid": "12d32e4a-2c44-4b71-b12e-c9d387941b2a",
224+
"showTitle": false,
225+
"tableResultSettingsMap": {},
226+
"title": ""
227+
}
228+
},
229+
"outputs": [],
230+
"source": [
231+
"sales_with_salesperson_df_2372 = sales_df_2372\\\n",
232+
" .join(\n",
233+
" customer_df_2372,\n",
234+
" on=\"customer_id\",\n",
235+
" how=\"right\" \n",
236+
" )"
237+
]
238+
},
239+
{
240+
"cell_type": "code",
241+
"execution_count": 0,
242+
"metadata": {
243+
"application/vnd.databricks.v1+cell": {
244+
"cellMetadata": {
245+
"byteLimit": 2048000,
246+
"rowLimit": 10000
247+
},
248+
"inputWidgets": {},
249+
"nuid": "4a2486ae-47b8-406e-811f-6d5eac238dd1",
250+
"showTitle": false,
251+
"tableResultSettingsMap": {},
252+
"title": ""
253+
}
254+
},
255+
"outputs": [],
256+
"source": [
257+
"salesperson_total_df_2372 = sales_with_salesperson_df_2372\\\n",
258+
" .groupBy(\"salesperson_id\")\\\n",
259+
" .agg(sum(\"price\").alias(\"total\"))"
260+
]
261+
},
262+
{
263+
"cell_type": "code",
264+
"execution_count": 0,
265+
"metadata": {
266+
"application/vnd.databricks.v1+cell": {
267+
"cellMetadata": {
268+
"byteLimit": 2048000,
269+
"rowLimit": 10000
270+
},
271+
"inputWidgets": {},
272+
"nuid": "e8066013-5493-458e-9809-f1d1b3de411e",
273+
"showTitle": false,
274+
"tableResultSettingsMap": {},
275+
"title": ""
276+
}
277+
},
278+
"outputs": [
279+
{
280+
"output_type": "stream",
281+
"name": "stdout",
282+
"output_type": "stream",
283+
"text": [
284+
"+--------------+-----+-----+\n|salesperson_id| name|total|\n+--------------+-----+-----+\n| 1|Alice| 1246|\n| 2| Bob| 1844|\n| 3|Jerry| 0|\n+--------------+-----+-----+\n\n"
285+
]
286+
}
287+
],
288+
"source": [
289+
"salesperson_df_2372\\\n",
290+
" .join(\n",
291+
" salesperson_total_df_2372,\n",
292+
" on=\"salesperson_id\",\n",
293+
" how=\"left\"\n",
294+
" ).fillna(0, subset=[\"total\"]).show()"
295+
]
191296
}
192297
],
193298
"metadata": {
194299
"application/vnd.databricks.v1+notebook": {
195-
"computePreferences": null,
300+
"computePreferences": {
301+
"hardware": {
302+
"accelerator": null,
303+
"gpuPoolId": null,
304+
"memory": null
305+
}
306+
},
196307
"dashboards": [],
197308
"environmentMetadata": {
198309
"base_environment": "",
199-
"environment_version": "1"
310+
"environment_version": "2"
200311
},
201312
"inputWidgetPreferences": null,
202313
"language": "python",

0 commit comments

Comments
 (0)