Created
November 20, 2021 01:14
-
-
Save luisvonmuller/44b27fe80624e6091e9c7c3e34f02c8b to your computer and use it in GitHub Desktop.
INF1031 - Introdução à computação - Tarefa 7 pro @ - do discord
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)) | |
# PS: Você pode só passar a base para a própria função de inteiros do python... int(num, base) => num @ base10 | |
# print(int("116", 16)) | |
# print(int("10011", 2)) | |
# Tarefa 7.2 ================================================================= | |
# Crie função recursiva e não-recursiva que recebem como parâmetro um número N na base | |
# 10, uma base B e retornam o número correspondente na base B. | |
# Não recursiva (iteravel) | |
def sete_ponto_dois(base, valor): | |
acumulador = "" | |
while base <= valor: | |
acumulador += str(valor % base) | |
valor = valor // base | |
return str(valor % base) + acumulador[::-1] | |
# Recursiva. | |
def sete_ponto_dois_recursivo(base, valor, acumulador=""): | |
resto = valor // base | |
if base > valor: | |
return print(str(valor % base) + acumulador[::-1]) | |
else: | |
sete_ponto_dois_recursivo(base, resto, acumulador + str(valor % base)) | |
print(sete_ponto_dois(2, 19)) | |
sete_ponto_dois_recursivo(2, 19) | |
print(sete_ponto_dois(16, 278)) | |
sete_ponto_dois_recursivo(16, 278) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment