Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solución reto#39 Python #1379

Open
grisales54 opened this issue Oct 18, 2024 · 0 comments
Open

Solución reto#39 Python #1379

grisales54 opened this issue Oct 18, 2024 · 0 comments

Comments

@grisales54
Copy link

`def decimal(binario: int):
# Convierte cada dígito de la cadena binaria a un entero y lo almacena en una lista
binario = [int(i) for i in binario]

# Calcula el valor decimal sumando cada bit multiplicado por la potencia correspondiente de 2
# Se recorre la lista desde el último elemento hasta el primero, usando el exponente de 2 adecuado
decimal = sum(binario[-i - 1] * 2 ** (i) for i in range(0, len(binario)))

# Retorna el valor decimal calculado
return decimal

Cadena binaria que se va a convertir a decimal

binario = str(11111)

Imprime el resultado de la conversión binaria a decimal

print(decimal(binario))`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant