Re: Is this an example of tail recursion?

2015-08-05 Thread jenny
On Wednesday, August 5, 2015 at 9:52:14 AM UTC-6, Chris Angelico wrote:
> On Thu, Aug 6, 2015 at 1:13 AM,   wrote:
> > I am trying to learn differences between tail recursion and non tail 
> > recursion.
> 
> Tail recursion is where you do exactly this:
> 
> return some_function(...)
> 
> Absolutely nothing is allowed to happen around or after that function,
> and that also means you can't do that inside a try/except/finally
> block, nor a with block, etc, etc, etc. It has to be nothing more than
> a function call, and you return the exact result of that call.
> 
> > Is the following recursive code tail recursive?
> > If it is not how to convert it to tail recursion?
> > If it is how to convert it to non tail recursion?
> >
> >def __init__(self):
> >self.dpw = 0
> 
> Not tail recursive - not recursive - doesn't call anything. Trivial case. :)
> 
> >def soldiersVsDefenders(self,soldiers,defenders):
> ># soldiers win
> >if defenders <=0:
> >   return 0
> ># castle/defenders win
> >if soldiers <= 0:
> >   return self.INFINITY
> 
> In these cases, equally trivial - not recursive in any form.
> 
> ># do another round of fighting
> ># 1. Soldiers kill as many defenders
> >defendersLeft = defenders - soldiers
> ># 2. defendersLeft kill as many soldiers
> >soldiersLeft = soldiers - defendersLeft
> 
> (Interesting that the attacking soldiers get the first strike.)
> 
> >return 1 + self.soldiersVsDefenders(soldiersLeft,defendersLeft)
> 
> This is NOT tail recursion, because you add 1 at the end of it. The
> way to make it tail recursive would be to add another argument to the
> function, which it would keep adding to; whenever it returns, you
> would add the accumulator to the return value:
> 
> def soldiersVsDefenders(self,soldiers,defenders, accum=0):
> if defenders <= 0:
> return 0 + accum
> if soldiers <= 0:
> return self.INFINITY + accum
> # other code goes here
> return self.soldiersVsDefenders(soldiersLeft,defendersLeft,1+accum)
> 
> Now it's tail recursive. If this looks ugly, it's because it is; tail
> recursion often isn't worth the effort.
> 
> Note that, as far as Python's concerned, this is a tail call, but
> isn't necessarily *recursion* (which implies that you somehow know
> you're calling the same function). If someone subclasses your code and
> overrides this method, your code will call the subclass's version - if
> the subclass calls through to super(), you'll end up with mutual
> recursion, but still not a simple case of tail recursion. However, you
> could choose to ignore this possibility and manually convert this into
> iteration:
> 
> def soldiersVsDefenders(self,soldiers,defenders):
> rounds = 0
> while soldiers and defenders:
> # do another round of fighting
> # 1. Soldiers kill as many defenders
> defendersLeft = defenders - soldiers
> # 2. defendersLeft kill as many soldiers
> soldiersLeft = soldiers - defendersLeft
> rounds += 1
> if defenders <= 0:
> return rounds
> return self.INFINITY + rounds
> 
> How's that look? Better? Worse?
> 
> On to the next function.
> 
> >def oneWave(self,soldiers,defenders,castleHits):
> ># castle is dead, let soldiers play against defenders
> >if castleHits <= 0:
> >defendersLeft = defenders - self.dpw
> >return self.soldiersVsDefenders(soldiers,defendersLeft)
> 
> This is a tail call. It's not tail *recursion* because you're calling
> a completely different function, but you are indeed calling another
> function and directly returning its return value.
> 
> >mini = self.INFINITY
> >for i in range(0,soldiers):
> >if i > defenders:
> > break
> >soldiersLeft = soldiers - (defenders -i)
> >defendersLeft = defenders - i + self.dpw
> >castleHitsLeft = castleHits - (soldiers -i)
> >mini = min(mini,1 + 
> > self.oneWave(soldiersLeft,defendersLeft,castleHitsLeft))
> >return mini
> 
> Not sure what the point of all this is, but sure. This clearly isn't
> tail recursion, though, as it's doing a whole lot of other work after
> the recursive call. Having a loop and recursion like this is pretty
> scary, but in terms of "is this or isn't this tail recursive", it's
> pretty clear.
> 
> >def playGame(self,soldiers,castleHits,defendersPerWave):
> >self.dpw = defendersPerWave
> >numWaves = self.oneWave(soldiers,0,castleHits)
> >if numWaves >= self.INFINITY:
> >   return -1
> >else:
> >   return numWaves
> 
> And this is, again, no tail call. If the trap for INFINITY becoming -1
> were inside oneWave(), then this could be turned into a tail call, but
> as it is, there's more work to be done after the other function
> returns, so it's not a tail call.
> 
> Hope that h

