easy +8 pts

Generate CSV row

Convert a list of values into a properly escaped CSV line.

Write a function `csv_row(values)` that takes a list of values (each value is either an int, float, or str) and returns a single CSV line as a string. Rules: - Separate fields with a comma (no spaces after commas). - If a field contains a comma, double quote, or a newline character (\n or \r), enclose it in double quotes. - Inside a quoted field, double quotes are escaped by doubling them ("" ). - Numeric values (int, float) should be converted to strings using `str()` without any extra formatting. - The string must not have a trailing newline. You must implement this from scratch; do not use the `csv` module.

Constraints

- The input list will contain between 0 and 1000 items. - Each item is either an int, float, or str. Strings can contain any printable ASCII characters plus newlines, carriage returns, and double quotes. - Expected time complexity: O(n) where n is the total number of characters in the output.

Example

>>> csv_row(["Name", "Age", "City"])
'Name,Age,City'
>>> csv_row(["Alice", 30, "New York"])
'Alice,30,New York'
>>> csv_row(["Doe, John", "123 Main St", "NYC"])
'"Doe, John",123 Main St,NYC'
>>> csv_row(["He said \"Hi\"", "line\nbreak"])
'"He said ""Hi""","line\nbreak"'
8 points ~12 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Check for the characters `,`, `"`, `\n`, and `\r` to decide if a field needs quotes.
To escape quotes inside a quoted field, replace each `"` with `""`.
Use a list to collect the formatted fields, then join with commas.
Remember that numbers should simply be converted with `str()` and are never quoted (unless they somehow contain special chars, which they don't).
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.