Skip to content

Commit 6e994f6

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

10 files changed

+1259
-70
lines changed

Solved/1853. Convert Date Format (Easy)-(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": "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,
@@ -92,15 +101,27 @@
92101
"execution_count": 0,
93102
"metadata": {
94103
"application/vnd.databricks.v1+cell": {
95-
"cellMetadata": {},
104+
"cellMetadata": {
105+
"byteLimit": 2048000,
106+
"rowLimit": 10000
107+
},
96108
"inputWidgets": {},
97109
"nuid": "7b78bc27-26cd-4523-8646-7369baf68fcb",
98110
"showTitle": false,
99111
"tableResultSettingsMap": {},
100112
"title": ""
101113
}
102114
},
103-
"outputs": [],
115+
"outputs": [
116+
{
117+
"output_type": "stream",
118+
"name": "stdout",
119+
"output_type": "stream",
120+
"text": [
121+
"+----------+\n| day|\n+----------+\n|2022-04-12|\n|2021-08-09|\n|2020-06-26|\n+----------+\n\n"
122+
]
123+
}
124+
],
104125
"source": [
105126
"days_data_1853 = [\n",
106127
" (\"2022-04-12\",),\n",
@@ -112,15 +133,120 @@
112133
"days_df_1853 = spark.createDataFrame(days_data_1853, days_columns_1853)\n",
113134
"days_df_1853.show()"
114135
]
136+
},
137+
{
138+
"cell_type": "code",
139+
"execution_count": 0,
140+
"metadata": {
141+
"application/vnd.databricks.v1+cell": {
142+
"cellMetadata": {
143+
"byteLimit": 2048000,
144+
"rowLimit": 10000
145+
},
146+
"inputWidgets": {},
147+
"nuid": "2de15cdd-29f9-4afe-9f82-d245b00484f9",
148+
"showTitle": false,
149+
"tableResultSettingsMap": {
150+
"0": {
151+
"dataGridStateBlob": "{\"version\":1,\"tableState\":{\"columnPinning\":{\"left\":[\"#row_number#\"],\"right\":[]},\"columnSizing\":{\"day\":178},\"columnVisibility\":{}},\"settings\":{\"columns\":{}},\"syncTimestamp\":1756317812080}",
152+
"filterBlob": null,
153+
"queryPlanFiltersBlob": null,
154+
"tableResultIndex": 0
155+
}
156+
},
157+
"title": ""
158+
}
159+
},
160+
"outputs": [
161+
{
162+
"output_type": "display_data",
163+
"data": {
164+
"text/html": [
165+
"<style scoped>\n",
166+
" .table-result-container {\n",
167+
" max-height: 300px;\n",
168+
" overflow: auto;\n",
169+
" }\n",
170+
" table, th, td {\n",
171+
" border: 1px solid black;\n",
172+
" border-collapse: collapse;\n",
173+
" }\n",
174+
" th, td {\n",
175+
" padding: 5px;\n",
176+
" }\n",
177+
" th {\n",
178+
" text-align: left;\n",
179+
" }\n",
180+
"</style><div class='table-result-container'><table class='table-result'><thead style='background-color: white'><tr><th>day</th></tr></thead><tbody><tr><td>Tuesday, April 12, 2022</td></tr><tr><td>Monday, August 9, 2021</td></tr><tr><td>Friday, June 26, 2020</td></tr></tbody></table></div>"
181+
]
182+
},
183+
"metadata": {
184+
"application/vnd.databricks.v1+output": {
185+
"addedWidgets": {},
186+
"aggData": [],
187+
"aggError": "",
188+
"aggOverflow": false,
189+
"aggSchema": [],
190+
"aggSeriesLimitReached": false,
191+
"aggType": "",
192+
"arguments": {},
193+
"columnCustomDisplayInfos": {},
194+
"data": [
195+
[
196+
"Tuesday, April 12, 2022"
197+
],
198+
[
199+
"Monday, August 9, 2021"
200+
],
201+
[
202+
"Friday, June 26, 2020"
203+
]
204+
],
205+
"datasetInfos": [],
206+
"dbfsResultPath": null,
207+
"isJsonSchema": true,
208+
"metadata": {},
209+
"overflow": false,
210+
"plotOptions": {
211+
"customPlotOptions": {},
212+
"displayType": "table",
213+
"pivotAggregation": null,
214+
"pivotColumns": null,
215+
"xColumns": null,
216+
"yColumns": null
217+
},
218+
"removedWidgets": [],
219+
"schema": [
220+
{
221+
"metadata": "{}",
222+
"name": "day",
223+
"type": "\"string\""
224+
}
225+
],
226+
"type": "table"
227+
}
228+
},
229+
"output_type": "display_data"
230+
}
231+
],
232+
"source": [
233+
"display(days_df_1853.withColumn( \"day\", date_format(\"day\", \"EEEE, MMMM d, yyyy\")))"
234+
]
115235
}
116236
],
117237
"metadata": {
118238
"application/vnd.databricks.v1+notebook": {
119-
"computePreferences": null,
239+
"computePreferences": {
240+
"hardware": {
241+
"accelerator": null,
242+
"gpuPoolId": null,
243+
"memory": null
244+
}
245+
},
120246
"dashboards": [],
121247
"environmentMetadata": {
122248
"base_environment": "",
123-
"environment_version": "1"
249+
"environment_version": "2"
124250
},
125251
"inputWidgetPreferences": null,
126252
"language": "python",

Solved/1867. Orders With Maximum Quantity Above Average (Medium)-(Solved).ipynb

Lines changed: 116 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,
@@ -126,15 +135,27 @@
126135
"execution_count": 0,
127136
"metadata": {
128137
"application/vnd.databricks.v1+cell": {
129-
"cellMetadata": {},
138+
"cellMetadata": {
139+
"byteLimit": 2048000,
140+
"rowLimit": 10000
141+
},
130142
"inputWidgets": {},
131143
"nuid": "7b78bc27-26cd-4523-8646-7369baf68fcb",
132144
"showTitle": false,
133145
"tableResultSettingsMap": {},
134146
"title": ""
135147
}
136148
},
137-
"outputs": [],
149+
"outputs": [
150+
{
151+
"output_type": "stream",
152+
"name": "stdout",
153+
"output_type": "stream",
154+
"text": [
155+
"+--------+----------+--------+\n|order_id|product_id|quantity|\n+--------+----------+--------+\n| 1| 1| 12|\n| 1| 2| 10|\n| 1| 3| 15|\n| 2| 1| 8|\n| 2| 4| 4|\n| 2| 5| 6|\n| 3| 3| 5|\n| 3| 4| 18|\n| 4| 5| 2|\n| 4| 6| 8|\n| 5| 7| 9|\n| 5| 8| 9|\n| 3| 9| 20|\n| 2| 9| 4|\n+--------+----------+--------+\n\n"
156+
]
157+
}
158+
],
138159
"source": [
139160
"orders_data_1867 = [\n",
140161
" (1, 1, 12),\n",
@@ -157,15 +178,103 @@
157178
"orders_df_1867 = spark.createDataFrame(orders_data_1867, orders_columns_1867)\n",
158179
"orders_df_1867.show()\n"
159180
]
181+
},
182+
{
183+
"cell_type": "code",
184+
"execution_count": 0,
185+
"metadata": {
186+
"application/vnd.databricks.v1+cell": {
187+
"cellMetadata": {
188+
"byteLimit": 2048000,
189+
"rowLimit": 10000
190+
},
191+
"inputWidgets": {},
192+
"nuid": "45bc923e-bde3-439c-b726-b9581c8e8132",
193+
"showTitle": false,
194+
"tableResultSettingsMap": {},
195+
"title": ""
196+
}
197+
},
198+
"outputs": [],
199+
"source": [
200+
"order_stats_df_1867 = orders_df_1867\\\n",
201+
" .groupBy(\"order_id\")\\\n",
202+
" .agg(\n",
203+
" (sum(\"quantity\") / count(\"product_id\")).alias(\"avg_qty\"),\n",
204+
" max(\"quantity\").alias(\"max_qty\")\n",
205+
" )"
206+
]
207+
},
208+
{
209+
"cell_type": "code",
210+
"execution_count": 0,
211+
"metadata": {
212+
"application/vnd.databricks.v1+cell": {
213+
"cellMetadata": {
214+
"byteLimit": 2048000,
215+
"rowLimit": 10000
216+
},
217+
"inputWidgets": {},
218+
"nuid": "1190269f-e9e6-44b5-9867-fd069b4205a4",
219+
"showTitle": false,
220+
"tableResultSettingsMap": {},
221+
"title": ""
222+
}
223+
},
224+
"outputs": [],
225+
"source": [
226+
"global_avg_max_df_1867 = order_stats_df_1867\\\n",
227+
" .agg(\n",
228+
" max(\"avg_qty\").alias(\"global_max_avg\")\n",
229+
" ).collect()[0][\"global_max_avg\"]\n"
230+
]
231+
},
232+
{
233+
"cell_type": "code",
234+
"execution_count": 0,
235+
"metadata": {
236+
"application/vnd.databricks.v1+cell": {
237+
"cellMetadata": {
238+
"byteLimit": 2048000,
239+
"rowLimit": 10000
240+
},
241+
"inputWidgets": {},
242+
"nuid": "77b8bbdf-349b-4552-9f24-276683d487b9",
243+
"showTitle": false,
244+
"tableResultSettingsMap": {},
245+
"title": ""
246+
}
247+
},
248+
"outputs": [
249+
{
250+
"output_type": "stream",
251+
"name": "stdout",
252+
"output_type": "stream",
253+
"text": [
254+
"+--------+\n|order_id|\n+--------+\n| 1|\n| 3|\n+--------+\n\n"
255+
]
256+
}
257+
],
258+
"source": [
259+
"order_stats_df_1867\\\n",
260+
" .filter(col(\"max_qty\") > global_avg_max_df_1867)\\\n",
261+
" .select(\"order_id\").show()"
262+
]
160263
}
161264
],
162265
"metadata": {
163266
"application/vnd.databricks.v1+notebook": {
164-
"computePreferences": null,
267+
"computePreferences": {
268+
"hardware": {
269+
"accelerator": null,
270+
"gpuPoolId": null,
271+
"memory": null
272+
}
273+
},
165274
"dashboards": [],
166275
"environmentMetadata": {
167276
"base_environment": "",
168-
"environment_version": "1"
277+
"environment_version": "2"
169278
},
170279
"inputWidgetPreferences": null,
171280
"language": "python",

0 commit comments

Comments
 (0)