Skip to content
10 changes: 5 additions & 5 deletions learntools/sql/ex5.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ def check(self, results):
# check 2: length of dataframe
assert (len(results) == len(rides_per_year_answer)), ("The results don't look right. Try again.")
# check 3: one value in particular
year_to_check = list(rides_per_year_answer["year"])[-1]
correct_number = int(rides_per_year_answer.loc[rides_per_year_answer["year"]==year_to_check]["num_trips"].values)
submitted_number = int(results.loc[results["year"]==year_to_check]["num_trips"].values)
assert (correct_number == submitted_number), ("The results don't look right. Try again.")

year_to_check = rides_per_year_answer["year"].iloc[-1]
correct_number = rides_per_year_answer.loc[rides_per_year_answer["year"] == year_to_check, "num_trips"].iloc[0]
submitted_number = results.loc[results["year"] == year_to_check, "num_trips"].iloc[0]
assert correct_number == submitted_number, "The results don't look right. Try again."
_hint = "Start your query with `SELECT EXTRACT(YEAR FROM trip_start_timestamp) AS year, COUNT(1) AS num_trips`."
_solution = CS(
"""
Expand Down
6 changes: 3 additions & 3 deletions learntools/sql/ex6.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,9 @@ def check(self, query, results):
assert ('user_id' in results.columns), ('You do not have a `user_id` column in your results.')
assert ('number_of_answers' in results.columns), ('You do not have a `number_of_answers` column in your results.')
# check 3: correct user IDs
correct_ids = set([int(i) for i in bigquery_experts_answer.user_id.values if not np.isnan(i)])
submitted_ids = set([int(i) for i in results.user_id.values if not np.isnan(i)])
assert (correct_ids == submitted_ids), ('You seem to have the wrong values in the `user_id` column.')
correct_ids = bigquery_experts_answer.loc[bigquery_experts_answer.user_id.notna(), "user_id"].unique()
submitted_ids = results.loc[results.user_id.notna(), "user_id"].unique()
assert np.array_equal(correct_ids, submitted_ids), 'You seem to have the wrong values in the `user_id` column.'
# check 4: check one value from other column
first_id = list(bigquery_experts_answer["user_id"])[0]
correct_num = int(bigquery_experts_answer[bigquery_experts_answer["user_id"] == first_id]["number_of_answers"])
Expand Down
10 changes: 5 additions & 5 deletions learntools/sql_advanced/ex1.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ def check(self, query):
"%d rows, but you have %d rows." % (len(correct_answer), len(results)))
# check 2: calculated values
# correct result
correct_list = [i for i in list(correct_answer["time_to_answer"]) if not math.isnan(i)]
correct_number = int(sum(correct_list)/len(correct_list))
correct_list = correct_answer.loc[correct_answer["time_to_answer"].notna(), "time_to_answer"]
correct_number = correct_list.sum()/len(correct_list)
# submitted value
submitted_list = [i for i in list(results["time_to_answer"]) if not math.isnan(i)]
submitted_number = int(sum(submitted_list)/len(submitted_list))
assert (int(submitted_number)==int(correct_number)), ("The results don't look right. Please make sure that the part of the query "
submitted_list = results.loc[results["time_to_answer"].notna(), "time_to_answer"]
submitted_number = submitted_list.sum()/len(submitted_list)
assert int(submitted_number) == int(correct_number), ("The results don't look right. Please make sure that the part of the query "
"that calculates the values in the `time_to_answer` column is unmodified.")

_solution = CS(\
Expand Down
10 changes: 5 additions & 5 deletions learntools/sql_advanced/ex2.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,12 @@ def check(self, query):
# check 3: check values, length of dataframe
assert (len(results)==len(break_time_answer)), ("Your answer does not have the correct number of rows.")
# check 4: specific number
id_to_check = list(break_time_answer["taxi_id"])[0]
correct_ans = [int(i) for i in list(break_time_answer.loc[break_time_answer["taxi_id"] == id_to_check]["prev_break"]) if math.isnan(i)==False]
submitted_ans = [int(i) for i in list(results.loc[results["taxi_id"] == id_to_check]["prev_break"]) if math.isnan(i)==False]
id_to_check = break_time_answer["taxi_id"].iloc[0]
correct_ans = break_time_answer.loc[break_time_answer["taxi_id"].eq(id_to_check) & break_time_answer["prev_break"].notna(), "prev_break"]
submitted_ans = results.loc[results["taxi_id"].eq(id_to_check) & results["prev_break"].notna(), "prev_break"]
if len(correct_ans) > 0:
assert (min(correct_ans)==min(submitted_ans)), ("The results don't look right. Try again.")
assert (max(correct_ans)==max(submitted_ans)), ("The results don't look right. Try again.")
assert correct_ans.min() == submitted_ans.min(), "The results don't look right. Try again."
assert correct_ans.max() == submitted_ans.max(), "The results don't look right. Try again."

_solution = CS( \
"""
Expand Down