I traced the following code, and one part doesn't make sense to me:
def bin_rep(n: int) -> str:
"""Return the binary representation of n
>>> bin_rep(0)
"0"
>>> bin_rep(1)
"1"
>>> bin_rep(5)
"101"
"""
if n > 1:
return bin_rep(n // 2) + bin_rep(n % 2)
else:
return str(n)
The part that I don't understand is, why are we adding bin_rep(n//2) and bin_rep(n%2). I know that without the addition this won't work, but I can't wrap my head around why the addition exists there.
bin_repalways returns a string, this is a string concatenation rather than an addition. Does that help you?