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 #26

Open
essepuntato opened this issue Nov 15, 2021 · 28 comments
Open

Lecture "Recursion", exercise 1 #26

essepuntato opened this issue Nov 15, 2021 · 28 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.

@11051620
Copy link

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

@martasoricetti
Copy link

image

@ManueleVeggi
Copy link

ManueleVeggi commented Nov 15, 2021

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

The output is:

True
True
True

@Postitisnt
Copy link


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

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

test_1 = test_exp(3, 4, 81)
test_2 = test_exp(17, 1, 17)
test_3 = test_exp(2, 0, 1)
print(test_1)
print(test_2)
print(test_3)

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

@tommasobattisti
Copy link

def test_exp(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_exp(3, 4, 81))
print(test_exp(17, 1, 17))
print(test_exp(2, 0, 1))

@federicabonifazi
Copy link

def my_test(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 (my_test(3, 4, 81))
print (my_test(17, 1, 17))
print (my_test(2, 0, 1))

@CarmenSantaniello
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))
print(exponentiation(3, 4))
print(exponentiation(17, 1))
print(exponentiation(2, 0))

It returns:

True
True
True
81
17
1

@OrsolaMBorrini
Copy link

OrsolaMBorrini commented Nov 15, 2021

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

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

print(test_exponentation(3,4,81)) #It returns True
print(test_exponentation(17,1,17)) #It returns True
print(test_exponentation(2,0,1)) #It returns True

@AmeliaLamargese
Copy link

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

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

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

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

@SaraVell1
Copy link

image

@RebeccaJillianBeattie
Copy link

recursive exponentiation function

@ghasempouri1984
Copy link

ghasempouri1984 commented Nov 16, 2021

This function can return a proper result for negative integer exponents and also proper error message return, for non-integer exponents and also a special case of zero power zero and zero power negative exponent. This function rounds the result of a negative integer exponent with 4 digits.

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


# Code of function
def exponentation(base_number, exponent):
    if base_number == 0:
        if exponent <= 0:
            return "undefined"
        else:
            return 0
    elif type(exponent) != int:
        return "this function can not calculate non-integer exponent"

    elif exponent == 0:
        return 1

    elif exponent < 0:

        return round(1/(base_number * exponentation(base_number, abs(exponent) - 1)), 4)

    else:
        return base_number * exponentation(base_number, exponent - 1)


# Tests
print(test_exponentation(0, 0, "indeterminate form"))
print(test_exponentation(0, -5, "indeterminate form"))
print(test_exponentation(2, 2.1, "this function can not calculate non-integer exponent"))
print(test_exponentation(0, 1.5, 0))
print(test_exponentation(2, 0, 1))
print(test_exponentation(0, 10, 0))
print(test_exponentation(3, 4, 81))
print(test_exponentation(17, 1, 17))
print(test_exponentation(-4, 0, 1))
print(test_exponentation(-2, -3, -0.125))
print(test_exponentation(-3.11, -2, 0.1034))

@olgagolgan
Copy link

image
image

@MaddaGh
Copy link

MaddaGh commented Nov 18, 2021

ex recursion_exponent

@Bianca-LM
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
    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))

@NoraPs
Copy link

NoraPs commented Nov 19, 2021

Cattura

@katya-avem
Copy link

image

@AnastasiyaSopyryaeva
Copy link

input

#Defining test function

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

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

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

output
True
True
True

@francescabudel
Copy link

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

print(exponentation(171, 6)) 
print(exponentation(34, 12))
print(exponentation(20, 3))

25002110044521
2386420683693101056
8000

@angstigone
Copy link

Schermata 2021-11-21 alle 18 08 12

Schermata 2021-11-21 alle 18 13 56

@chloeppd
Copy link

def test_exponentation(number1, number2, expected):
    result= exponentation(number1,number2)
    if expected == result:
        return True
    else:
        return False


def exponentation(number1, number2):
    if number2==0:
        return 1
    
    else:
        return number1*exponentation(number1, number2-1)


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

@sanyuezoe
Copy link

sanyuezoe commented Nov 22, 2021

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

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

Output

True
True
True

@essepuntato
Copy link
Contributor Author

Hi all, just a couple of notes:

  • @11051620, please format your code appropriately otherwise it is difficult to understand if you did the implementation correctly or not
  • @sanyuezoe, you should develop the function as a recursive function

@teragramgius
Copy link

Exercise1
Exercise1_

@ahsanv101
Copy link

ahsanv101 commented Dec 5, 2021

image

result:
True
True
True

@ManuSrivastava1
Copy link

def test_exponentiation(base,power):
    x = base**power
    y = exponentiation(base,power)
    if x ==y: 
        return True
    else:
        return False


def exponentiation(base,power):
    if power==0:
        return 1
    else:
        result=base*exponentiation(base,power-1)
    return result

y = test_exponentiation(3,4)
print(y)
y = test_exponentiation(17,1)
print(y)
y = test_exponentiation(2,0)
print(y)

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