[Tutor] Calculate 4**9 without using **
Sri Kavi
gvmcmt at gmail.com
Mon Mar 6 02:52:50 EST 2017
I realize how complicated I made it!
If exponent is negative, this version takes abs(exponent) and iteratively
divides result by base.
def power(base, exponent):
""" Returns base**exponent. """
if exponent == 0:
return 1
elif exponent < 0:
result = 1
for _ in range(abs(exponent)):
result /= base
return result
else:
result = 1
for _ in range(exponent):
result *= base
return result
If exponent is negative, this version resets base to its reciprocal and
exponent to abs(exponent).
def power(base, exponent):
""" Returns base**exponent. """
if exponent == 0:
return 1
elif exponent < 0:
base = 1 / base
exponent = abs(exponent)
result = 1
for _ in range(exponent):
result *= base
return result
else:
result = 1
for _ in range(exponent):
result *= base
return result
This version deals with both negative and non-negative exponents in a
single loop. I like this.
def power(base, exponent):
""" Returns base**exponent. """
if exponent == 0:
return 1
else:
result = 1
for _ in range(abs(exponent)):
result *= base
if exponent < 0:
return 1 / result
else:
return result
I'm learning a lot. Thank you for being so helpful.
On Sun, Mar 5, 2017 at 11:33 PM, Alex Kleider <akleider at sonic.net> wrote:
> On 2017-03-05 02:24, Peter Otten wrote:
>
>> Sri Kavi wrote:
>>
>> Like I just said in my other message, I was trying to reply to Tasha
>>> Burman, but I was in digest mode and I didn't know how to change my
>>> subscription options in order to reply to individual messages. I also
>>> don't know if it's an assignment, but I'm a beginner learning to program,
>>> too :)
>>>
>>> What if any of the following are true, and what should be done in each
>>>>
>>> case?
>>>
>>>> if exponent ==1: .....
>>>>
>>>
> New discovery:
> This (if exponent ==1) 'special case' ceases to be special if result is
> set to 1 rather than to base. It also simplifies the parameters to the
> range function: range(exponent) rather than range(1, exponent).
>
> if exponent = 0: .....
>>>> if exponent < 0: .....
>>>>
>>>
> If the exponent is negative, one need only reset the base to its
> reciprocal and the the exponent to its absolute value after which the same
> algorithm does the job.
> Alternatively you could simply change the exponent to its absolute value
> and set a flag and at the end change res to its reciprocal if your flag is
> set- again avoiding having two separate loops.
>
>
>>> Here's my implementation.
>>>
>>
>> In Python 3.6.0
>>>
>>> def power(base, exponent):
>>> if exponent == 0:
>>> return 1
>>> elif exponent > 0:
>>> result = base
>>>
>>> for _ in range(1, exponent):
>>> result *= base
>>>
>>> return result
>>> else:
>>> exponent = -exponent
>>> result = base
>>>
>>> for _ in range(1, exponent):
>>> result *= base
>>> return 1 / result
>>>
>>> Please share your thoughts.
>>>
>>
>> You can try to simplify that a bit:
>>
>> - Handle the case with non-negative exponents in one branch
>> - Avoid duplicating the for loop for negative exponents, e. g. by calling
>> power() from within power()
>>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list