Welcome to MultiHoster, a free image upload solution. Simply browse, select, and upload !!

2008-11-24 Thread jenny
http://www.effriend.com/index.php
--
http://mail.python.org/mailman/listinfo/python-list


Help me please urgently!

2015-03-15 Thread Jenny Hale
Hi 

How would I do this?
The teacher wants to keep track of the scores each member of the class obtains 
in the quiz. There are three classes in the school and the data should be kept 
separately for each class.

Here is my code:

import random
import operator

MATHS_OPERATIONS = [
(operator.add, "+"),
(operator.mul, "*"),
(operator.sub, "-")
]

NUM_QUESTIONS = 10

def get_int_input(prompt=''):
while True:
  try:
return int(input(prompt))
  except ValueError:
print("Not a valid input (integer is expected)")

def get_bool_input(prompt=''):
while True:
val = input(prompt).lower()
if val == 'yes':
return True
elif val == 'no':
return False
else:
print("Not a valid input (yes/no is expected)")

if __name__ == '__main__':
name = input("What is your name?").title()
class_name = input("What is your Class? ")
print(name, ", Welcome to the Maths Test")

score = 0
for _ in range(NUM_QUESTIONS):
num1 = random.randint(1,100)
num2 = random.randint(1,100)
op, symbol = random.choice(MATHS_OPERATIONS)
print("What is", num1, symbol, num2)
if get_int_input() == op(num1, num2):
print("Correct")
score += 1
else:
print("Incorrect")

print("Well done", name, "you scored", score, "/", NB_QUESTIONS)

filename = class_name + ".txt"

with open(filename, 'a') as f:
f.write(str(name) + " : " + str(score) + '\n')

if get_bool_input("Do you wish to view previous results for your class"):
with open(filename, 'r') as f:
print(f.read())
else:
input ("Press any key to exit")

Could somebody here me please?
-- 
https://mail.python.org/mailman/listinfo/python-list


Looking for python SIP/MGCP stacks

2006-12-29 Thread Jenny Zhao (zhzhao)
Hi Python users,

I am using python to write a testing tools, currently this tool only
supports skinny protocol. I am planning to add SIP and MGCP support as
well, wondering if you have written these protocol stacks before which
can be leveraged from.

 

thanks

Jenny

-- 
http://mail.python.org/mailman/listinfo/python-list

RE: Looking for python SIP/MGCP stacks

2007-01-02 Thread Jenny Zhao (zhzhao)
Thanks Anthony.

I am wondering where I can get Divmod Sine and Shtoom. Are they open
source ?

Thanks again
Jenny 

-Original Message-
From: Anthony Baxter [mailto:[EMAIL PROTECTED] 
Sent: Saturday, December 30, 2006 2:38 AM
To: Jenny Zhao (zhzhao)
Cc: python-list@python.org
Subject: Re: Looking for python SIP/MGCP stacks

> I am using python to write a testing tools, currently this tool only 
> supports skinny protocol. I am planning to add SIP and MGCP support as

> well, wondering if you have written these protocol stacks before which

> can be leveraged from.

There's two I know of - shtoom and Divmod Sine. The latter is a more
complete implementation of SIP and probably what you want to use.
-- 
http://mail.python.org/mailman/listinfo/python-list