Skip to content

Commit 375fcf8

Browse files
author
bitoollearner
committed
LeetCode Pyspark Questions Solution
LeetCode Pyspark Questions Solution
1 parent 58c9673 commit 375fcf8

11 files changed

+774
-83
lines changed

Solved/1045. Customers Who Bought All Products (Medium)-(Solved).ipynb

Lines changed: 86 additions & 9 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": "4cd0cff4-1b3d-443e-8f60-5eaa3c44b351",
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": "97710283-7ef7-4994-bdf7-ab35e4e4acd8",
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": "d438d974-0dc5-432f-af61-ceb185a0700c",
4655
"showTitle": false,
@@ -110,7 +119,10 @@
110119
"execution_count": 0,
111120
"metadata": {
112121
"application/vnd.databricks.v1+cell": {
113-
"cellMetadata": {},
122+
"cellMetadata": {
123+
"byteLimit": 2048000,
124+
"rowLimit": 10000
125+
},
114126
"inputWidgets": {},
115127
"nuid": "c899699d-2ed8-4227-ba6f-ca46f3f95a7f",
116128
"showTitle": false,
@@ -130,27 +142,92 @@
130142
"\n",
131143
"customer_columns_1045 = [\"customer_id\", \"product_key\"]\n",
132144
"customer_df_1045 = spark.createDataFrame(customer_data_1045, customer_columns_1045)\n",
133-
"customer_df_1045.show()"
145+
"customer_df_1045.show()\n",
146+
"\n",
147+
"\n",
148+
"product_data_1045 = [\n",
149+
" (5,),\n",
150+
" (6,)\n",
151+
"]\n",
152+
"product_columns_1045 = [\"product_key\"]\n",
153+
"product_df_1045 = spark.createDataFrame(product_data_1045, product_columns_1045)\n",
154+
"product_df_1045.show()\n"
155+
]
156+
},
157+
{
158+
"cell_type": "code",
159+
"execution_count": 0,
160+
"metadata": {
161+
"application/vnd.databricks.v1+cell": {
162+
"cellMetadata": {
163+
"byteLimit": 2048000,
164+
"rowLimit": 10000
165+
},
166+
"inputWidgets": {},
167+
"nuid": "959c580f-e9fe-4952-b46c-43e04e49b28e",
168+
"showTitle": false,
169+
"tableResultSettingsMap": {},
170+
"title": ""
171+
}
172+
},
173+
"outputs": [],
174+
"source": [
175+
"total_products_1045 = product_df_1045.select(countDistinct(\"product_key\")).collect()[0][0]\n"
176+
]
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": "e115017a-7d4f-4798-968b-73babdfb4b1d",
189+
"showTitle": false,
190+
"tableResultSettingsMap": {},
191+
"title": ""
192+
}
193+
},
194+
"outputs": [],
195+
"source": [
196+
"customer_df_1045\\\n",
197+
" .groupBy(\"customer_id\").agg(\n",
198+
" countDistinct(\"product_key\").alias(\"num_products_bought\"))\\\n",
199+
" .filter(\n",
200+
" col(\"num_products_bought\") == total_products_1045\n",
201+
" ).select(\"customer_id\").show()\n"
134202
]
135203
}
136204
],
137205
"metadata": {
138206
"application/vnd.databricks.v1+notebook": {
139-
"computePreferences": null,
207+
"computePreferences": {
208+
"hardware": {
209+
"accelerator": null,
210+
"gpuPoolId": null,
211+
"memory": null
212+
}
213+
},
140214
"dashboards": [],
141215
"environmentMetadata": {
142216
"base_environment": "",
143-
"environment_version": "1"
217+
"environment_version": "2"
144218
},
145219
"inputWidgetPreferences": null,
146220
"language": "python",
147221
"notebookMetadata": {
148222
"pythonIndentUnit": 4
149223
},
150-
"notebookName": "1045. Customers Who Bought All Products (Medium)",
224+
"notebookName": "1045. Customers Who Bought All Products (Medium)-(Solved)",
151225
"widgets": {}
226+
},
227+
"language_info": {
228+
"name": "python"
152229
}
153230
},
154231
"nbformat": 4,
155232
"nbformat_minor": 0
156-
}
233+
}

Solved/1050. Actors and Directors Who Cooperated At Least Three Times (Easy)-(Solved).ipynb

