Skip to content

Commit c8f6d93

Browse files
author
bitoollearner
committed
Leet Code Pyspark Questions Batch-3
Leet Code Pyspark Questions Batch-3
1 parent 62329cc commit c8f6d93

File tree

2 files changed

+124
-16
lines changed

2 files changed

+124
-16
lines changed

Solved/577. Employee Bonus (Easy)-(Solved).ipynb

Lines changed: 53 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": "db7ebec7-889d-4791-9b66-269554c0d581",
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": "70352d4b-6bb2-42f3-8465-ba88ab41d730",
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": "154313e3-f403-4508-a1d1-a93ae1e178d9",
4655
"showTitle": false,
@@ -111,7 +120,10 @@
111120
"execution_count": 0,
112121
"metadata": {
113122
"application/vnd.databricks.v1+cell": {
114-
"cellMetadata": {},
123+
"cellMetadata": {
124+
"byteLimit": 2048000,
125+
"rowLimit": 10000
126+
},
115127
"inputWidgets": {},
116128
"nuid": "787ef081-e08b-4a47-9b9a-a1a07b8b2b93",
117129
"showTitle": false,
@@ -141,25 +153,58 @@
141153
"df_bonus_577 = spark.createDataFrame(bonus_data_577, bonus_columns_577)\n",
142154
"df_bonus_577.show()"
143155
]
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": "513c7f49-e38e-44a4-8d52-83d8ad2af197",
168+
"showTitle": false,
169+
"tableResultSettingsMap": {},
170+
"title": ""
171+
}
172+
},
173+
"outputs": [],
174+
"source": [
175+
"df_employee_577\\\n",
176+
" .join(df_bonus_577, df_bonus_577.empId == df_employee_577.empId, 'left')\\\n",
177+
" .select(col(\"name\"), col(\"bonus\"))\\\n",
178+
" .filter((col(\"bonus\") < 1000) | col(\"bonus\").isNull()).show()"
179+
]
144180
}
145181
],
146182
"metadata": {
147183
"application/vnd.databricks.v1+notebook": {
148-
"computePreferences": null,
184+
"computePreferences": {
185+
"hardware": {
186+
"accelerator": null,
187+
"gpuPoolId": null,
188+
"memory": null
189+
}
190+
},
149191
"dashboards": [],
150192
"environmentMetadata": {
151193
"base_environment": "",
152-
"environment_version": "1"
194+
"environment_version": "2"
153195
},
154196
"inputWidgetPreferences": null,
155197
"language": "python",
156198
"notebookMetadata": {
157199
"pythonIndentUnit": 4
158200
},
159-
"notebookName": "577. Employee Bonus (Easy)",
201+
"notebookName": "577. Employee Bonus (Easy)-(Solved)",
160202
"widgets": {}
203+
},
204+
"language_info": {
205+
"name": "python"
161206
}
162207
},
163208
"nbformat": 4,
164209
"nbformat_minor": 0
165-
}
210+
}

Solved/578. Get Highest Answer Rate Question (Medium)-(Solved).ipynb

Lines changed: 71 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": "db7ebec7-889d-4791-9b66-269554c0d581",
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": "70352d4b-6bb2-42f3-8465-ba88ab41d730",
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": "154313e3-f403-4508-a1d1-a93ae1e178d9",
4655
"showTitle": false,
@@ -102,7 +111,10 @@
102111
"execution_count": 0,
103112
"metadata": {
104113
"application/vnd.databricks.v1+cell": {
105-
"cellMetadata": {},
114+
"cellMetadata": {
115+
"byteLimit": 2048000,
116+
"rowLimit": 10000
117+
},
106118
"inputWidgets": {},
107119
"nuid": "787ef081-e08b-4a47-9b9a-a1a07b8b2b93",
108120
"showTitle": false,
@@ -123,25 +135,76 @@
123135
"survey_log_df_578 = spark.createDataFrame(survey_log_data_578, survey_log_columns_578)\n",
124136
"survey_log_df_578.show()\n"
125137
]
138+
},
139+
{
140+
"cell_type": "code",
141+
"execution_count": 0,
142+
"metadata": {
143+
"application/vnd.databricks.v1+cell": {
144+
"cellMetadata": {
145+
"byteLimit": 2048000,
146+
"rowLimit": 10000
147+
},
148+
"inputWidgets": {},
149+
"nuid": "8448776c-e90d-429d-ba0d-00c46d06099f",
150+
"showTitle": false,
151+
"tableResultSettingsMap": {},
152+
"title": ""
153+
}
154+
},
155+
"outputs": [],
156+
"source": [
157+
"answer_counts_df_578 = survey_log_df_578\\\n",
158+
" .filter(col(\"action\") == 'answer')\\\n",
159+
" .groupBy(\"question_id\")\\\n",
160+
" .agg(count('action').alias(\"answer_count\"))\n",
161+
"\n",
162+
"show_counts_df_578 = survey_log_df_578\\\n",
163+
" .filter(col(\"action\") == \"show\")\\\n",
164+
" .groupBy(\"question_id\")\\\n",
165+
" .agg(count('action').alias(\"show_count\"))\n",
166+
"\n",
167+
"answer_rates_df_578 = show_counts_df_578\\\n",
168+
" .join(answer_counts_df_578, show_counts_df_578.question_id == answer_counts_df_578.question_id, 'left')\\\n",
169+
" .withColumn(\"answer_rate\", when(col(\"answer_count\").isNull(),0.0)\\\n",
170+
" .otherwise(col('answer_count')/col('show_count')))\\\n",
171+
" .select(show_counts_df_578[\"question_id\"], \"answer_rate\")\n",
172+
"\n",
173+
"window_spec = Window.orderBy(col(\"answer_rate\").desc(), col(\"question_id\").asc())\n",
174+
"ranked_df_578 = answer_rates_df_578.withColumn(\"rank\", rank().over(window_spec))\n",
175+
"\n",
176+
"ranked_df_578\\\n",
177+
" .filter(col(\"rank\") == 1)\\\n",
178+
" .select(\"question_id\").show()"
179+
]
126180
}
127181
],
128182
"metadata": {
129183
"application/vnd.databricks.v1+notebook": {
130-
"computePreferences": null,
184+
"computePreferences": {
185+
"hardware": {
186+
"accelerator": null,
187+
"gpuPoolId": null,
188+
"memory": null
189+
}
190+
},
131191
"dashboards": [],
132192
"environmentMetadata": {
133193
"base_environment": "",
134-
"environment_version": "1"
194+
"environment_version": "2"
135195
},
136196
"inputWidgetPreferences": null,
137197
"language": "python",
138198
"notebookMetadata": {
139199
"pythonIndentUnit": 4
140200
},
141-
"notebookName": "578. Get Highest Answer Rate Question (Medium)",
201+
"notebookName": "578. Get Highest Answer Rate Question (Medium)-(Solved)",
142202
"widgets": {}
203+
},
204+
"language_info": {
205+
"name": "python"
143206
}
144207
},
145208
"nbformat": 4,
146209
"nbformat_minor": 0
147-
}
210+
}

0 commit comments

Comments
 (0)