Created
November 20, 2021 00:06
-
-
Save luisvonmuller/a3534a9873ed9dd22a17483724d52f39 to your computer and use it in GitHub Desktop.
LiLas... 7.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Tarefa 7.1 ================================================================= | |
# Crie função recursiva e não-recursiva que recebem como parâmetro a base B e um número | |
# N na base B e retornam o número correspondente na base 10. Para representar os números | |
# usar apenas digitos. | |
def sete_ponto_um_recursiva(base, valor, n=0, acumulador=0): | |
if str(valor)[:-1] == '': | |
return print(acumulador + (valor * (base ** n))) | |
else: | |
position = int(repr(valor)[-1]) | |
acumulador += position * (base ** n) | |
sete_ponto_um_recursiva(base, | |
int(str(valor)[:-1]), | |
n + 1, | |
acumulador) | |
def sete_ponto_um(base, valor): | |
acumulador = 0 | |
n = 0 | |
for position in repr(valor)[::-1]: | |
acumulador += int(position) * (base ** n) | |
n = n + 1 | |
return acumulador | |
sete_ponto_um_recursiva(16, 116) | |
sete_ponto_um_recursiva(2, 10011) | |
print(sete_ponto_um(16, 116)) | |
print(sete_ponto_um(2, 10011)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment