Quantcast
Viewing all articles
Browse latest Browse all 74

Answer by ergonaut for Convert int to binary string in Python

Here's yet another way using regular math, no loops, only recursion. (Trivial case 0 returns nothing).

def toBin(num):  if num == 0:    return ""  return toBin(num//2) + str(num%2)print ([(toBin(i)) for i in range(10)])['', '1', '10', '11', '100', '101', '110', '111', '1000', '1001']

Viewing all articles
Browse latest Browse all 74

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>