Skip to content

Commit 7780899

Browse files
author
bitoollearner
committed
Pyspark Leetcode solution
1 parent bca1030 commit 7780899

4 files changed

+367
-29
lines changed

Solved/1378. Replace Employee ID With The Unique Identifier (Easy)-(Solved).ipynb

Lines changed: 66 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,
@@ -118,15 +127,27 @@
118127
"execution_count": 0,
119128
"metadata": {
120129
"application/vnd.databricks.v1+cell": {
121-
"cellMetadata": {},
130+
"cellMetadata": {
131+
"byteLimit": 2048000,
132+
"rowLimit": 10000
133+
},
122134
"inputWidgets": {},
123135
"nuid": "01e4d778-a8a5-48a7-8ebb-7033f0b84d62",
124136
"showTitle": false,
125137
"tableResultSettingsMap": {},
126138
"title": ""
127139
}
128140
},
129-
"outputs": [],
141+
"outputs": [
142+
{
143+
"output_type": "stream",
144+
"name": "stdout",
145+
"output_type": "stream",
146+
"text": [
147+
"+---+--------+\n| id| name|\n+---+--------+\n| 1| Alice|\n| 7| Bob|\n| 11| Meir|\n| 90| Winston|\n| 3|Jonathan|\n+---+--------+\n\n+---+---------+\n| id|unique_id|\n+---+---------+\n| 3| 1|\n| 11| 2|\n| 90| 3|\n+---+---------+\n\n"
148+
]
149+
}
150+
],
130151
"source": [
131152
"employees_data_1378 = [\n",
132153
" (1, \"Alice\"),\n",
@@ -150,15 +171,53 @@
150171
"employee_uni_df_1378 = spark.createDataFrame(employee_uni_data_1378, employee_uni_columns_1378)\n",
151172
"employee_uni_df_1378.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": "599a10a9-50ac-4053-97f7-89bce90ce713",
186+
"showTitle": false,
187+
"tableResultSettingsMap": {},
188+
"title": ""
189+
}
190+
},
191+
"outputs": [
192+
{
193+
"output_type": "stream",
194+
"name": "stdout",
195+
"output_type": "stream",
196+
"text": [
197+
"+---------+--------+\n|unique_id| name|\n+---------+--------+\n| NULL| Alice|\n| NULL| Bob|\n| 2| Meir|\n| 3| Winston|\n| 1|Jonathan|\n+---------+--------+\n\n"
198+
]
199+
}
200+
],
201+
"source": [
202+
"employees_df_1378\\\n",
203+
" .join(employee_uni_df_1378, on=\"id\", how=\"left\") \\\n",
204+
" .select(\"unique_id\", \"name\").show()"
205+
]
153206
}
154207
],
155208
"metadata": {
156209
"application/vnd.databricks.v1+notebook": {
157-
"computePreferences": null,
210+
"computePreferences": {
211+
"hardware": {
212+
"accelerator": null,
213+
"gpuPoolId": null,
214+
"memory": null
215+
}
216+
},
158217
"dashboards": [],
159218
"environmentMetadata": {
160219
"base_environment": "",
161-
"environment_version": "1"
220+
"environment_version": "2"
162221
},
163222
"inputWidgetPreferences": null,
164223
"language": "python",

Solved/1384. Total Sales Amount by Year (Hard)-(Solved).ipynb

Lines changed: 139 additions & 8 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|product_id|product_name|\n+----------+------------+\n| 1| LC Phone|\n| 2| LC T-Shirt|\n| 3| LC Keychain|\n+----------+------------+\n\n+----------+------------+----------+-------------------+------------------+\n|product_id|period_start|period_end|average_daily_sales| year_list|\n+----------+------------+----------+-------------------+------------------+\n| 1| 2019-01-25|2019-02-28| 100| [2019]|\n| 2| 2018-12-01|2020-01-01| 10|[2018, 2019, 2020]|\n| 3| 2019-12-01|2020-01-31| 1| [2019, 2020]|\n+----------+------------+----------+-------------------+------------------+\n\n"
151+
]
152+
}
153+
],
133154
"source": [
134155
"product_data_1384 = [\n",
135156
" (1, \"LC Phone\"),\n",
@@ -146,18 +167,128 @@
146167
" (3, \"2019-12-01\", \"2020-01-31\", 1)\n",
147168
"]\n",
148169
"sales_columns_1384 = [\"product_id\", \"period_start\", \"period_end\", \"average_daily_sales\"]\n",
149-
"sales_df_1384 = spark.createDataFrame(sales_data_1384, sales_columns_1384)\n",
170+
"sales_df_1384 = spark.createDataFrame(sales_data_1384, sales_columns_1384)\\\n",
171+
" .withColumn(\"period_start\", col(\"period_start\").cast(DateType())) \\\n",
172+
" .withColumn(\"period_end\", col(\"period_end\").cast(DateType()))\\\n",
173+
" .withColumn(\"year_list\",sequence(year(col(\"period_start\")), year(col(\"period_end\"))))\n",
174+
"\n",
150175
"sales_df_1384.show()"
151176
]
177+
},
178+
{
179+
"cell_type": "code",
180+
"execution_count": 0,
181+
"metadata": {
182+
"application/vnd.databricks.v1+cell": {
183+
"cellMetadata": {
184+
"byteLimit": 2048000,
185+
"rowLimit": 10000
186+
},
187+
"inputWidgets": {},
188+
"nuid": "e86cc1dd-71e8-4e41-b745-ed97f63b2c89",
189+
"showTitle": false,
190+
"tableResultSettingsMap": {},
191+
"title": ""
192+
}
193+
},
194+
"outputs": [],
195+
"source": [
196+
"sales_exploded_df_1384 = sales_df_1384.withColumn(\"report_year\", explode(col(\"year_list\")))"
197+
]
198+
},
199+
{
200+
"cell_type": "code",
201+
"execution_count": 0,
202+
"metadata": {
203+
"application/vnd.databricks.v1+cell": {
204+
"cellMetadata": {
205+
"byteLimit": 2048000,
206+
"rowLimit": 10000
207+
},
208+
"inputWidgets": {},
209+
"nuid": "cef75ba1-e2cf-4a23-a0ae-420b95e0c5cc",
210+
"showTitle": false,
211+
"tableResultSettingsMap": {},
212+
"title": ""
213+
}
214+
},
215+
"outputs": [],
216+
"source": [
217+
"sales_split_df_1384 = sales_exploded_df_1384\\\n",
218+
" .withColumn(\"start_date\",greatest(col(\"period_start\"), col(\"report_year\").cast(\"string\").cast(DateType())))\\\n",
219+
" .withColumn(\"end_date\",least(col(\"period_end\"),(col(\"report_year\") + lit(1)).cast(\"string\").cast(DateType()) - expr(\"INTERVAL 1 DAY\")))\n"
220+
]
221+
},
222+
{
223+
"cell_type": "code",
224+
"execution_count": 0,
225+
"metadata": {
226+
"application/vnd.databricks.v1+cell": {
227+
"cellMetadata": {
228+
"byteLimit": 2048000,
229+
"rowLimit": 10000
230+
},
231+
"inputWidgets": {},
232+
"nuid": "9005884e-330e-43ac-9fbd-52003cc83dd0",
233+
"showTitle": false,
234+
"tableResultSettingsMap": {},
235+
"title": ""
236+
}
237+
},
238+
"outputs": [],
239+
"source": [
240+
"sales_split_df_1384 = sales_split_df_1384\\\n",
241+
" .withColumn(\"days\",datediff(col(\"end_date\"), col(\"start_date\")) + lit(1))\\\n",
242+
" .withColumn(\"total_amount\",col(\"days\") * col(\"average_daily_sales\"))"
243+
]
244+
},
245+
{
246+
"cell_type": "code",
247+
"execution_count": 0,
248+
"metadata": {
249+
"application/vnd.databricks.v1+cell": {
250+
"cellMetadata": {
251+
"byteLimit": 2048000,
252+
"rowLimit": 10000
253+
},
254+
"inputWidgets": {},
255+
"nuid": "63b455c8-05b6-46aa-9988-8aa32e95239f",
256+
"showTitle": false,
257+
"tableResultSettingsMap": {},
258+
"title": ""
259+
}
260+
},
261+
"outputs": [
262+
{
263+
"output_type": "stream",
264+
"name": "stdout",
265+
"output_type": "stream",
266+
"text": [
267+
"+----------+------------+-----------+------------+\n|product_id|product_name|report_year|total_amount|\n+----------+------------+-----------+------------+\n| 1| LC Phone| 2019| 3500|\n| 2| LC T-Shirt| 2018| 310|\n| 2| LC T-Shirt| 2019| 3650|\n| 2| LC T-Shirt| 2020| 10|\n| 3| LC Keychain| 2019| 31|\n| 3| LC Keychain| 2020| 31|\n+----------+------------+-----------+------------+\n\n"
268+
]
269+
}
270+
],
271+
"source": [
272+
"sales_split_df_1384\\\n",
273+
" .join(product_df_1384, on=\"product_id\", how=\"left\") \\\n",
274+
" .select(\"product_id\", \"product_name\", \"report_year\", \"total_amount\") \\\n",
275+
" .orderBy(\"product_id\", \"report_year\").show()"
276+
]
152277
}
153278
],
154279
"metadata": {
155280
"application/vnd.databricks.v1+notebook": {
156-
"computePreferences": null,
281+
"computePreferences": {
282+
"hardware": {
283+
"accelerator": null,
284+
"gpuPoolId": null,
285+
"memory": null
286+
}
287+
},
157288
"dashboards": [],
158289
"environmentMetadata": {
159290
"base_environment": "",
160-
"environment_version": "1"
291+
"environment_version": "2"
161292
},
162293
"inputWidgetPreferences": null,
163294
"language": "python",

0 commit comments

Comments
 (0)