Warning

This book is new. If you'd like to go through it, then join the Learn Code Forum to get help while you attempt it. I'm also looking for feedback on the difficulty of the projects. If you don't like public forums, then email help@learncodethehardway.org to talk privately.

Exercise 5: cat

With Exercise 4 you began the work of finding out what blocks you. It was a simple challenge of conducting a spike researching how best to get command line arguments from a user. The real purpose of the exercise was your lab notes on what you do to get started. Did you discover anything you need to change? Any strange habits or setup issues? In this exercise you'll be creating a replica of a simple command called cat, but your real purpose is to pick one thing to change about your setup so you get started faster. Remember that the point is not your implementation of cat. The point is how quickly can you get started and can you do something useful in 45 minutes.

As with the previous exercise, stick to the 45 minute deadline. Setting limits on how much time you'll spend on an exercise is a useful technique to get you into the mode of coding. In fact, if you can warm up with a 45 minute hack every day that would be an ideal practice going forward. Before you can do that you'll need to get better at starting, so figure out what friction you're going to kill today and let's begin.

Warning

I will state this one more time so that it is clear: You cannot fail at this exercise. If you approach the 45 minute limit as a graded exercise and set any expectations for how well you should or should not do then you are doing this wrong. The correct way to think about the 45 minutes is simply a device to kick you in the rear and get you going. It is not a test. I repeat, it is not a test. Keep telling yourself that so you relax and just go for it.

Exercise Challenge

The cat command is short for "concatenate," and it's most commonly used to dump the contents of a file to the screen. You use it like this:

cat somefile.txt

This command will spit out the contents of somefile.txt. That's not actually it's original purpose. Originally it was for combining more than one file--thus it was called cat. To do that simply add each file to cat:

cat A.txt B.txt C.txt

The cat command will then go through each file, write its contents out, and then exit when it's gone through all of them. The problem is, how does this possibly concatenate files? To do that you'll also need to use the POSIX file redirection features found in your Terminal:

cat A.txt B.txt C.txt > D.txt

The use of the > symbol should be familiar to you, and if not then you need to refresh yourself on basic Unix shell operation. It simply takes the standard output of the cat command (which in this case is the contents of A.txt B.txt C.txt all combined) and writes it to the file on the right D.txt.

You are to reimplement as much of the cat command as quickly as you can, using what you learned from Exercise 4 to get command line arguments. Remember that to do standard output you just use print in Python. To learn more about cat use the man system like this:

man cat

That is the manual for the cat command, and you get bonus points for implementing as much of it as you can in the 45 minute time slot.

Solution

You can find my solution to the problem at the project repository at http://bit.ly/lmpthwsolve on github. It will be in ex5/, and you'll see I did a fairly quick and dirty solution. If you began this exercise worried about quality or creativity then you are doing this wrong. Your job is to be sloppy, fast, and get it done. The point of the time limit is to get you past this notion that every time you touch the keyboard you have to produce a golden code calf to worship. Do it as best you can, and then after you can go analyze and see what to improve.

Study Drills

  1. Were there any surprising features of cat that you have never used or were difficult to implement?
  2. Were you able to remove your one friction blocker from you starting process? This is more important than implementing cat, so if you didn't avoid that blocker then you need to do this exercise again.
  3. Did you identify more things that are getting in your way? Simple things like your neck hurts because where you sit is too low? Not being a good touch typer? How about your mental state? Were there things you thought that blocked you? Can you stop thinking them?

Further Study

This isn't a self-help book, and I'm not going to fix your psychology, but I find that a huge blocker to learning something new isn't the subject but rather your fears. If in this exercise you found that damaging thoughts or fears were keeping you from getting started, then I suggest you start doing 10 minutes of journaling about how you feel before you do your 45 minute hack. Writing down your fears, anxieties and feelings will articulate them and helps you see how impractical it is to worry about them for something as tiny as a 45 minute hack. Try it. You'd be surprised what 10 minutes of writing about you feels does for your feels.

Pre-order Learn More Python The Hard Way

When you pre-order Learn More Python The Hard Way, you'll receive the Python 3 Edition as it's being created. All files are DRM free and you can download them to your computer for offline viewing. Digital Download Only! You do not get a physical book.

$29.99

Pre-Order From Zed

Or, you can read Learn More Python the Hard Way for free right here, video lectures not included.

Other Buying Options

Other buying options coming soon.

No content available for this exercise. You can view all available downloads at your customer account page.