Learn to Code in 5 Minutes a Day: Lesson 8
By Code with ADHD
- 2 minutes read - 296 wordsLesson 7 Solution: Roman Numeral II
If you completed lesson 7, you should have gotten something like the following. Remember, there is no one correct way to program any more than there is one right way to write a story or paint a picture. If you came up with a slightly different solution but it passes your unit tests, awesome!!!
roman_test.go
func TestRoman1(t *testing.T) {
result := roman(1)
expected := "I"
if result != expected {
t.Errorf("result was incorrect, got: %v, want: %v.", result, expected)
}
}
func TestRoman2(t *testing.T) {
result := roman(2)
expected := "II"
if result != expected {
t.Errorf("result was incorrect, got: %v, want: %v.", result, expected)
}
}
roman.go
func roman(number int) string {
if number == 2 {
return "II"
}
return "I"
}
If you didn’t get this working, go back and see if you can figure it out without cutting and pasting or just re-typing. Deleting what you have and seeing if you can retrace your steps is a great way to figure out how much you’ve absorbed.
Lesson 8: Roman Numeral III
For today’s session go ahead and write a test that tests roman(3) gives back “III”.
Make sure you have a good handle on doing this because next lesson we will add another new concept.
Again, if your solution looks slightly different from mine, that is absolutely awesome. It means you’re starting to write your own code instead of blindly copying mine.
Standard reminder: this is not a speed race. No one is watching how fast you’re going. If you want to spend a couple days or a week here, that’s better than charging ahead and getting lost.
Don’t forget to run “go fmt” on your code to keep it looking nice and legible.