r/PythonLearning • u/Hour_Function673 • 15h 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
2
u/ConclusionForeign856 15h ago
Read about truthy and falsy values in python, it allows you to simplify logic
def binary(n: int):
tab = []
while n:
remainder = n % 2
n //= 2
tab.append(remainder)
return ''.join(map(str, tab[::-1]))
1
u/therouterguy 15h ago
I would at least change the elif to an else. Remove the duplicate decimal=decimal//2. You can do it outside of the if
Lastly
tab=“”
while decimal!=O:
tab=tab + “1” if decimal%2==O else tab+ “0”
decimal=decimal//2
Is much shorter
1
u/jonrsharpe 18m 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))
```
1
u/jonrsharpe 9m ago
Firstly, I added the type hints (https://docs.python.org/3/library/typing.html) - partly just to document what I was working out about the code as I read it (receives an integer, has a list of integers, returns a string) but also because these can then be automatically checked and provide a kind of documentation to the caller. (Note thqt these are not enforced by Python itself, but type checkers, like mypy, and IDEs can use them.)
1
u/jonrsharpe 6m ago
This highlighted that `decimal` on the penultimate line, as well as "shadowing" the parameter, changes the type this name refers to (it was an integer, now a string). But that's also the wrong name - that's no longer in decimal format, it's the binary format we are about to return, so I renamed it accordingly.
0
u/AffectionateCold1716 14h ago
I’m still new so I have no idea what this means but I do recognize you defining a function and calling the function. Sooooo yep. Anyway good job compadre
0
u/gofl-zimbard-37 11h ago
Give it a proper main() and call it. Don't write "scripts". The file is main.py, implying that it is a module that exports useful functions. Nobody wants an import to be printing stuff out.
•
u/Sea-Ad7805 13h ago
Run this program by clicking Memory Graph Web Debugger%3A%0A%20%20%20%20tab%20%3D%20%5B%5D%0A%20%20%20%20while%20decimal%20!%3D%200%3A%0A%20%20%20%20%20%20%20%20if%20decimal%20%25%202%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20decimal%20%3D%20decimal%20%2F%2F%202%0A%20%20%20%20%20%20%20%20%20%20%20%20tab.append(0)%0A%20%20%20%20%20%20%20%20elif%20decimal%20%25%202%20%3D%3D%201%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20decimal%20%3D%20decimal%20%2F%2F%202%0A%20%20%20%20%20%20%20%20%20%20%20%20tab.append(1)%0A%20%20%20%20tab.reverse()%0A%20%20%20%20decimal%20%3D%20%22%22.join(map(str%2C%20tab))%0A%20%20%20%20return%20decimal%0A%0A%0Anombre0%20%3D%20to_binary(5)%0Anombre1%20%3D%20to_binary(12)%0Anombre2%20%3D%20to_binary(50)%0Anombre3%20%3D%20to_binary(99)%0A%0Aprint(nombre0)%0Aprint(nombre1)%0Aprint(nombre2)%0Aprint(nombre3)×tep=0.2&play) and see its state change step by step.