-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

3
  • 1
    if-else: if exp is 0, return 1 else return the result of a calculation. You do not need to resort to using eval, either. Commented May 21, 2017 at 2:00
  • Are the arguments integers, or floating point? Commented May 21, 2017 at 2:01
  • @TomLynch i fill bad right now. Considered that but did it wrong thanks a lot Commented May 21, 2017 at 2:13

6 Answers 6

0

This worked fine

def power(a,b): if b == 0: return 1 else: return eval(((str(a)+"*")*b)[:-1]) 
Sign up to request clarification or add additional context in comments.

1 Comment

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

Comments

0

You can use a for loop

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

1 Comment

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=<anything>.b=0 will print 1.
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' 

Comments

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

Comments

-1
def power(a, b): if b == 0: return 1 else: return a ** b 

2 Comments

Code only answers are not considered good. Please add some explanation to it.
This does not answer the question, because it was explicitly mentioned in the text "do not use **"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.