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 "Recursion", exercise 1 #24

Open
essepuntato opened this issue Nov 21, 2022 · 15 comments
Open

Lecture "Recursion", exercise 1 #24

essepuntato opened this issue Nov 21, 2022 · 15 comments
Labels

Comments

@essepuntato
Copy link
Contributor

Define a recursive function def exponentiation(base_number, exponent) for implementing the exponentiation operation. Test (by implementing the related test case) it on the following inputs: 34, 171, and 20.

@delete4ever
Copy link

def test_exponentiation (int_1, int_2, expected):
    result=exponentiation(int_1, int_2)
    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 runs
print(test_exponentiation(3, 4, 81))
print(test_exponentiation(17, 1, 17))
print(test_exponentiation(2, 0 ,1))
# Console output
True
True
True

@lucia1299
Copy link

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

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



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

#Console output: 
True 
True 
True

@giorgiacrosilla
Copy link

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))

@ranacoskun
Copy link

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

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

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

@ChiaraParravicini
Copy link

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 (exponentiation(base_number, (exponent - 1)) * base_number)

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

@corrado877
Copy link

corrado877 commented Nov 22, 2022

# Define Test Function
def test_exponentiation (based_number, exponent, expected):
    result=exponentiation (based_number, exponent)
    if result==expected:
        return True
    else:
        return False

# Code of Algorithm
def exponentiation (based_number, exponent):
    if exponent==0:
        return 1
    elif exponent==1:
        return based_number
    else:
         return based_number * exponentiation(based_number, exponent-1)

# Three test runs 
print (test_exponentiation(3, 4, 81))
print (test_exponentiation(17, 1, 17))
print (test_exponentiation(2, 0, 1))

@lvcasmariani
Copy link

#Define Test Function
def test_exponentiation (int_1, int_2, expected):
    result = exponentiation (int_1, int_2)
    if expected == result:
        return True
    else:
        return False

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

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

#Output
True
True
True

@EricaAndreose
Copy link

# Test of the function

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

# Implementation of the function

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)

# Test runs that returns True

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

@NicoleLiggeri
Copy link

def test_exponentiation(base_number, exponent, expected):
    result = exponentiation(base_number, exponent)
    if result == expected:
        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))

@n1kg0r
Copy link

n1kg0r commented Nov 22, 2022

def test_exponentiation(base_number, exponent, expected):
    return expected == exponentiation(base_number, exponent)

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)) # True 
print(test_exponentiation(17, 1, 17)) # True 
print(test_exponentiation(2, 0 ,1)) # True

@matteo-guenci
Copy link

matteo-guenci commented Nov 23, 2022

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


def exponentiation(base_number, exponent):
    if exponent == 0:
        return 1
    if exponent == 1:
        return base_number
    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))

@irematmar
Copy link

def test_expo(num1, num2, expected):
    result = exponentiation(num1, num2)
    if result == expected:
        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_expo(3, 4, 81))    
print(test_expo(17, 1, 17))
print(test_expo(2, 0, 1))


True
True
True

@eugeniavd
Copy link

eugeniavd commented Nov 23, 2022

image

@alka2696
Copy link

# Test case for the algorithm
def test_exponentiation (int_1, int_2, expected):
    result = exponentiation(int_1, int_2) 
    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(test_exponentiation(3, 4, 81)) #True
print(test_exponentiation(17, 1, 17)) #True
print(test_exponentiation(2, 0, 1)) #True

@SalvatoreDiMarzo
Copy link

def test_exponentiation(bn, ex, expected):
    result = exponentiation(bn, ex)
    if expected == result:
        return True
    else: 
        return False
    

def exponentiation(bn, ex):
    if ex == 0:
        return 1
    elif ex == 1:
        return bn
    else: 
        return bn * exponentiation(bn, ex - 1)
    

print(test_exponentiation(7, 2, 49))
print(test_exponentiation(4, 4, 256))
print(test_exponentiation(2, 8, 213))

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