943,789 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 594
  • Python RSS
Jun 7th, 2008
0

cs help

Expand Post »
def divide(dividend, divisor):

This function should return the calculated value of integer division of divisor by dividend. Calculate the solution by using subtraction recursively to divide the two numbers. You are not allowed to use the division operator for this problem. Explanation: note that 7 divided by 2 is 3 because you can subtract like so: 7 – 2 = 5. 5 – 2 = 3. 3 – 2 = 1. 1 – 2 = -1 (so stop.) We were able to subtract 2 from our original 7 three times before reaching a negative number.

So far i have:
def divide(dividend, divisor):
n = dividend - divisor
while n > 0:
return

i know this must be easy im just not seeing it..
Last edited by jhartley3; Jun 7th, 2008 at 5:23 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jhartley3 is offline Offline
3 posts
since Jun 2008
Jun 7th, 2008
0

Re: cs help

Add some print statements. And I've formated your snippet with code statements in the way that I think it would have to be indented, but there is no way to tell.
Python Syntax (Toggle Plain Text)
  1. def divide(dividend, divisor):
  2. n = dividend - divisor
  3. print "n =", n
  4. while n > 0:
  5. print "entering while statement"
  6. return
Last edited by woooee; Jun 7th, 2008 at 9:39 pm.
Reputation Points: 741
Solved Threads: 692
Nearly a Posting Maven
woooee is offline Offline
2,305 posts
since Dec 2006
Jun 8th, 2008
1

Re: cs help

Well.
There are two kind of problems in the snippet. Is the algorithm good (gives the right result in a reasonable time), the other is if the program implements this algorithm.

The algo would be in some pseudo language:
input: dividend, divisor nonnegativ integers
counter=0
n=dividend-divisor
while n>0
do
n=n-divisor
counter=counter+1
doend
return counter

In python that would be:
python Syntax (Toggle Plain Text)
  1. def divide(dividend, divisor):
  2. counter=0
  3. n=dividend-divisor
  4. while n>0:
  5. n-=divisor
  6. counter+=1
  7. return counter

This gives back how many times the divisor is in dividend. If you want to have the remainder, than you should return n+divisor.

If you don't make this program for learning purposes, you may have a look at the divmod built in function, which does the same thing, and more.
Last edited by slate; Jun 8th, 2008 at 10:55 am.
Reputation Points: 56
Solved Threads: 65
Posting Whiz in Training
slate is offline Offline
242 posts
since Jun 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Tree control with check boxes
Next Thread in Python Forum Timeline: calculate total price





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC