Problem overview
This is the first version of a small project: a tiny copy of the Unix cat command, which prints text one line per row. The method cat(self, lines) receives a list of strings and returns one string in which the lines are separated by newline characters (\n). Printing that string shows each original line on its own row.
For ["hello", "world", "foo"] the result is 'hello\nworld\nfoo', and printing it gives:
hello
world
foo
Lines may be blank, and the list may be empty. An empty list gives an empty string.
Approach: a separator goes between lines
The result is built by putting "\n" between each two lines. The word "between" matters: with three lines there are two separators, not three, so the last line is not followed by a newline.
A loop that adds each line plus a newline would leave one newline too many at the end, and you would need extra code to remove it. Python's string method join does exactly the right thing in one call. It is called on the separator and takes the list: "\n".join(lines).
Step by step
The lesson has a single step: inside cat, return the lines glued together with join, using "\n" as the separator. There is nothing to set up first, because join accepts the whole list and handles every case: one line, many lines, blank lines and an empty list.
The solution in Python
class Solution:
def cat(self, lines):
return "\n".join(lines)
Some details of join that are easy to trip on:
- The separator is the string you call the method on. The list goes inside the parentheses. Writing
lines.join("\n")fails, because lists have nojoinmethod. - Every item must be a string.
"\n".join([1, 2])raisesTypeError: sequence item 0: expected str instance, int found. Convert numbers withstrfirst. - Strings are immutable.
joinbuilds and returns a new string, and the list is left as it was. - Joining is also the efficient way. Adding strings one at a time in a loop makes a new string on every step, while
joinbuilds the result once.
Edge cases and common mistakes
- An empty list. There is nothing to separate, so
cat([])returns''. - A single line. There is no separator at all:
cat(["x"])returns'x', with no newline. - Blank lines. An empty string is still a line.
cat(["a", "", "b"])returns'a\n\nb', which prints with an empty row in the middle. - One blank line looks like no lines.
cat([""])andcat([])both return''. The result is the same string, so the two inputs can't be told apart from it. - A trailing newline. The result ends with the last line's text, not with
\n. Adding+ "\n"would make every result one character too long and fail the lesson's tests. - Returning versus printing. The method returns the string. It does not call
print. - Passing a string instead of a list.
"\n".join("abc")treats the string as a sequence of characters and returns'a\nb\nc'. The method expects a list of lines.
Testing
These are the results of calling cat on a few inputs:
["hello", "world", "foo"]gives'hello\nworld\nfoo'.["a", "", "b"]gives'a\n\nb'.["x"]gives'x'.[""]gives''.[]gives''.
The next version builds on this one.
