joncalhoun/algorithmswithgo.com

DecToBase etc. doesn't test conversion of 0

thomasweitzel opened this issue · 1 comments

There's an important test case missing: DecToBase(0, ...). Same goes for BaseToDec.

In case of DecToBase(0, ...) an empty String is returned, which probably should be "0" instead.

I had to modify my recursive function - it now uses a helper function and duplicates some code:

const digits = "0123456789ABCDEF"

func _decToBase(dec, base int) string {
	if dec == 0 {
		return ""
	}
	digit := dec % base
	return _decToBase(dec/base, base) + string(digits[digit])
}

func DecToBase(dec, base int) string {
	digit := dec % base
	return _decToBase(dec/base, base) + string(digits[digit])
}

Thanks! I'll add a test case for this and amend the video to address it.