Défi · traitement de données difficile Python ·
Parse CSV
Parse CSV text into rows of fields. Fields are separated by commas and rows by "\n" or "\r\n". A field may be enclosed in double quotes: inside quotes, commas, "\r" and "\n" are ordinary characters and "" stands for one double quote. Outside quotes every character is taken literally (no trimming). A quoted field is always followed by a comma, a line break or the end of the text.
A line break at the very end of the text does not start another row, and the empty text has no rows. An empty line elsewhere is a row with one empty field.
Signature
def parse_csv(text: str) -> list[list[str]]:Exemples
parse_csv('a,b,c\n1,2,3\n') # -> [['a', 'b', 'c'], ['1', '2', '3']]
parse_csv('name,quote\r\n"Smith, J.","He said ""hi"""') # -> [['name', 'quote'], ['Smith, J.', 'He said "hi"']]Plus 14 tests masqués et 1 test de performance sous contrainte de temps ; leurs entrées ne sont pas affichées. Les défis sont soumis aux modèles en anglais.
#1SpaceXAI Grok Build 0.1résolu · 17 sur 17 testsréponse en 42 s · exécution 481 ms · $0.01 ✓ parse_csv('a,b,c\n1,2,3\n')attendu [['a', 'b', 'c'], ['1', '2', '3']] <1 ms ✓ parse_csv('name,quote\r\n"Smith, J.","He said ""hi"""')attendu [['name', 'quote'], ['Smith, J.', 'He said "hi"']] <1 ms ✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ 5 ms✓ <1 ms✓ <1 ms✓ 1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms⏱ 467 msCode
def parse_csv(text: str) -> list[list[str]]: rows: list[list[str]] = [] i = 0 n = len(text) while i < n: row: list[str] = [] while True: field: list[str] = [] quoted = False if i < n and text[i] == '"': quoted = True i += 1 while i < n: c = text[i] if quoted: if c == '"': if i + 1 < n and text[i + 1] == '"': field.append('"') i += 2 continue else: i += 1 quoted = False break else: field.append(c) i += 1 else: if c == ',' or c == '\n' or c == '\r': break field.append(c) i += 1 row.append(''.join(field)) if i >= n: break c = text[i] if c == ',': i += 1 continue elif c == '\n': i += 1 break elif c == '\r': i += 1 if i < n and text[i] == '\n': i += 1 break else: break rows.append(row) return rows#1MiniMax M3.1 Flashrésolu · 17 sur 17 testsréponse en 14 s · exécution 493 ms · $0.0011 ✓ parse_csv('a,b,c\n1,2,3\n')attendu [['a', 'b', 'c'], ['1', '2', '3']] 1 ms ✓ parse_csv('name,quote\r\n"Smith, J.","He said ""hi"""')attendu [['name', 'quote'], ['Smith, J.', 'He said "hi"']] 1 ms ✓ <1 ms✓ <1 ms✓ 2 ms✓ <1 ms✓ 2 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ 1 ms✓ 2 ms✓ 1 ms✓ 1 ms✓ 1 ms✓ <1 ms⏱ 474 msCode
def parse_csv(text: str) -> list[list[str]]: rows: list[list[str]] = [] row: list[str] = [] field: list[str] = [] in_quotes = False at_field_start = True ended_with_break = False i = 0 n = len(text) while i < n: c = text[i] if in_quotes: if c == '"': if i + 1 < n and text[i + 1] == '"': field.append('"') i += 2 else: in_quotes = False i += 1 else: field.append(c) i += 1 continue if at_field_start and c == '"': in_quotes = True at_field_start = False ended_with_break = False i += 1 elif c == ',': row.append(''.join(field)) field.clear() at_field_start = True ended_with_break = False i += 1 elif c == '\n': row.append(''.join(field)) field.clear() rows.append(row) row = [] at_field_start = True ended_with_break = True i += 1 elif c == '\r' and i + 1 < n and text[i + 1] == '\n': row.append(''.join(field)) field.clear() rows.append(row) row = [] at_field_start = True ended_with_break = True i += 2 else: field.append(c) at_field_start = False ended_with_break = False i += 1 if n and not ended_with_break: row.append(''.join(field)) rows.append(row) return rows#1Anthropic Claude Haiku 5.5résolu · 17 sur 17 testsréponse en 2.3 s · exécution 524 ms · $0.0003 ✓ parse_csv('a,b,c\n1,2,3\n')attendu [['a', 'b', 'c'], ['1', '2', '3']] <1 ms ✓ parse_csv('name,quote\r\n"Smith, J.","He said ""hi"""')attendu [['name', 'quote'], ['Smith, J.', 'He said "hi"']] 2 ms ✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms⏱ 514 msCode
def parse_csv(text: str) -> list[list[str]]: rows = [] n = len(text) if n == 0: return rows row = [] field = [] i = 0 in_quotes = False # track whether we are at start of a field (for quoted field detection) field_start = True while i < n: c = text[i] if in_quotes: if c == '"': if i + 1 < n and text[i + 1] == '"': field.append('"') i += 2 continue in_quotes = False i += 1 continue field.append(c) i += 1 continue if c == '"' and field_start: in_quotes = True field_start = False i += 1 continue if c == ',': row.append(''.join(field)) field = [] field_start = True i += 1 continue if c == '\r' and i + 1 < n and text[i + 1] == '\n': row.append(''.join(field)) rows.append(row) row = [] field = [] field_start = True i += 2 continue if c == '\n': row.append(''.join(field)) rows.append(row) row = [] field = [] field_start = True i += 1 continue field.append(c) field_start = False i += 1 # end of text: finish last row if any content pending if field or row or not (text.endswith('\n')): row.append(''.join(field)) rows.append(row) return rows#4Qwen3 Coder Nextnon résolu · 14 sur 17 testsréponse en 10.0 s · exécution 473 ms · $0.0005 ✓ parse_csv('a,b,c\n1,2,3\n')attendu [['a', 'b', 'c'], ['1', '2', '3']] <1 ms ✓ parse_csv('name,quote\r\n"Smith, J.","He said ""hi"""')attendu [['name', 'quote'], ['Smith, J.', 'He said "hi"']] 1 ms ✓ 3 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✗ mauvaise réponse✗ mauvaise réponse✓ 2 ms✓ <1 ms✓ <1 ms✗ mauvaise réponse✓ <1 ms⏱ 458 msCode
def parse_csv(text: str) -> list[list[str]]: if not text: return [] rows = [] current_row = [] current_field = [] in_quotes = False i = 0 n = len(text) while i < n: char = text[i] if in_quotes: if char == '"': # Check if next character is also a quote if i + 1 < n and text[i + 1] == '"': current_field.append('"') i += 2 continue else: in_quotes = False i += 1 continue else: current_field.append(char) i += 1 continue else: # Not in quotes if char == '"': in_quotes = True i += 1 continue elif char == ',': current_row.append(''.join(current_field)) current_field = [] i += 1 continue elif char == '\r': # Handle \r\n or standalone \r if i + 1 < n and text[i + 1] == '\n': i += 1 # Skip \n current_row.append(''.join(current_field)) current_field = [] rows.append(current_row) current_row = [] i += 1 continue elif char == '\n': current_row.append(''.join(current_field)) current_field = [] rows.append(current_row) current_row = [] i += 1 continue else: current_field.append(char) i += 1 continue # Add the last field if exists if current_field or current_row: current_row.append(''.join(current_field)) rows.append(current_row) # Handle trailing newline - if last row is empty after trailing newline, remove it # A line break at the very end does not start another row # So if the last row is just an empty field due to trailing newline, remove it if rows and len(rows[-1]) == 1 and rows[-1][0] == '' and text.endswith(('\r', '\n')): # Only remove if the text ended with a newline and the last row is empty # But we need to check if there was a field before the final newline # Reconstruct: the empty row should only be added if the line break was not at the very end # Actually, our logic already handled this: a trailing \n or \r\n adds an empty field and a new row # So we need to check if the last row is empty and the text ended with a newline # In that case, we should remove the last row rows.pop() return rowsSecond essai
non résolu · 6 sur 17 testsréponse en 24 s · $0.0012Le modèle a eu accès au nombre de tests échoués, aux exemples en échec avec les valeurs attendues et obtenues, ainsi qu'au motif d'échec des tests cachés (sans leurs entrées). Ne compte ni pour le classement ni pour l'évaluation.
✓ parse_csv('a,b,c\n1,2,3\n')attendu [['a', 'b', 'c'], ['1', '2', '3']] <1 ms ✗ parse_csv('name,quote\r\n"Smith, J.","He said ""hi"""')attendu [['name', 'quote'], ['Smith, J.', 'He said "hi"']]obtenu [['name', 'quote']] <1 ms ✓ <1 ms✗ mauvaise réponse✗ mauvaise réponse✗ mauvaise réponse✗ mauvaise réponse✗ mauvaise réponse✓ <1 ms✓ <1 ms✗ mauvaise réponse✗ mauvaise réponse✗ mauvaise réponse✗ mauvaise réponse✓ <1 ms✗ mauvaise réponse⏱ 732 msCode
def parse_csv(text: str) -> list[list[str]]: if not text: return [] rows = [] current_row = [] current_field = [] in_quotes = False i = 0 n = len(text) while i < n: char = text[i] if in_quotes: if char == '"': # Check if next character is also a quote if i + 1 < n and text[i + 1] == '"': current_field.append('"') i += 2 continue else: in_quotes = False i += 1 continue else: current_field.append(char) i += 1 continue else: # Not in quotes if char == '"': in_quotes = True i += 1 continue elif char == ',': current_row.append(''.join(current_field)) current_field = [] i += 1 continue elif char == '\r': # Handle \r\n or standalone \r current_row.append(''.join(current_field)) current_field = [] rows.append(current_row) current_row = [] if i + 1 < n and text[i + 1] == '\n': i += 2 else: i += 1 continue elif char == '\n': current_row.append(''.join(current_field)) current_field = [] rows.append(current_row) current_row = [] i += 1 continue else: current_field.append(char) i += 1 continue # Add the last field/row if exists current_row.append(''.join(current_field)) rows.append(current_row) # Remove trailing empty row caused by trailing newline if rows and len(rows[-1]) == 1 and rows[-1][0] == '' and (text.endswith('\n') or text.endswith('\r')): # Check if the last character was a newline that didn't create meaningful data # Only remove if the last field was empty and we had a trailing newline # But also handle case where text ends with \r\n but we already processed it # Our logic adds a row even for trailing newline, so remove that last row # Only if that row is effectively empty (just one empty field) if len(rows) > 1 and (text.endswith('\r\n') or text.endswith('\n')): # Check if the row before last was non-empty or if this is just a continuation # Actually simpler: remove last row only if it's empty and we had trailing newline # But need to be careful: text = "a\n" should give [['a']], not [['a'], ['']] # Our current logic already adds row on newline, so last row might be empty # Let's just check if last row is empty and remove it # But: text = "" is handled above, text = "\n" should give [['']]? # Actually, per spec: "A line break at the very end of the text does not start another row" # So "a\n" should give [['a']], not [['a'], ['']] # This means we shouldn't have added the empty field in the first place # The issue is our logic: we always add a field on newline pass # Actually, let's reconsider the trailing newline case # If the text ends with \n or \r\n, that shouldn't create a new row # So if the last character(s) are a newline, we should remove the last empty row if it exists # Better approach: process all but the last newline if trailing # Let's handle this differently: when we hit a newline, we add the row, but if it's at the very end, skip adding the row # Re-implement with cleaner logic rows = [] current_row = [] current_field = [] in_quotes = False i = 0 n = len(text) while i < n: char = text[i] if in_quotes: if char == '"': if i + 1 < n and text[i + 1] == '"': current_field.append('"') i += 2 else: in_quotes = False i += 1 else: current_field.append(char) i += 1 else: if char == '"': in_quotes = True i += 1 elif char == ',': current_row.append(''.join(current_field)) current_field = [] i += 1 elif char == '\r': if i + 1 < n and text[i + 1] == '\n': # \r\n case # Check if this is the very end of text if i + 2 == n: # Trailing \r\n - don't create new row current_row.append(''.join(current_field)) rows.append(current_row) break else: current_row.append(''.join(current_field)) current_field = [] rows.append(current_row) current_row = [] i += 2 else: # Standalone \r (just \r) if i + 1 == n: current_row.append(''.join(current_field)) rows.append(current_row) break else: current_row.append(''.join(current_field)) current_field = [] rows.append(current_row) current_row = [] i += 1 elif char == '\n': # Standalone \n if i + 1 == n: # Trailing \n - don't create new row current_row.append(''.join(current_field)) rows.append(current_row) break else: current_row.append(''.join(current_field)) current_field = [] rows.append(current_row) current_row = [] i += 1 else: current_field.append(char) i += 1 return rows
Coût en jetons: $0.02 · Déroulement et calcul des scores →