Giter VIP home page Giter VIP logo

Comments (4)

arcangelo7 avatar arcangelo7 commented on August 27, 2024
def test_exponentiation(base_number, exponent, expected, solution_dict):
    if exponentiation(base_number, exponent, solution_dict) == expected:
        return True
    else:
        return False

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

print(test_exponentiation(3, 4, 81, dict())) # True
print(test_exponentiation(17, 1, 17, dict())) # True
print(test_exponentiation(2, 0, 1, dict())) # True

from 2019-2020.

sntcristian avatar sntcristian commented on August 27, 2024

This is a dynamic version of the exponentiation algorithm that uses the property of multiplication between powers with same bases to implement a divide and conquer approach.

def test_exponentiation(base_number, exp, d, expected):
    result = exponentiation_dp(base_number, exp, d)
    if expected == result:
        return True
    else:
        return False

def exponentiation_dp (base_number, exp, d):
    if (base_number, exp) not in d:
        if exp == 0:
            d[(base_number, exp)] = 1
        elif exp == 1:
            d[(base_number, exp)] = base_number
        else:
            half_exp = exp // 2
            exponentiation1 = exponentiation_dp(base_number, half_exp, d)
            exponentiation2 = exponentiation_dp(base_number, half_exp, d)
            if exp % 2 == 0:
                d[(base_number, exp)] = exponentiation1 * exponentiation2
            else:
                d[(base_number, exp)] = exponentiation1 * exponentiation2 * base_number
    return d[(base_number, exp)] 

print(test_exponentiation(2, 5, dict(), 32)) #True
print(test_exponentiation(4, 4, dict(), 256)) #True
print(test_exponentiation(2, 9, dict(), 512)) #True

from 2019-2020.

FrancescoFernicola avatar FrancescoFernicola commented on August 27, 2024
def test_exponentiation(base_number, exponent, expected, solution_dict):
    result = exponentiation(base_number, exponent, solution_dict)
    if result == expected:
        return True
    else:
        return False

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


print(test_exponentiation(3, 4, 81, dict())) #True
print(test_exponentiation(17, 1, 17, dict()))  #True
print(test_exponentiation(2, 0, 1, dict()))  #True
print(test_exponentiation(0, 0, 1, dict()))  #True
print(test_exponentiation(1, 0, 1, dict()))  #True
print(test_exponentiation(0, 2, 0, dict()))  #True

from 2019-2020.

essepuntato avatar essepuntato commented on August 27, 2024

Hi all,

please find attached my personal solution – also available online:

# Test case for the function
def test_exponentation(base_number, exponent, solution_dict, expected):
    result = exponentation(base_number, exponent, solution_dict)
    if expected == result:
        return True
    else:
        return False


# Code of the function
def exponentation(base_number, exponent, solution_dict):
    exp_pair = (base_number, exponent)

    if exp_pair not in solution_dict:
        if exponent == 0:
            solution_dict[exp_pair] = 1
        else:
            solution_dict[exp_pair] = base_number * exponentation(base_number, exponent - 1, solution_dict)

    return solution_dict[exp_pair]


# Tests
my_dict = {}
print(test_exponentation(3, 2, my_dict, 9))
print(test_exponentation(3, 4, my_dict, 81))
print(test_exponentation(17, 1, my_dict, 17))
print(test_exponentation(2, 0, my_dict, 1))
print(test_exponentation(2, 6, my_dict, 64))
print(test_exponentation(0, 15, my_dict, 0))
print(test_exponentation(0, 0, my_dict, 1))

from 2019-2020.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.