comp-think / 2024-2025

The GitHub repository containing all the material related to the Computational Thinking and Programming course of the Digital Humanities and Digital Knowledge degree at the University of Bologna (a.a. 2024/2025).
12 stars 0 forks source link

Lecture "Recursion", exercise 1 #26

Open essepuntato opened 2 days ago

essepuntato commented 2 days ago

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.

epistrephein commented 2 days ago
# Code
def exponentiation(base_number, exponent):
    if exponent == 0:
        return 1
    else:
        return base_number * exponentiation(base_number, exponent - 1)

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

# Test runs
print(test_exponentiation(3, 4, 81))   # True
print(test_exponentiation(17, 1, 17))  # True
print(test_exponentiation(2, 0, 1))    # True
KikaYang commented 2 days ago

[

Screenshot 2024-11-25 at 19 23 50

](url)

maridematteis commented 2 days ago
Screenshot 2024-11-25 alle 19 26 40
mir-pin commented 1 day ago

image

ValkyrieCain9 commented 1 day ago
def test_exponentiation(base_num, exponent, expected):
    if exponentiation(base_num, exponent) == expected:
        return True
    else:
        return False

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

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

I uploaded this from my phone as I don’t have my laptop this week so if there are any errors please let me know

theair-hub commented 1 day ago

image

shiho1000 commented 1 day ago
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)) 
#True
print(test_exponentiation(17,1,17))
#True
print(test_exponentiation(20,0,1))
#True
lisitein commented 1 day ago
def test_exponentiation(base_number, exponent, expected):
    expected = base_number ** exponent
    result = exponentiation(base_number, exponent)
    if expected == result:
        return True

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

print (test_exponentiation(3,4,81))#TRUE
print (exponentiation(3,4))
print (test_exponentiation(17,1,17))#TRUE
print (exponentiation(17,1))
print (test_exponentiation(2,0,1))#TRUE
print (exponentiation(2,0))
print (test_exponentiation(2,-1,0.5))#TRUE
print (exponentiation(2,-1))
截屏2024-11-26 12 18 54
martinamrc commented 1 day ago
# Testing the function
def test_exponentiation(base_number, exponent, expected):
    result = exponentiation(base_number, exponent)
    if expected == result:
       return True
    else:
       return False

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

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

print(exponentiation(2, 3))
print(exponentiation(2, 8))
Screenshot 2024-11-26 alle 17 41 13
arinee01 commented 23 hours ago
Снимок экрана 2024-11-26 в 19 40 43
yutong316 commented 21 hours ago

image

justyna09549 commented 21 hours ago
Zrzut ekranu 2024-11-26 o 10 06 30 PM
orboboro commented 16 hours ago
def test_exponentiation(base_number, exponent, expected):

    result=exponentiation(base_number,exponent)

    if result!=expected:

        return False

    else:
        return True

def exponentiation(base_number, exponent):

    if exponent == 0:

        if base_number == 0:

            return "indefinito"

        return 1

    if exponent < 0:

        return 1 / (base_number * exponentiation(base_number, -(exponent) - 1))

    else:

        return base_number * exponentiation(base_number, exponent - 1)

test_exponentiation(0, 0, "indefinito")
#True
test_exponentiation(2, 1, 2)
#True
test_exponentiation(2, 4 16)
#True
test_exponentiation(-2, 2, 4)
#True
test_exponentiation(2, -2, 1/4)
#True
ERendall commented 2 hours ago
image
digitalctrlv commented 1 hour ago
def test_case(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 0
    else:
        return base_number + exponentiation(base_number, exponent-1)

print(test_case(3, 4, 12))
print(test_case(17, 1, 17))
print(test_case(2, 0, 0))

True
True
True