Code Monkey home page Code Monkey logo

Comments (18)

delfimpandiani avatar delfimpandiani commented on June 13, 2024 3

First I approached the problem similarly to the multiplication recursion. That is, I wrote with pencil & paper a way to conceive of exponentiation in a recursive way. For example for 3^4:

3^4 = 3 x 3 x 3 x 3
3^4 = 3 x (3^3)
3^4 = 3 x (3 x (3^2))
3^4 = 3 x (3 x (3 x (3^1)))
3^4 = 3 x (3 x (3 x (3 x (3^0))))

Since we know that any number raised to the power of 0 will give 1 (such as, 2^0=1, just like 64235626^0=1), we can use this as the base case.

# Test case for the exponentiation algorithm

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

# Code of the exponentiation algorithm

def exponentiation(base_number, exponent):
    if exponent == 0: #this is the application of the base case as I mentioned above
        return 1 # any number to the power of 0 equals 1
    else:
        return base_number * exponentiation(base_number, exponent - 1) # reduce the exponent by one and multiply this whole thing by the base_number

# Three test runs of the algorithm

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

from 2018-2019.

MattiaSpadoni avatar MattiaSpadoni commented on June 13, 2024 2

All true, I was stack in the temptative of doing this thing with another way, but I'm not able to solve a problems with variables.

image

from 2018-2019.

hizclick avatar hizclick commented on June 13, 2024 1
def test_pwr(n, expo, expected):
    if pwr(n, expo) == expected:
        return True
    else:
        return False
     
def pwr(n, expo):
    if(expo==0):
        result = 1
    elif(expo==1):
        result = n
    else: 
        result = n * pwr(n,expo-1)
    return result
print(test_pwr(3,4,81))
print(test_pwr(17,1,17))
print(test_pwr(2,0,1))
#true 
#true 
#true 

from 2018-2019.

simayguzel avatar simayguzel commented on June 13, 2024 1
def test_exponentiation(n,e, expected):
    result = exponentiation(n,e)
    if expected == result:
        return True
    else:
        return False

def exponentiation(n, e):
    if e==0:
        return 1
    elif e==1:
        return n
    else:
        return n * exponentiation(n,e-1)
        
print(test_exponentiation(3,4,81))      #True
print(test_exponentiation(17,1,17))     #True
print(test_exponentiation(2,0,1))         #True

from 2018-2019.

beccadelbens avatar beccadelbens commented on June 13, 2024 1
#Test case for the algorithm
def test_exponentiation(base_number, exponent, expected):
    result = exponentiation(base_number, exponent)
    if expected == result:
        return True
    else:
        return False

#Code of the algorithm
def exponentiation(base_number, exponent):
    if exponent == 0:
        return 1
    else:
        return base_number * exponentiation(base_number, exponent - 1)

print(exponentiation(5, 3)) #return 125

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

from 2018-2019.

friendlynihilist avatar friendlynihilist commented on June 13, 2024 1

Provided that in recursion x y = x * x y-1, that's my idea:

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


def exponentiation(base_number, exponent):
    if exponent == 0:
        return 1
    else:
        return base_number * exponentiation(base_number, exponent - 1)


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

from 2018-2019.

tceron avatar tceron commented on June 13, 2024 1
def test_exponentiation(num, exponent, expected):
    if  exponentiation(num, exponent) == expected:
        return True
    else: 
        return False

def exponentiation(num, exponent):
    if exponent == 0:
        return 1
    elif exponent == 1:
        return num
    else: 
        result = num * exponentiation(num,exponent - 1)
    return result

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

from 2018-2019.

mangiafrangette avatar mangiafrangette commented on June 13, 2024 1
def test_my_alg(n, e, expected):
    return my_alg(n, e) == expected


def my_alg(n, e):

   if e == 0:
       output = 1
   else:
       output = n * my_alg(n, e - 1)

   return output


n1 = 3
e1 = 4
expected1 = 81
print(test_my_alg(n1, e1, expected1))
n2 = 17
e2 = 1
expected2 = 17
print(test_my_alg(n2, e2, expected2))
n3 = 2
e3 = 0
expected3 = 1
print(test_my_alg(n3, e3, expected3))

from 2018-2019.

SeverinJB avatar SeverinJB commented on June 13, 2024 1
# Test case for the algorithm 
def test_exponentiation(base_number, exponent, expected): 
    result = exponentiation(base_number, exponent) 
    if expected == result: 
        return True 
    else: 
        return False 

# Code of the algorithm
def exponentiation(base_number, exponent):
    if exponent == 0: 
        return 1
    else:        
        return base_number * exponentiation(base_number, exponent - 1) 

