Check-in 3: Fix unit-tested code
Once you’ve written tests for the
and used them to expose the three bugs,
Try fixing the code for
formatInt so that all the tests pass.
- For the
size=0bug, it’s okay to require
size>0as a precondition, and then test for the
- For the leading-zero bug, the problem is that
"48"when it should be
"048". Instead of using
str(), you can use Python format strings like this:
>>> "%0*d" % (8, 415) '00000415' >>> "%0*d" % (5, 415) '00415'
where the first number in the pair is the
- The trickiest is probably the negative number bug. I suggest
checking for a negative number explicitly, and either setting a
boolean flag, or else a string to represent the
if num < 0: sign = "-" num = - num # Make it positive else: sign = ""
Then you can proceed with doing the formatting for the positive number, and when done, stick the
Before committing, you should create a file at the top of your
.gitignore (notice the leading dot), with the
__pycache__/ .coverage htmlcov/
That will ensure that temporary object files and coverage reports do not get committed to the repository.