-1

Undertaking a task to Write a function power that accepts two arguments, a and b and calculates a raised to the power b.

Example

power(2, 3) => 8

Note: Don't use

2 ** 3

and don't use

Math.pow(2, 3)

I have tried this

def power(a,b):
    return eval(((str(a)+"*")*b)[:-1])

And it works but seems to fail one test which is to return_1_when_exp_is_0

and i also get the error

Unhandled Exception: unexpected EOF while parsing (, line 0)

Please how do i solve this issue considering that i am new to python

diagold
  • 493
  • 1
  • 7
  • 28

6 Answers6

0

You can use a for loop

x=1    
for i in range(b):
    x=x*a

print(x)
Hisham Sliman
  • 322
  • 3
  • 11
  • 1
    `a=2, b=3` prints `16` not `8`... I'm sure you meant to initialize `x=1`. Setting `x=1` also solves the `0` exponent ask, `a=.b=0` will print `1`. – AChampion May 21 '17 at 02:20
0

Using eval is a terrible idea, but if you really wanted to then using join() would be a better way to create the string:

def power(a, b):
    return eval('*'.join([str(a)]*b))

>>> power(2, 3)
8

If you add ['1'] to the front then the 0 exponent behaves properly:

def power(a, b):
    return eval('*'.join(['1']+[str(a)]*b))

>>> power(2, 0)
1

However, this is simple to implement for integer exponents with a for loop:

def power(n, e):
    t = 1
    for _ in range(e):
        t *= n
    return t

>>> power(2, 3)
8
>>> power(2, 0)
1

You could also use functools.reduce() to do the same thing:

import functools as ft
import operator as op

def power(n, e):
    return ft.reduce(op.mul, [n]*e, 1)
AChampion
  • 29,683
  • 4
  • 59
  • 75
0

This worked fine

def power(a,b):
    if b == 0:
      return 1
    else:
      return eval(((str(a)+"*")*b)[:-1])
diagold
  • 493
  • 1
  • 7
  • 28
  • You should not use `eval` without explicit reason. Especially for pure math\algorithm task. Basically what you do here is just performing `b-1` multiplications. – The Godfather Oct 30 '18 at 20:20
0
def power(theNumber, thePower):
#basically, multiply the number for power times

try:
    theNumber=int(theNumber)
    thePower=int(thePower)
    if theNumber == 0:
        return 0
    elif thePower == 0:
        return 1
    else:
        return theNumber * power(theNumber,thePower-1)

except exception as err:
    return 'Only digits are allowed as input'   
Mwangi Thiga
  • 1,339
  • 18
  • 22
0

You should avoid eval by all costs, especially when it's very simple to implement pure algorithmic efficient solution. Classic efficient algorithm is Exponentiation_by_squaring. Instead of computing and multiplying numbers n times, you can always divide it to squares to archive logarithmic* complexity. For example, for calculating x^15:

x^15 = (x^7)*(x^7)*x 
x^7 = (x^3)*(x^3)*x 
x^3 = x*x*x

Thus taking 6 multiplications instead of 14.

def pow3(x, n):
    r = 1
    while n:
        if n % 2 == 1:
            r *= x
            n -= 1
        x *= x
        n /= 2
    return r

Source: https://helloacm.com/exponentiation-by-squaring/

Note: it was not mentioned in the question, but everything above considers N to be positive integer. If your question was also covering fractional or negative exponent, suggested approach will not work "as is".

* Of course depends on length of x and complexity of multiplying, see Wikipedia for detailed complexity analysis.

Also may be interesting to check out following questions: C solution or Python implementing pow() for exponentiation by squaring for very large integers

The Godfather
  • 4,235
  • 4
  • 39
  • 61
-1
def power(a, b):
  if b == 0:
    return 1
  else:
    return a ** b
Madhur Bhaiya
  • 28,155
  • 10
  • 49
  • 57
Ace
  • 1