Skip to content

Commit 6305ab5

Browse files
author
bitoollearner
committed
PySpark LeetCode Question
1 parent 9c15dde commit 6305ab5

File tree

2 files changed

+162
-14
lines changed

2 files changed

+162
-14
lines changed

Solved/1715. Count Apples and Oranges (Medium)-(Solved).ipynb

Lines changed: 95 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|box_id|chest_id|apple_count|orange_count|\n+------+--------+-----------+------------+\n| 2| NULL| 6| 15|\n| 18| 14| 4| 15|\n| 19| 3| 8| 4|\n| 12| 2| 19| 20|\n| 20| 6| 12| 9|\n| 8| 6| 9| 9|\n| 3| 14| 16| 7|\n+------+--------+-----------+------------+\n\n+--------+-----------+------------+\n|chest_id|apple_count|orange_count|\n+--------+-----------+------------+\n| 6| 5| 6|\n| 14| 20| 10|\n| 2| 8| 8|\n| 3| 19| 4|\n| 16| 19| 19|\n+--------+-----------+------------+\n\n"
156+
]
157+
}
158+
],
138159
"source": [
139160
"boxes_data_1715 = [\n",
140161
" (2, None, 6, 15),\n",
@@ -163,15 +184,82 @@
163184
"chests_df_1715.show()\n",
164185
"\n"
165186
]
187+
},
188+
{
189+
"cell_type": "code",
190+
"execution_count": 0,
191+
"metadata": {
192+
"application/vnd.databricks.v1+cell": {
193+
"cellMetadata": {
194+
"byteLimit": 2048000,
195+
"rowLimit": 10000
196+
},
197+
"inputWidgets": {},
198+
"nuid": "4771ec37-b5de-46bb-8eff-59493121cb22",
199+
"showTitle": false,
200+
"tableResultSettingsMap": {},
201+
"title": ""
202+
}
203+
},
204+
"outputs": [],
205+
"source": [
206+
"boxes = boxes_df_1715.alias(\"b\")\n",
207+
"chests = chests_df_1715.alias(\"c\")"
208+
]
209+
},
210+
{
211+
"cell_type": "code",
212+
"execution_count": 0,
213+
"metadata": {
214+
"application/vnd.databricks.v1+cell": {
215+
"cellMetadata": {
216+
"byteLimit": 2048000,
217+
"rowLimit": 10000
218+
},
219+
"inputWidgets": {},
220+
"nuid": "343a17f7-9b2a-431d-a80a-f77682728cd0",
221+
"showTitle": false,
222+
"tableResultSettingsMap": {},
223+
"title": ""
224+
}
225+
},
226+
"outputs": [
227+
{
228+
"output_type": "stream",
229+
"name": "stdout",
230+
"output_type": "stream",
231+
"text": [
232+
"+-----------+------------+\n|apple_count|orange_count|\n+-----------+------------+\n| 151| 123|\n+-----------+------------+\n\n"
233+
]
234+
}
235+
],
236+
"source": [
237+
"boxes\\\n",
238+
" .join(chests, boxes.chest_id == chests.chest_id, \"left\")\\\n",
239+
" .select(\n",
240+
" (col(\"b.apple_count\") + coalesce(col(\"c.apple_count\"), lit(0))).alias(\"total_apples\"),\n",
241+
" (col(\"b.orange_count\") + coalesce(col(\"c.orange_count\"), lit(0))).alias(\"total_oranges\")\n",
242+
" )\\\n",
243+
" .agg(\n",
244+
" sum(\"total_apples\").alias(\"apple_count\"),\n",
245+
" sum(\"total_oranges\").alias(\"orange_count\")\n",
246+
" ).show()"
247+
]
166248
}
167249
],
168250
"metadata": {
169251
"application/vnd.databricks.v1+notebook": {
170-
"computePreferences": null,
252+
"computePreferences": {
253+
"hardware": {
254+
"accelerator": null,
255+
"gpuPoolId": null,
256+
"memory": null
257+
}
258+
},
171259
"dashboards": [],
172260
"environmentMetadata": {
173261
"base_environment": "",
174-
"environment_version": "1"
262+
"environment_version": "2"
175263
},
176264
"inputWidgetPreferences": null,
177265
"language": "python",

Solved/1729. Find Followers Count (Easy)-(Solved).ipynb

Lines changed: 67 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,
@@ -96,15 +105,27 @@
96105
"execution_count": 0,
97106
"metadata": {
98107
"application/vnd.databricks.v1+cell": {
99-
"cellMetadata": {},
108+
"cellMetadata": {
109+
"byteLimit": 2048000,
110+
"rowLimit": 10000
111+
},
100112
"inputWidgets": {},
101113
"nuid": "7b78bc27-26cd-4523-8646-7369baf68fcb",
102114
"showTitle": false,
103115
"tableResultSettingsMap": {},
104116
"title": ""
105117
}
106118
},
107-
"outputs": [],
119+
"outputs": [
120+
{
121+
"output_type": "stream",
122+
"name": "stdout",
123+
"output_type": "stream",
124+
"text": [
125+
"+-------+-----------+\n|user_id|follower_id|\n+-------+-----------+\n| 0| 1|\n| 1| 0|\n| 2| 0|\n| 2| 1|\n+-------+-----------+\n\n"
126+
]
127+
}
128+
],
108129
"source": [
109130
"followers_data_1729 = [\n",
110131
" (0, 1),\n",
@@ -116,15 +137,54 @@
116137
"followers_df_1729 = spark.createDataFrame(followers_data_1729, followers_columns_1729)\n",
117138
"followers_df_1729.show()"
118139
]
140+
},
141+
{
142+
"cell_type": "code",
143+
"execution_count": 0,
144+
"metadata": {
145+
"application/vnd.databricks.v1+cell": {
146+
"cellMetadata": {
147+
"byteLimit": 2048000,
148+
"rowLimit": 10000
149+
},
150+
"inputWidgets": {},
151+
"nuid": "7e4c3842-0033-447f-8644-93c62a25c090",
152+
"showTitle": false,
153+
"tableResultSettingsMap": {},
154+
"title": ""
155+
}
156+
},
157+
"outputs": [
158+
{
159+
"output_type": "stream",
160+
"name": "stdout",
161+
"output_type": "stream",
162+
"text": [
163+
"+-------+---------------+\n|user_id|followers_count|\n+-------+---------------+\n| 0| 1|\n| 1| 1|\n| 2| 2|\n+-------+---------------+\n\n"
164+
]
165+
}
166+
],
167+
"source": [
168+
"followers_df_1729\\\n",
169+
" .groupBy(\"user_id\") \\\n",
170+
" .agg(count(\"follower_id\").alias(\"followers_count\")) \\\n",
171+
" .orderBy(\"user_id\").show()"
172+
]
119173
}
120174
],
121175
"metadata": {
122176
"application/vnd.databricks.v1+notebook": {
123-
"computePreferences": null,
177+
"computePreferences": {
178+
"hardware": {
179+
"accelerator": null,
180+
"gpuPoolId": null,
181+
"memory": null
182+
}
183+
},
124184
"dashboards": [],
125185
"environmentMetadata": {
126186
"base_environment": "",
127-
"environment_version": "1"
187+
"environment_version": "2"
128188
},
129189
"inputWidgetPreferences": null,
130190
"language": "python",

0 commit comments

Comments
 (0)