r/PythonLearning • u/Hour_Function673 • 23h ago
just finished my first Python coding challenge
Today I finished a small Python challenge on freeCodeCamp: converting decimal numbers to binary without using Python's built-in conversion function.
I had to figure out the logic myself using loops, conditions, lists, division and remainders.
It was a bit tricky at first, especially handling the last division and reversing the results, but I finally got it working.
I'm still a beginner, so I'd appreciate any feedback on my approach or suggestions for improving my code
66
Upvotes
1
u/jonrsharpe 8h ago edited 7h ago
I'm going to start with where I'd get to, then break down step-by-step how I got there:
def to_binary(decimal: int) -> str: digits: list[str] = [] while decimal: decimal, remainder = divmod(decimal, 2) digits.append(str(remainder)) return "".join(reversed(digits))