المهمة · معالجة البيانات صعب 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.
توقيع الدالة
def parse_csv(text: str) -> list[list[str]]:أمثلة
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"']]بالإضافة إلى 14 اختباراً مخفياً واختبار مدخلات ضخمة واحد ضمن حد زمني؛ لا تُعرض مدخلاتها. تُقدَّم المهام للنماذج باللغة الإنجليزية.
المركز 1SpaceXAI Grok Build 0.1محلولة · 17 من أصل 17 اختباراتتمت الإجابة في 42 ثوانٍ · زمن التشغيل 481 مللي ثانية · $0.01 ✓ parse_csv('a,b,c\n1,2,3\n')المتوقع [['a', 'b', 'c'], ['1', '2', '3']] <1 مللي ثانية ✓ parse_csv('name,quote\r\n"Smith, J.","He said ""hi"""')المتوقع [['name', 'quote'], ['Smith, J.', 'He said "hi"']] <1 مللي ثانية ✓ <1 مللي ثانية✓ <1 مللي ثانية✓ <1 مللي ثانية✓ <1 مللي ثانية✓ <1 مللي ثانية✓ <1 مللي ثانية✓ 5 مللي ثانية✓ <1 مللي ثانية✓ <1 مللي ثانية✓ 1 مللي ثانية✓ <1 مللي ثانية✓ <1 مللي ثانية✓ <1 مللي ثانية✓ <1 مللي ثانية⏱ 467 مللي ثانيةالكود
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 Flashمحلولة · 17 من أصل 17 اختباراتتمت الإجابة في 14 ثوانٍ · زمن التشغيل 493 مللي ثانية · $0.0011 ✓ parse_csv('a,b,c\n1,2,3\n')المتوقع [['a', 'b', 'c'], ['1', '2', '3']] 1 مللي ثانية ✓ parse_csv('name,quote\r\n"Smith, J.","He said ""hi"""')المتوقع [['name', 'quote'], ['Smith, J.', 'He said "hi"']] 1 مللي ثانية ✓ <1 مللي ثانية✓ <1 مللي ثانية✓ 2 مللي ثانية✓ <1 مللي ثانية✓ 2 مللي ثانية✓ <1 مللي ثانية✓ <1 مللي ثانية✓ <1 مللي ثانية✓ 1 مللي ثانية✓ 2 مللي ثانية✓ 1 مللي ثانية✓ 1 مللي ثانية✓ 1 مللي ثانية✓ <1 مللي ثانية⏱ 474 مللي ثانيةالكود
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.5محلولة · 17 من أصل 17 اختباراتتمت الإجابة في 2.3 ثوانٍ · زمن التشغيل 524 مللي ثانية · $0.0003 ✓ parse_csv('a,b,c\n1,2,3\n')المتوقع [['a', 'b', 'c'], ['1', '2', '3']] <1 مللي ثانية ✓ parse_csv('name,quote\r\n"Smith, J.","He said ""hi"""')المتوقع [['name', 'quote'], ['Smith, J.', 'He said "hi"']] 2 مللي ثانية ✓ <1 مللي ثانية✓ <1 مللي ثانية✓ <1 مللي ثانية✓ <1 مللي ثانية✓ <1 مللي ثانية✓ <1 مللي ثانية✓ <1 مللي ثانية✓ <1 مللي ثانية✓ <1 مللي ثانية✓ <1 مللي ثانية✓ <1 مللي ثانية✓ <1 مللي ثانية✓ <1 مللي ثانية✓ <1 مللي ثانية⏱ 514 مللي ثانيةالكود
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 Nextغير محلولة · 14 من أصل 17 اختباراتتمت الإجابة في 10.0 ثوانٍ · زمن التشغيل 473 مللي ثانية · $0.0005 ✓ parse_csv('a,b,c\n1,2,3\n')المتوقع [['a', 'b', 'c'], ['1', '2', '3']] <1 مللي ثانية ✓ parse_csv('name,quote\r\n"Smith, J.","He said ""hi"""')المتوقع [['name', 'quote'], ['Smith, J.', 'He said "hi"']] 1 مللي ثانية ✓ 3 مللي ثانية✓ <1 مللي ثانية✓ <1 مللي ثانية✓ <1 مللي ثانية✓ <1 مللي ثانية✓ <1 مللي ثانية✓ <1 مللي ثانية✗ إجابة خاطئة✗ إجابة خاطئة✓ 2 مللي ثانية✓ <1 مللي ثانية✓ <1 مللي ثانية✗ إجابة خاطئة✓ <1 مللي ثانية⏱ 458 مللي ثانيةالكود
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 rowsالمحاولة الثانية
غير محلولة · 6 من أصل 17 اختباراتتمت الإجابة في 24 ثوانٍ · $0.0012اطلع النموذج على عدد الاختبارات التي فشلت، مع الأمثلة الخاطئة وقيمها المتوقعة والراجعة، واطلع على سبب فشل الاختبارات الخفية فقط دون مدخلاتها. لا تؤثر هذه المحاولة في الترتيب أو التقييم.
✓ parse_csv('a,b,c\n1,2,3\n')المتوقع [['a', 'b', 'c'], ['1', '2', '3']] <1 مللي ثانية ✗ parse_csv('name,quote\r\n"Smith, J.","He said ""hi"""')المتوقع [['name', 'quote'], ['Smith, J.', 'He said "hi"']]الناتج [['name', 'quote']] <1 مللي ثانية ✓ <1 مللي ثانية✗ إجابة خاطئة✗ إجابة خاطئة✗ إجابة خاطئة✗ إجابة خاطئة✗ إجابة خاطئة✓ <1 مللي ثانية✓ <1 مللي ثانية✗ إجابة خاطئة✗ إجابة خاطئة✗ إجابة خاطئة✗ إجابة خاطئة✓ <1 مللي ثانية✗ إجابة خاطئة⏱ 732 مللي ثانيةالكود
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
تكلفة الرموز: $0.02 · كيف تُدار الجولات وتُحتسب النقاط ←