Skip to content

Commit 3dd748c

Browse files
author
bitoollearner
committed
LeetCode Pyspark Solution
1 parent 0b98d21 commit 3dd748c

5 files changed

+470
-35
lines changed

Solved/1571. Warehouse Manager (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": "44cd2565-3e27-417c-8c6c-7b2d8f1b94c5",
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": "e6da106f-4e0b-41e0-9f75-6e329535d5c0",
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": "f0267197-68d5-4180-8492-6999c905acb1",
4655
"showTitle": false,
@@ -128,15 +137,27 @@
128137
"execution_count": 0,
129138
"metadata": {
130139
"application/vnd.databricks.v1+cell": {
131-
"cellMetadata": {},
140+
"cellMetadata": {
141+
"byteLimit": 2048000,
142+
"rowLimit": 10000
143+
},
132144
"inputWidgets": {},
133145
"nuid": "e98c62a4-8cd4-48d6-9337-86cf54c19ddd",
134146
"showTitle": false,
135147
"tableResultSettingsMap": {},
136148
"title": ""
137149
}
138150
},
139-
"outputs": [],
151+
"outputs": [
152+
{
153+
"output_type": "stream",
154+
"name": "stdout",
155+
"output_type": "stream",
156+
"text": [
157+
"+--------+----------+-----+\n| name|product_id|units|\n+--------+----------+-----+\n|LCHouse1| 1| 1|\n|LCHouse1| 2| 10|\n|LCHouse1| 3| 5|\n|LCHouse2| 1| 2|\n|LCHouse2| 2| 2|\n|LCHouse3| 4| 1|\n+--------+----------+-----+\n\n+----------+------------+-----+------+------+\n|product_id|product_name|Width|Length|Height|\n+----------+------------+-----+------+------+\n| 1| LC-TV| 5| 50| 40|\n| 2| LC-KeyChain| 5| 5| 5|\n| 3| LC-Phone| 2| 10| 10|\n| 4| LC-T-Shirt| 4| 10| 20|\n+----------+------------+-----+------+------+\n\n"
158+
]
159+
}
160+
],
140161
"source": [
141162
"warehouse_data_1571 = [\n",
142163
" (\"LCHouse1\", 1, 1),\n",
@@ -162,15 +183,77 @@
162183
"products_df_1571 = spark.createDataFrame(products_data_1571, products_columns_1571)\n",
163184
"products_df_1571.show()"
164185
]
186+
},
187+
{
188+
"cell_type": "code",
189+
"execution_count": 0,
190+
"metadata": {
191+
"application/vnd.databricks.v1+cell": {
192+
"cellMetadata": {
193+
"byteLimit": 2048000,
194+
"rowLimit": 10000
195+
},
196+
"inputWidgets": {},
197+
"nuid": "f21f909a-7256-407e-9d15-1debceb99bda",
198+
"showTitle": false,
199+
"tableResultSettingsMap": {},
200+
"title": ""
201+
}
202+
},
203+
"outputs": [],
204+
"source": [
205+
"joined_df_1571 = warehouse_df_1571\\\n",
206+
" .join(products_df_1571, on=\"product_id\")\\\n",
207+
" .withColumn(\"product_volume\", col(\"Width\") * col(\"Length\") * col(\"Height\"))\\\n",
208+
" .withColumn(\"total_volume\", col(\"product_volume\") * col(\"units\"))"
209+
]
210+
},
211+
{
212+
"cell_type": "code",
213+
"execution_count": 0,
214+
"metadata": {
215+
"application/vnd.databricks.v1+cell": {
216+
"cellMetadata": {
217+
"byteLimit": 2048000,
218+
"rowLimit": 10000
219+
},
220+
"inputWidgets": {},
221+
"nuid": "0747c320-99ce-4b45-a98b-d75e8bdc2fe1",
222+
"showTitle": false,
223+
"tableResultSettingsMap": {},
224+
"title": ""
225+
}
226+
},
227+
"outputs": [
228+
{
229+
"output_type": "stream",
230+
"name": "stdout",
231+
"output_type": "stream",
232+
"text": [
233+
"+--------------+------+\n|warehouse_name|volume|\n+--------------+------+\n| LCHouse1| 12250|\n| LCHouse2| 20250|\n| LCHouse3| 800|\n+--------------+------+\n\n"
234+
]
235+
}
236+
],
237+
"source": [
238+
"joined_df_1571\\\n",
239+
" .groupBy(\"name\").agg(sum(\"total_volume\").alias(\"volume\"))\\\n",
240+
" .withColumnRenamed(\"name\", \"warehouse_name\").show()"
241+
]
165242
}
166243
],
167244
"metadata": {
168245
"application/vnd.databricks.v1+notebook": {
169-
"computePreferences": null,
246+
"computePreferences": {
247+
"hardware": {
248+
"accelerator": null,
249+
"gpuPoolId": null,
250+
"memory": null
251+
}
252+
},
170253
"dashboards": [],
171254
"environmentMetadata": {
172255
"base_environment": "",
173-
"environment_version": "1"
256+
"environment_version": "2"
174257
},
175258
"inputWidgetPreferences": null,
176259
"language": "python",

Solved/1581. Customer Who Visited but Did Not Make Any Transactions (Easy)-(Solved).ipynb

Lines changed: 89 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": "44cd2565-3e27-417c-8c6c-7b2d8f1b94c5",
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": "e6da106f-4e0b-41e0-9f75-6e329535d5c0",
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": "f0267197-68d5-4180-8492-6999c905acb1",
4655
"showTitle": false,
@@ -123,15 +132,27 @@
123132
"execution_count": 0,
124133
"metadata": {
125134
"application/vnd.databricks.v1+cell": {
126-
"cellMetadata": {},
135+
"cellMetadata": {
136+
"byteLimit": 2048000,
137+
"rowLimit": 10000
138+
},
127139
"inputWidgets": {},
128140
"nuid": "e98c62a4-8cd4-48d6-9337-86cf54c19ddd",
129141
"showTitle": false,
130142
"tableResultSettingsMap": {},
131143
"title": ""
132144
}
133145
},
134-
"outputs": [],
146+
"outputs": [
147+
{
148+
"output_type": "stream",
149+
"name": "stdout",
150+
"output_type": "stream",
151+
"text": [
152+
"+--------+-----------+\n|visit_id|customer_id|\n+--------+-----------+\n| 1| 23|\n| 2| 9|\n| 4| 30|\n| 5| 54|\n| 6| 96|\n| 7| 54|\n| 8| 54|\n+--------+-----------+\n\n+--------------+--------+------+\n|transaction_id|visit_id|amount|\n+--------------+--------+------+\n| 2| 5| 310|\n| 3| 5| 300|\n| 9| 5| 200|\n| 12| 1| 910|\n| 13| 2| 970|\n+--------------+--------+------+\n\n"
153+
]
154+
}
155+
],
135156
"source": [
136157
"visits_data_1581 = [\n",
137158
" (1, 23), (2, 9), (4, 30), (5, 54), (6, 96), (7, 54), (8, 54)\n",
@@ -149,15 +170,76 @@
149170
"transactions_df_1581 = spark.createDataFrame(transactions_data_1581, transactions_columns_1581)\n",
150171
"transactions_df_1581.show()"
151172
]
173+
},
174+
{
175+
"cell_type": "code",
176+
"execution_count": 0,
177+
"metadata": {
178+
"application/vnd.databricks.v1+cell": {
179+
"cellMetadata": {
180+
"byteLimit": 2048000,
181+
"rowLimit": 10000
182+
},
183+
"inputWidgets": {},
184+
"nuid": "755bd799-c5a1-4a0f-8c45-a8225d351ed2",
185+
"showTitle": false,
186+
"tableResultSettingsMap": {},
187+
"title": ""
188+
}
189+
},
190+
"outputs": [],
191+
"source": [
192+
"no_trans_df_1581 = visits_df_1581\\\n",
193+
" .join( transactions_df_1581, on=\"visit_id\", how=\"left\")\\\n",
194+
" .filter(col(\"transaction_id\").isNull())"
195+
]
196+
},
197+
{
198+
"cell_type": "code",
199+
"execution_count": 0,
200+
"metadata": {
201+
"application/vnd.databricks.v1+cell": {
202+
"cellMetadata": {
203+
"byteLimit": 2048000,
204+
"rowLimit": 10000
205+
},
206+
"inputWidgets": {},
207+
"nuid": "ea9566fb-9ad9-46f3-ad52-d6072a450da3",
208+
"showTitle": false,
209+
"tableResultSettingsMap": {},
210+
"title": ""
211+
}
212+
},
213+
"outputs": [
214+
{
215+
"output_type": "stream",
216+
"name": "stdout",
217+
"output_type": "stream",
218+
"text": [
219+
"+-----------+--------------+\n|customer_id|count_no_trans|\n+-----------+--------------+\n| 30| 1|\n| 96| 1|\n| 54| 2|\n+-----------+--------------+\n\n"
220+
]
221+
}
222+
],
223+
"source": [
224+
"no_trans_df_1581\\\n",
225+
" .groupBy(\"customer_id\") \\\n",
226+
" .agg(count(\"visit_id\").alias(\"count_no_trans\")).show()"
227+
]
152228
}
153229
],
154230
"metadata": {
155231
"application/vnd.databricks.v1+notebook": {
156-
"computePreferences": null,
232+
"computePreferences": {
233+
"hardware": {
234+
"accelerator": null,
235+
"gpuPoolId": null,
236+
"memory": null
237+
}
238+
},
157239
"dashboards": [],
158240
"environmentMetadata": {
159241
"base_environment": "",
160-
"environment_version": "1"
242+
"environment_version": "2"
161243
},
162244
"inputWidgetPreferences": null,
163245
"language": "python",

0 commit comments

Comments
 (0)