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

Lecture "Dynamic programming algorithms", exercise 2 #30

Open
essepuntato opened this issue Nov 30, 2022 · 6 comments
Open

Lecture "Dynamic programming algorithms", exercise 2 #30

essepuntato opened this issue Nov 30, 2022 · 6 comments
Labels

Comments

@essepuntato
Copy link
Contributor

Choose any recursive algorithm introduced in the previous chapters and provide a new implementation in Python following the dynamic programming approach.

@delete4ever
Copy link

def test_exponentiation(int_1, int_2, solution_dict, expected):
    if exponentiation(int_1, int_2, solution_dict) == expected:
        return True
    else:
        return False

def exponentiation(base_number, exponent, solution_dict):
    if base_number not in solution_dict:
        if exponent == 0: 
            solution_dict[base_number] = 1
        elif exponent == 1:
            solution_dict[base_number] = base_number
        else:
            solution_dict[base_number] = base_number*(exponentiation(base_number, exponent-1, solution_dict))
    return solution_dict.get(base_number)

print(test_exponentiation(100, 10, dict(), 100**10)) #True
print(test_exponentiation(2, 100, dict(), 2**100)) #True
print(test_exponentiation(123456, 0, dict(), 1)) #True

@n1kg0r
Copy link

n1kg0r commented Dec 1, 2022

from math import factorial as fc

def test_factorial_recursive_dp(n):
    return factorial_recursive_dp(n, dict()) == fc(n)

def factorial_recursive_dp(n, solution_dict):
    if n not in solution_dict:
        if n == 1:
            return 1
        else:
            return n * factorial_recursive_dp(n-1, solution_dict)
    return solution_dict

print(test_factorial_recursive_dp(5))
print(test_factorial_recursive_dp(1))
print(test_factorial_recursive_dp(28))

@EricaAndreose
Copy link

EricaAndreose commented Dec 4, 2022

Update! Now I know I can use tuples so:

def test_exponentation(base_number, exponent, solution_dict, expected):
    result = exponentation(base_number, exponent, solution_dict)
    if expected == result:
        return True
    else:
        return False


def exponentation(base_number, exponent, solution_dict: dict):
    couple=(base_number, exponent)
    if couple not in solution_dict:
        if exponent == 0:
            solution_dict[couple] = 1
        elif exponent == 1:
            solution_dict[couple] = base_number
        else:
            solution_dict[couple] = base_number * exponentation(base_number, exponent - 1, solution_dict)
    return solution_dict.get(couple)

# Test runs returning True
print(test_exponentation(3, 4, dict(), 81))
print(test_exponentation(17, 1, dict(), 17))
print(test_exponentation(2, 0, dict(), 1))
print(test_exponentation(2, 4, dict(), 16))
print(test_exponentation(3, 2, dict(), 9))
print(test_exponentation(3, 4, dict(), 81))
print(test_exponentation(17, 1, dict(), 17))
print(test_exponentation(2, 0, dict(), 1))
print(test_exponentation(2, 6, dict(), 64))
print(test_exponentation(0, 15, dict(), 0))
print(test_exponentation(0, 0, dict(), 1))

Previous attempt.

def test_exponentation(base_number, exponent, solution_dict, expected):
    result = exponentation(base_number, exponent, solution_dict)
    if expected == result:
        return True
    else:
        return False

def exponentation(base_number, exponent, solution_dict: dict):
    if base_number not in solution_dict:
        if exponent == 0:     # base case 1
            solution_dict[base_number] = 1
        elif exponent == 1:     # base case 2
            solution_dict[base_number] = base_number
        else:    # recursive step
            solution_dict[base_number] = base_number * exponentation(base_number, exponent - 1, solution_dict)
    return solution_dict.get(base_number)

# Test runs returning True
print(test_exponentation(3, 4, dict(), 81))
print(test_exponentation(17, 1, dict(), 17))
print(test_exponentation(2, 0, dict(), 1))
print(test_exponentation(2, 4, dict(), 16))

@lucia1299
Copy link

def test_my_exponentiation (base_number, exponent, solution_dict, expected):
    result = exponentiation(base_number, exponent, solution_dict)
    if result == expected:
        return True
    else: 
        return False

def exponentiation(base_number, exponent, solution_dict):
    solution_dict = dict()
    if base_number not in solution_dict:
        if exponent == 0:
            solution_dict[base_number] = 1
        elif exponent == 1:
            solution_dict[base_number] = base_number
        else:
            solution_dict[base_number] = base_number * (exponentiation(base_number, exponent-1, solution_dict))
    return solution_dict.get(base_number)



print(test_my_exponentiation (3, 4, {}, 81)) #True
print(test_my_exponentiation(17, 1, {}, 17)) #True
print(test_my_exponentiation(2, 0, {}, 1)) #True 

@giorgiacrosilla
Copy link

def test_exp(base_number, exponent, solution_dict, expected):
    result = exp(base_number, exponent, solution_dict)
    if expected == result:
        return True
    else: 
        return False 

def exp(base_number, exponent, solution_dict):
    if base_number not in solution_dict:
        if exponent == 0:
            solution_dict[base_number] = 1
        elif exponent == 1:
            solution_dict[base_number] = base_number
        else:
            solution_dict[base_number] = base_number * exp(base_number, exponent-1, solution_dict)
    return solution_dict.get(base_number)

print(test_exp(3,4,dict(), 81))
print(test_exp(17,1, dict(), 17))
print(test_exp(2,0,dict(), 1))

@alka2696
Copy link

def test_exponentation(base_number, exponent, solution_dict, expected):
    result = exponentation(base_number, exponent, solution_dict)
    if expected == result:
        return True
    else:
        return False


def exponentation(base_number, exponent, solution_dict):
    together=(base_number, exponent)
    if together not in solution_dict:
        if exponent == 0:
            solution_dict[together] = 1
        elif exponent == 1:
            solution_dict[together] = base_number
        else:
            solution_dict[together] = base_number * exponentation(base_number, exponent - 1, solution_dict)
    return solution_dict.get(together)

soultion_dict= dict()
# Test runs returning True
print(test_exponentation(3, 4, dict(), 81)) #True
print(test_exponentation(5, 6, dict(), 15625)) #True

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

No branches or pull requests

7 participants