Lines changed: 54 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": "4cd0cff4-1b3d-443e-8f60-5eaa3c44b351",
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": "97710283-7ef7-4994-bdf7-ab35e4e4acd8",
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": "d438d974-0dc5-432f-af61-ceb185a0700c",
4655
"showTitle": false,
@@ -95,7 +104,10 @@
95104
"execution_count": 0,
96105
"metadata": {
97106
"application/vnd.databricks.v1+cell": {
98-
"cellMetadata": {},
107+
"cellMetadata": {
108+
"byteLimit": 2048000,
109+
"rowLimit": 10000
110+
},
99111
"inputWidgets": {},
100112
"nuid": "c899699d-2ed8-4227-ba6f-ca46f3f95a7f",
101113
"showTitle": false,
@@ -119,25 +131,59 @@
119131
"actor_director_df_1050 = spark.createDataFrame(actor_director_data_1050, actor_director_columns_1050)\n",
120132
"actor_director_df_1050.show()"
121133
]
134+
},
135+
{
136+
"cell_type": "code",
137+
"execution_count": 0,
138+
"metadata": {
139+
"application/vnd.databricks.v1+cell": {
140+
"cellMetadata": {
141+
"byteLimit": 2048000,
142+
"rowLimit": 10000
143+
},
144+
"inputWidgets": {},
145+
"nuid": "034b4770-8582-4619-ae18-b0aaeb1d5115",
146+
"showTitle": false,
147+
"tableResultSettingsMap": {},
148+
"title": ""
149+
}
150+
},
151+
"outputs": [],
152+
"source": [
153+
"actor_director_df_1050\\\n",
154+
" .groupBy(\"actor_id\", \"director_id\")\\\n",
155+
" .agg(count(\"*\").alias(\"cooperations\"))\\\n",
156+
" .filter(col(\"cooperations\") >= 3)\\\n",
157+
" .select(\"actor_id\", \"director_id\").show()"
158+
]
122159
}
123160
],
124161
"metadata": {
125162
"application/vnd.databricks.v1+notebook": {
126-
"computePreferences": null,
163+
"computePreferences": {
164+
"hardware": {
165+
"accelerator": null,
166+
"gpuPoolId": null,
167+
"memory": null
168+
}
169+
},
127170
"dashboards": [],
128171
"environmentMetadata": {
129172
"base_environment": "",
130-
"environment_version": "1"
173+
"environment_version": "2"
131174
},
132175
"inputWidgetPreferences": null,
133176
"language": "python",
134177
"notebookMetadata": {
135178
"pythonIndentUnit": 4
136179
},
137-
"notebookName": "1050. Actors and Directors Who Cooperated At Least Three Times (Easy)",
180+
"notebookName": "1050. Actors and Directors Who Cooperated At Least Three Times (Easy)-(Solved)",
138181
"widgets": {}
182+
},
183+
"language_info": {
184+
"name": "python"
139185
}
140186
},
141187
"nbformat": 4,
142188
"nbformat_minor": 0
143-
}
189+
}

Solved/1068. Product Sales Analysis I (Easy)-(Solved).ipynb

Lines changed: 52 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": "4cd0cff4-1b3d-443e-8f60-5eaa3c44b351",
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": "97710283-7ef7-4994-bdf7-ab35e4e4acd8",
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": "d438d974-0dc5-432f-af61-ceb185a0700c",
4655
"showTitle": false,
@@ -119,7 +128,10 @@
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": "c899699d-2ed8-4227-ba6f-ca46f3f95a7f",
125137
"showTitle": false,
@@ -149,25 +161,57 @@
149161
"product_df_1068 = spark.createDataFrame(product_data_1068, product_columns_1068)\n",
150162
"product_df_1068.show()"
151163
]
164+
},
165+
{
166+
"cell_type": "code",
167+
"execution_count": 0,
168+
"metadata": {
169+
"application/vnd.databricks.v1+cell": {
170+
"cellMetadata": {
171+
"byteLimit": 2048000,
172+
"rowLimit": 10000
173+
},
174+
"inputWidgets": {},
175+
"nuid": "51804e80-7ca3-457d-826c-cf47be503b6f",
176+
"showTitle": false,
177+
"tableResultSettingsMap": {},
178+
"title": ""
179+
}
180+
},
181+
"outputs": [],
182+
"source": [
183+
"sales_df_1068\\\n",
184+
" .join(product_df_1068, on=\"product_id\", how=\"inner\")\\\n",
185+
" .select(\"product_name\", \"year\", \"price\").show()\n"
186+
]
152187
}
153188
],
154189
"metadata": {
155190
"application/vnd.databricks.v1+notebook": {
156-
"computePreferences": null,
191+
"computePreferences": {
192+
"hardware": {
193+
"accelerator": null,
194+
"gpuPoolId": null,
195+
"memory": null
196+
}
197+
},
157198
"dashboards": [],
158199
"environmentMetadata": {
159200
"base_environment": "",
160-
"environment_version": "1"
201+
"environment_version": "2"
161202
},
162203
"inputWidgetPreferences": null,
163204
"language": "python",
164205
"notebookMetadata": {
165206
"pythonIndentUnit": 4
166207
},
167-
"notebookName": "1068. Product Sales Analysis I (Easy)",
208+
"notebookName": "1068. Product Sales Analysis I (Easy)-(Solved)",
168209
"widgets": {}
210+
},
211+
"language_info": {
212+
"name": "python"
169213
}
170214
},
171215
"nbformat": 4,
172216
"nbformat_minor": 0
173-
}
217+
}

0 commit comments

Comments
 (0)