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 19, 2023 · 22 comments
Open

Lecture "Recursion", exercise 1 #24

essepuntato opened this issue Nov 19, 2023 · 22 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.

@frammenti
Copy link

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

def exponentiation(base_number, exponent):
    if exponent == 0:
        return 1
    else:
        # Recursive call that reads as x^n = x * x^(n-1) = x * x * x^(n-2) until n reaches 0:
        # x^n = x n times * x^0 = x n times * 1
        result = base_number * exponentiation(base_number, exponent-1)
    return result

print(test_exponentiation(3,4,81))
print(test_exponentiation(17,1,17))
print(test_exponentiation(2,0,1))
print(test_exponentiation(-5,2,25)) # With negatives, result is positive if exponent is even
print(test_exponentiation(-5,3,-125)) # Result is negative if exponent is odd

@Liber-R
Copy link

Liber-R commented Nov 20, 2023

Screenshot 2023-11-20 102228

@Asemica-me
Copy link

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

#Algorithm
def exponentiation(base_number, exponent):
    # Base case: when exponent is 0, result is 1
    if exponent == 0:
        return 1
    # Recursive case: multiply base_number by the result of exponentiation with a reduced exponent
    else:
        return base_number * exponentiation(base_number, exponent - 1)

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

@enricabruno
Copy link

enricabruno commented Nov 20, 2023

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

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

# three tests run
print(test_exponentiation(3, 4, 81)) # True
print(test_exponentiation(17, 1, 17)) # True
print(test_exponentiation(2, 0, 1)) # basic case

@Chiaramartina
Copy link

Schermata 2023-11-20 alle 15 11 01

@valetedd
Copy link

valetedd commented Nov 20, 2023

#Ex 1 Recursion

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:
        return 1
    elif 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))

@ThIheb
Copy link

ThIheb commented Nov 20, 2023

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

def exponentiation(base_number, exponent):
    if exponent == 0:
        return 1
    else:
        result = base_number * exponentiation(base_number, exponent-1)
        return result
print(test_exponentiation(3,4,81))
print(test_exponentiation(17,1,17))
print(test_exponentiation(2,0,1))

@katyakrsn
Copy link

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


test_1 = exponentiation(3, 4)
print(test_1)

test_2 = exponentiation(17, 1)
print(test_2)

test_3 = exponentiation(2, 0)
print(test_3)

@krzywonos
Copy link

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

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

@qwindici
Copy link

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


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


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

@Sergpoipoip
Copy link

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

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

@CarlaMenegat
Copy link

Captura de Tela 2023-11-21 às 11 22 53

@alicepiazzi
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):    #3=base number, 4=exponent
    if exponent == 0:   
        return 1 
    else:
        return base_number * exponentiation(base_number, exponent - 1)

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

@MariaFrancesca6
Copy link

def test_esp(base_number, exponent , expected):
    result = esp(base_number, exponent)
    if result == expected:
        return True
    else:
        return False

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

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

@matildepassafaro
Copy link

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

# test case for the function
def test_exponentiation(base_number, exponent, expected):
    result = exponentiation(base_number, exponent)
    if expected == result:
        return True
    else:
        return False

# test runs: use as input 3 exponent 4, 17 exponent 1 and 2 exponent 0
print(test_exponentiation(3, 4, 81))
print(test_exponentiation(17, 1, 17))
print(test_exponentiation(2, 0, 1))

@VirginiaDa00
Copy link

es 1 lez 8

@rufferbaraldi
Copy link

def test_exp_number(int_1, int_2, expected):
    result = exp_number(int_1, int_2)
    if result == expected:
        return True
    else:
        return False
        
def exp_number(int_1, int_2):
    if int_2 == 0:
        return 1
    else:
        return int_1 * exp_number(int_1, int_2 - 1)
       
print(test_exp_number(3,4,81))
print(test_exp_number(17,1,17))
print(test_exp_number(2,0,1))

@essepuntato
Copy link
Contributor Author

Thank you for all your takes. Just a few comments:

  • @agaveeoleandro @ThIheb @krzywonos, the tests must always return True or False, nothing else (even if it is clear why you decided on a different return value for the False situation. However, that could be avoided by printing on the screen the result of the execution of the algorithm in the test with its expected value;

  • @katyakrsn, you did not develop the testing function at all. Please look at the solutions from your colleagues to see what I mean, and also read the text where I introduce Python (chapter four), which includes templates for organising the tests.

@simocasaz
Copy link

simocasaz commented Nov 29, 2023

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


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

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

@saramadonia
Copy link

recursionex1

@saramadonia
Copy link

recursionex1

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