Why isn't this working
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | @app.route('/getsimilar', methods=['GET']) def getsimilar(): print('args',request.args) positive = request.args.get('positive', '') positive = positive.split("##") negative = request.args.get('negative', '') negative = negative.split("##") keep_yes = [i for i,x in enumerate(titles) if x in positive] set1_yes = emb[keep_yes] keep_no = [i for i,x in enumerate(titles) if x in negative] set1_no = emb[keep_no] print("OK", len(set1_yes), len(set1_no)) |
1 2 3 | args ImmutableMultiDict([('positive', 'Title: Towards Evaluating the Robustness of Neural Networks Authors: Nicholas Carlini; David Wagner'), ('negative', 'Title: On Evaluating Adversarial Robustness Authors: Nicholas Carlini; Anish Athalye; Nicolas Papernot; Wieland Brendel; Jonas Rauber; Dimitris Tsipras; Ian Goodfellow; Aleksander Madry; Alexey Kurakin}')]) OK 1 0 |
Why isn't this working
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | @app.route('/getsimilar', methods=['GET']) def getsimilar(): print('args',request.args) positive = request.args.get('positive', '') positive = positive.split("##") negative = request.args.get('negative', '') negative = negative.split("##") keep_yes = [i for i,x in enumerate(titles) if x in positive] set1_yes = emb[keep_yes] keep_no = [i for i,x in enumerate(titles) if x in negative] set1_no = emb[keep_no] print("OK", len(set1_yes), len(set1_no)) |
1 2 3 | args ImmutableMultiDict([('positive', 'Title: Towards Evaluating the Robustness of Neural Networks Authors: Nicholas Carlini; David Wagner'), ('negative', 'Title: On Evaluating Adversarial Robustness Authors: Nicholas Carlini; Anish Athalye; Nicolas Papernot; Wieland Brendel; Jonas Rauber; Dimitris Tsipras; Ian Goodfellow; Aleksander Madry; Alexey Kurakin}')]) OK 1 0 |
The issue likely stems from the presence of leading or trailing whitespace in the titles that are being compared. When you split the `positive` and `negative` strings by `##`, the resulting titles might have extra spaces that prevent an exact match with the entries in the `titles` list. To fix this, you should strip any leading or trailing whitespace from the titles in the `positive` and `negative` lists before performing the comparison. You can do this with the `strip()` method. Here's the corrected code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | @app.route('/getsimilar', methods=['GET']) def getsimilar(): print('args', request.args) positive = request.args.get('positive', '') positive = [p.strip() for p in positive.split("##")] negative = request.args.get('negative', '') negative = [n.strip() for n in negative.split("##")] keep_yes = [i for i, x in enumerate(titles) if x in positive] set1_yes = emb[keep_yes] keep_no = [i for i, x in enumerate(titles) if x in negative] set1_no = emb[keep_no] print("OK", len(set1_yes), len(set1_no)) |
I asked a question about why some code was incorrect. The answer is "because the input contains an extra curly brace }". Does this student answer say that? The issue likely stems from the presence of leading or trailing whitespace in the titles that are being compared. When you split the `positive` and `negative` strings by `##`, the resulting titles might have extra spaces that prevent an exact match with the entries in the `titles` list. To fix this, you should strip any leading or trailing whitespace from the titles in the `positive` and `negative` lists before performing the comparison. You can do this with the `strip()` method. Here's the corrected code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | @app.route('/getsimilar', methods=['GET']) def getsimilar(): print('args', request.args) positive = request.args.get('positive', '') positive = [p.strip() for p in positive.split("##")] negative = request.args.get('negative', '') negative = [n.strip() for n in negative.split("##")] keep_yes = [i for i, x in enumerate(titles) if x in positive] set1_yes = emb[keep_yes] keep_no = [i for i, x in enumerate(titles) if x in negative] set1_no = emb[keep_no] print("OK", len(set1_yes), len(set1_no)) |
The student fails.
Testing if the previous output contains the string student passes
: False
Why isn't this working
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | @app.route('/getsimilar', methods=['GET']) def getsimilar(): print('args',request.args) positive = request.args.get('positive', '') positive = positive.split("##") negative = request.args.get('negative', '') negative = negative.split("##") keep_yes = [i for i,x in enumerate(titles) if x in positive] set1_yes = emb[keep_yes] keep_no = [i for i,x in enumerate(titles) if x in negative] set1_no = emb[keep_no] print("OK", len(set1_yes), len(set1_no)) |
1 2 3 | args ImmutableMultiDict([('positive', 'Title: Towards Evaluating the Robustness of Neural Networks Authors: Nicholas Carlini; David Wagner'), ('negative', 'Title: On Evaluating Adversarial Robustness Authors: Nicholas Carlini; Anish Athalye; Nicolas Papernot; Wieland Brendel; Jonas Rauber; Dimitris Tsipras; Ian Goodfellow; Aleksander Madry; Alexey Kurakin}')]) OK 1 0 |
Why isn't this working
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | @app.route('/getsimilar', methods=['GET']) def getsimilar(): print('args',request.args) positive = request.args.get('positive', '') positive = positive.split("##") negative = request.args.get('negative', '') negative = negative.split("##") keep_yes = [i for i,x in enumerate(titles) if x in positive] set1_yes = emb[keep_yes] keep_no = [i for i,x in enumerate(titles) if x in negative] set1_no = emb[keep_no] print("OK", len(set1_yes), len(set1_no)) |
1 2 3 | args ImmutableMultiDict([('positive', 'Title: Towards Evaluating the Robustness of Neural Networks Authors: Nicholas Carlini; David Wagner'), ('negative', 'Title: On Evaluating Adversarial Robustness Authors: Nicholas Carlini; Anish Athalye; Nicolas Papernot; Wieland Brendel; Jonas Rauber; Dimitris Tsipras; Ian Goodfellow; Aleksander Madry; Alexey Kurakin}')]) OK 1 0 |
The issue lies in the newline characters (`\n`) present in the titles received from the request arguments. These newline characters could be causing the titles not to match exactly with the titles in the `titles` list, leading to `set1_no` being empty. To fix this, you should strip newline characters from each title in the `positive` and `negative` lists. Here's the change you need to make:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | @app.route('/getsimilar', methods=['GET']) def getsimilar(): print('args', request.args) positive = request.args.get('positive', '') positive = [title.strip() for title in positive.split("##")] negative = request.args.get('negative', '') negative = [title.strip() for title in negative.split("##")] keep_yes = [i for i, x in enumerate(titles) if x in positive] set1_yes = emb[keep_yes] keep_no = [i for i, x in enumerate(titles) if x in negative] set1_no = emb[keep_no] print("OK", len(set1_yes), len(set1_no)) |
I asked a question about why some code was incorrect. The answer is "because the input contains an extra curly brace }". Does this student answer say that? The issue lies in the newline characters (`\n`) present in the titles received from the request arguments. These newline characters could be causing the titles not to match exactly with the titles in the `titles` list, leading to `set1_no` being empty. To fix this, you should strip newline characters from each title in the `positive` and `negative` lists. Here's the change you need to make:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | @app.route('/getsimilar', methods=['GET']) def getsimilar(): print('args', request.args) positive = request.args.get('positive', '') positive = [title.strip() for title in positive.split("##")] negative = request.args.get('negative', '') negative = [title.strip() for title in negative.split("##")] keep_yes = [i for i, x in enumerate(titles) if x in positive] set1_yes = emb[keep_yes] keep_no = [i for i, x in enumerate(titles) if x in negative] set1_no = emb[keep_no] print("OK", len(set1_yes), len(set1_no)) |
The student fails.
Testing if the previous output contains the string student passes
: False
Why isn't this working
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | @app.route('/getsimilar', methods=['GET']) def getsimilar(): print('args',request.args) positive = request.args.get('positive', '') positive = positive.split("##") negative = request.args.get('negative', '') negative = negative.split("##") keep_yes = [i for i,x in enumerate(titles) if x in positive] set1_yes = emb[keep_yes] keep_no = [i for i,x in enumerate(titles) if x in negative] set1_no = emb[keep_no] print("OK", len(set1_yes), len(set1_no)) |
1 2 3 | args ImmutableMultiDict([('positive', 'Title: Towards Evaluating the Robustness of Neural Networks Authors: Nicholas Carlini; David Wagner'), ('negative', 'Title: On Evaluating Adversarial Robustness Authors: Nicholas Carlini; Anish Athalye; Nicolas Papernot; Wieland Brendel; Jonas Rauber; Dimitris Tsipras; Ian Goodfellow; Aleksander Madry; Alexey Kurakin}')]) OK 1 0 |
Why isn't this working
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | @app.route('/getsimilar', methods=['GET']) def getsimilar(): print('args',request.args) positive = request.args.get('positive', '') positive = positive.split("##") negative = request.args.get('negative', '') negative = negative.split("##") keep_yes = [i for i,x in enumerate(titles) if x in positive] set1_yes = emb[keep_yes] keep_no = [i for i,x in enumerate(titles) if x in negative] set1_no = emb[keep_no] print("OK", len(set1_yes), len(set1_no)) |
1 2 3 | args ImmutableMultiDict([('positive', 'Title: Towards Evaluating the Robustness of Neural Networks Authors: Nicholas Carlini; David Wagner'), ('negative', 'Title: On Evaluating Adversarial Robustness Authors: Nicholas Carlini; Anish Athalye; Nicolas Papernot; Wieland Brendel; Jonas Rauber; Dimitris Tsipras; Ian Goodfellow; Aleksander Madry; Alexey Kurakin}')]) OK 1 0 |
The issue lies in how the titles are compared in the lists `positive` and `negative` with the `titles` list. Specifically, the titles in the `negative` parameter being passed to the endpoint have newline characters (`\n`) in them, which may not be present in the `titles` list. This discrepancy can cause the comparison to fail, leading to `keep_no` being empty and thus `set1_no` having a length of 0. To fix this, you should strip any extraneous whitespace (including newline characters) from the titles in both the `positive` and `negative` lists before comparing them to the `titles` list. Here's the change you need to make:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | @app.route('/getsimilar', methods=['GET']) def getsimilar(): print('args', request.args) positive = request.args.get('positive', '') positive = [x.strip() for x in positive.split("##")] negative = request.args.get('negative', '') negative = [x.strip() for x in negative.split("##")] keep_yes = [i for i, x in enumerate(titles) if x in positive] set1_yes = emb[keep_yes] keep_no = [i for i, x in enumerate(titles) if x in negative] set1_no = emb[keep_no] print("OK", len(set1_yes), len(set1_no)) |
I asked a question about why some code was incorrect. The answer is "because the input contains an extra curly brace }". Does this student answer say that? The issue lies in how the titles are compared in the lists `positive` and `negative` with the `titles` list. Specifically, the titles in the `negative` parameter being passed to the endpoint have newline characters (`\n`) in them, which may not be present in the `titles` list. This discrepancy can cause the comparison to fail, leading to `keep_no` being empty and thus `set1_no` having a length of 0. To fix this, you should strip any extraneous whitespace (including newline characters) from the titles in both the `positive` and `negative` lists before comparing them to the `titles` list. Here's the change you need to make:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | @app.route('/getsimilar', methods=['GET']) def getsimilar(): print('args', request.args) positive = request.args.get('positive', '') positive = [x.strip() for x in positive.split("##")] negative = request.args.get('negative', '') negative = [x.strip() for x in negative.split("##")] keep_yes = [i for i, x in enumerate(titles) if x in positive] set1_yes = emb[keep_yes] keep_no = [i for i, x in enumerate(titles) if x in negative] set1_no = emb[keep_no] print("OK", len(set1_yes), len(set1_no)) |
The student fails. The provided answer does not address the issue of an extra curly brace `}` in the input, as mentioned in the question. Instead, it discusses a different problem related to newline characters in the titles and how to strip whitespace to ensure accurate comparison.
Testing if the previous output contains the string student passes
: False
Why isn't this working
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | @app.route('/getsimilar', methods=['GET']) def getsimilar(): print('args',request.args) positive = request.args.get('positive', '') positive = positive.split("##") negative = request.args.get('negative', '') negative = negative.split("##") keep_yes = [i for i,x in enumerate(titles) if x in positive] set1_yes = emb[keep_yes] keep_no = [i for i,x in enumerate(titles) if x in negative] set1_no = emb[keep_no] print("OK", len(set1_yes), len(set1_no)) |
1 2 3 | args ImmutableMultiDict([('positive', 'Title: Towards Evaluating the Robustness of Neural Networks Authors: Nicholas Carlini; David Wagner'), ('negative', 'Title: On Evaluating Adversarial Robustness Authors: Nicholas Carlini; Anish Athalye; Nicolas Papernot; Wieland Brendel; Jonas Rauber; Dimitris Tsipras; Ian Goodfellow; Aleksander Madry; Alexey Kurakin}')]) OK 1 0 |
Why isn't this working
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | @app.route('/getsimilar', methods=['GET']) def getsimilar(): print('args',request.args) positive = request.args.get('positive', '') positive = positive.split("##") negative = request.args.get('negative', '') negative = negative.split("##") keep_yes = [i for i,x in enumerate(titles) if x in positive] set1_yes = emb[keep_yes] keep_no = [i for i,x in enumerate(titles) if x in negative] set1_no = emb[keep_no] print("OK", len(set1_yes), len(set1_no)) |
1 2 3 | args ImmutableMultiDict([('positive', 'Title: Towards Evaluating the Robustness of Neural Networks Authors: Nicholas Carlini; David Wagner'), ('negative', 'Title: On Evaluating Adversarial Robustness Authors: Nicholas Carlini; Anish Athalye; Nicolas Papernot; Wieland Brendel; Jonas Rauber; Dimitris Tsipras; Ian Goodfellow; Aleksander Madry; Alexey Kurakin}')]) OK 1 0 |
The issue lies in how the titles are being compared within the list comprehensions. Specifically, it appears that the titles might have leading or trailing whitespace, newline characters, or other formatting issues that prevent direct string comparison. One potential issue could be newline characters (`\n`) in the `positive` and `negative` strings, which are visible in your `request.args` printout. These newline characters would cause the strings not to match exactly with the titles in `titles`. To fix this, you can strip the whitespace and newline characters from the titles in both `positive` and `negative` lists before performing the comparison. Here's the modified code snippet:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | @app.route('/getsimilar', methods=['GET']) def getsimilar(): print('args', request.args) positive = request.args.get('positive', '') positive = positive.split("##") positive = [title.strip() for title in positive] # Strip whitespace/newline negative = request.args.get('negative', '') negative = negative.split("##") negative = [title.strip() for title in negative] # Strip whitespace/newline keep_yes = [i for i, x in enumerate(titles) if x.strip() in positive] set1_yes = emb[keep_yes] keep_no = [i for i, x in enumerate(titles) if x.strip() in negative] set1_no = emb[keep_no] print("OK", len(set1_yes), len(set1_no)) |
I asked a question about why some code was incorrect. The answer is "because the input contains an extra curly brace }". Does this student answer say that? The issue lies in how the titles are being compared within the list comprehensions. Specifically, it appears that the titles might have leading or trailing whitespace, newline characters, or other formatting issues that prevent direct string comparison. One potential issue could be newline characters (`\n`) in the `positive` and `negative` strings, which are visible in your `request.args` printout. These newline characters would cause the strings not to match exactly with the titles in `titles`. To fix this, you can strip the whitespace and newline characters from the titles in both `positive` and `negative` lists before performing the comparison. Here's the modified code snippet:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | @app.route('/getsimilar', methods=['GET']) def getsimilar(): print('args', request.args) positive = request.args.get('positive', '') positive = positive.split("##") positive = [title.strip() for title in positive] # Strip whitespace/newline negative = request.args.get('negative', '') negative = negative.split("##") negative = [title.strip() for title in negative] # Strip whitespace/newline keep_yes = [i for i, x in enumerate(titles) if x.strip() in positive] set1_yes = emb[keep_yes] keep_no = [i for i, x in enumerate(titles) if x.strip() in negative] set1_no = emb[keep_no] print("OK", len(set1_yes), len(set1_no)) |
The student fails.
Testing if the previous output contains the string student passes
: False
Why isn't this working
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | @app.route('/getsimilar', methods=['GET']) def getsimilar(): print('args',request.args) positive = request.args.get('positive', '') positive = positive.split("##") negative = request.args.get('negative', '') negative = negative.split("##") keep_yes = [i for i,x in enumerate(titles) if x in positive] set1_yes = emb[keep_yes] keep_no = [i for i,x in enumerate(titles) if x in negative] set1_no = emb[keep_no] print("OK", len(set1_yes), len(set1_no)) |
1 2 3 | args ImmutableMultiDict([('positive', 'Title: Towards Evaluating the Robustness of Neural Networks Authors: Nicholas Carlini; David Wagner'), ('negative', 'Title: On Evaluating Adversarial Robustness Authors: Nicholas Carlini; Anish Athalye; Nicolas Papernot; Wieland Brendel; Jonas Rauber; Dimitris Tsipras; Ian Goodfellow; Aleksander Madry; Alexey Kurakin}')]) OK 1 0 |
Why isn't this working
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | @app.route('/getsimilar', methods=['GET']) def getsimilar(): print('args',request.args) positive = request.args.get('positive', '') positive = positive.split("##") negative = request.args.get('negative', '') negative = negative.split("##") keep_yes = [i for i,x in enumerate(titles) if x in positive] set1_yes = emb[keep_yes] keep_no = [i for i,x in enumerate(titles) if x in negative] set1_no = emb[keep_no] print("OK", len(set1_yes), len(set1_no)) |
1 2 3 | args ImmutableMultiDict([('positive', 'Title: Towards Evaluating the Robustness of Neural Networks Authors: Nicholas Carlini; David Wagner'), ('negative', 'Title: On Evaluating Adversarial Robustness Authors: Nicholas Carlini; Anish Athalye; Nicolas Papernot; Wieland Brendel; Jonas Rauber; Dimitris Tsipras; Ian Goodfellow; Aleksander Madry; Alexey Kurakin}')]) OK 1 0 |
The issue likely stems from the fact that the `negative` string contains leading or trailing whitespace that doesn't match exactly with any entries in the `titles` list. Python's string comparison is strict and considers whitespace. You can fix this by stripping the whitespace from each element in the `negative` list before checking for membership in the `titles` list. Here's the corrected code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | @app.route('/getsimilar', methods=['GET']) def getsimilar(): print('args', request.args) positive = request.args.get('positive', '') positive = positive.split("##") negative = request.args.get('negative', '') negative = negative.split("##") keep_yes = [i for i, x in enumerate(titles) if x in positive] set1_yes = emb[keep_yes] keep_no = [i for i, x in enumerate(titles) if x.strip() in negative] set1_no = emb[keep_no] print("OK", len(set1_yes), len(set1_no)) |
I asked a question about why some code was incorrect. The answer is "because the input contains an extra curly brace }". Does this student answer say that? The issue likely stems from the fact that the `negative` string contains leading or trailing whitespace that doesn't match exactly with any entries in the `titles` list. Python's string comparison is strict and considers whitespace. You can fix this by stripping the whitespace from each element in the `negative` list before checking for membership in the `titles` list. Here's the corrected code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | @app.route('/getsimilar', methods=['GET']) def getsimilar(): print('args', request.args) positive = request.args.get('positive', '') positive = positive.split("##") negative = request.args.get('negative', '') negative = negative.split("##") keep_yes = [i for i, x in enumerate(titles) if x in positive] set1_yes = emb[keep_yes] keep_no = [i for i, x in enumerate(titles) if x.strip() in negative] set1_no = emb[keep_no] print("OK", len(set1_yes), len(set1_no)) |
The student fails. The provided answer does not address the issue of an extra curly brace `}` in the input, as mentioned in the question. Instead, it discusses a different problem related to leading or trailing whitespace in the `negative` string and how to correct it by stripping whitespace for string comparison. This does not answer the question about the incorrect code due to an extra curly brace.
Testing if the previous output contains the string student passes
: False