# Test Cases 
print(test_exponentiation(3, 4, 81)) 
print(test_exponentiation(17, 1, 17)) 
print(test_exponentiation(2, 0, 1))

from 2018-2019.

ilsamoano avatar ilsamoano commented on June 13, 2024 1
# Test case for the algorithm
def exponentiation_test(base_number, exponent, expected):
     if exponentiation(base_number, exponent)== expected:
         return True
     else:
         return False
     
#code         
def exponentiation(base_number, exponent):
    if exponent == 0:
        return 1     
    else:
        return base_number * exponentiation(base_number, exponent - 1)
 
#test cases   
print(exponentiation_test(3,4,81))        
print(exponentiation_test(17,1,17))
print (exponentiation_test(2,0,1))

from 2018-2019.

MilenaCorbellini avatar MilenaCorbellini commented on June 13, 2024 1

esercizio 11

from 2018-2019.

andreamust avatar andreamust commented on June 13, 2024 1
def test_exponentation(base_number, exponent, expected):
    result = exponentation(base_number, exponent)
    if expected == result:
        return True
    else:
        return False


def exponentation(base_number, exponent):
    if exponent == 0:
        return 1
    if exponent == 1:
        return base_number
    else:
        return base_number * exponentation(base_number, exponent - 1)


print(test_exponentation(3, 4, 81))
print(test_exponentation(17, 1, 17))
print(test_exponentation(2, 0, 1))

from 2018-2019.

federicabologna avatar federicabologna commented on June 13, 2024

def test_exp(n, e, expected):
if expected == exp(n, e):
return True
else:
return False

def exp(n, e):
if e == 0:
return 1
else:
return n*exp(n, e-1)

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

True
True
True

from 2018-2019.

EleonoraPeruch avatar EleonoraPeruch commented on June 13, 2024
# Test case for the algorithm
def test_exponentiation(base_number, exponent, expected):
    result = base_number ** exponent
    if expected == result:
        return True
    else:
        return False

# Code of the algorithm
def exponentiation(base_number, exponent):
    x = base_number
    n = exponent
    if n == 0:
        return 1 # any integer raised to 0 return 1
    else:
        return x * exponentiation(x, n-1) # reduce the exponent of 1
                                          # multiply the exponentiation of the base_number
                                          # raised to the reduced exponent and the base_number
# run some tests
print(test_exponentiation(3, 4, 81))
print(test_exponentiation(17, 1, 17))
print(test_exponentiation(2, 0, 1))

from 2018-2019.

bluebell94 avatar bluebell94 commented on June 13, 2024

def exp_check(n,e,expected):
if exp(n,e) == expected:
return True
else:
return False

def exp(n,e):
if (e==0):
result=1
else:
result=n*exp(n,e-1)
return result

print (exp(2,3)) {8)
print(exp(93,0)) {1)

print(exp_check(3,4,81)) {True)
print(exp_check(17,1,17)) {True)
print(exp_check(2,0,1)) {True)

from 2018-2019.

lisasiurina avatar lisasiurina commented on June 13, 2024

def test_exponentiation(n,e, expected):
result = exponentiation(n,e)
if expected == result:
return True
else:
return False

def exponentiation(n, e):
if e==0:
return 1
elif e==1:
return n
else:
return n * exponentiation(n,e-1)

print(test_exponentiation(3,4,81)) Output:True
print(test_exponentiation(17,1,17)) Output:True
print(test_exponentiation(2,0,1)) Output:True

from 2018-2019.

DavideApolloni avatar DavideApolloni commented on June 13, 2024

#Test case for the algorithm

def test_expontentiation(n, exponent, expected)
result = exponentiation(n, exponent)
if expected == result
return True
else
return False

#Code of the algorithm

def exponentation(n, e):
if exponent == 0:
return 1
elif exponent == 1:
return n
else
return n ** exponentiation(n, e-1)

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

from 2018-2019.

essepuntato avatar essepuntato commented on June 13, 2024

Hi guys,

here my take on the exercise (source code available online):

# Test case for the algorithm
def test_exponentation(int_1, int_2, expected):
    result = exponentation(int_1, int_2)
    if expected == result:
        return True
    else:
        return False


# Code of the algorithm
def exponentation(int_1, int_2):
    if int_2 == 0:
        return 1
    else:
        return int_1 * exponentation(int_1, int_2 - 1)


print(test_exponentation(3, 4, 81))
print(test_exponentation(17, 1, 17))
print(test_exponentation(2, 0, 1))

Some comments:

  1. Python code and indentation: please, in your answers to the various questions, if you have to write down a Python code, be sure that the correct indent is preserved by previewing your post before to publish it. You can use the ``` environment for defining your Python code, e.g.:

```
write your Python code here
```

from 2018-2019.

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.