Re: Recursion on list

2021-11-04 Thread Peter Pearson
On Thu, 4 Nov 2021 08:57:14 +0100, ast wrote: > > li = [] > > li.append(li) > > li > [[...]] > > >li[0][0][0][0] > [[...]] > > That's funny After the coming AI upheaval, such cruelty to machines will be considered punishable and not funny. -- To email me, substitute nowhere->runbox, invalid->c

Re: Recursion on list

2021-11-04 Thread Pieter van Oostrum
ast writes: >> li = [] >> li.append(li) >> li > [[...]] > >>li[0][0][0][0] > [[...]] > > That's funny > You made a list whose only element is itself. In [1]: li = [] In [3]: li.append(li) In [4]: li[0] is li Out[4]: True -- Pieter van Oostrum www: http://pieter.vanoostrum.org/ PGP key: [8D

Re: Recursion error in metaclass

2011-06-11 Thread Daniel Urban
On Sat, Jun 11, 2011 at 21:39, Terry Reedy wrote: > What may not be obvious from the docs is that the metaclass calculation > described in the doc section on class statements is carried out within > type.__new__ (or after a possible patch, called from within that), so that > type calls are really

Re: Recursion error in metaclass

2011-06-11 Thread Terry Reedy
On 6/11/2011 7:38 AM, Steven D'Aprano wrote: On Sat, 11 Jun 2011 01:33:25 -0400, Terry Reedy wrote: On 6/10/2011 11:34 PM, Steven D'Aprano wrote: I have a metaclass in Python 3.1: class MC1(type): @staticmethod def get_mro(bases): print('get_mro called') return

Re: Recursion error in metaclass

2011-06-11 Thread Steven D'Aprano
On Sat, 11 Jun 2011 01:33:25 -0400, Terry Reedy wrote: > On 6/10/2011 11:34 PM, Steven D'Aprano wrote: >> I have a metaclass in Python 3.1: >> >> class MC1(type): >> @staticmethod >> def get_mro(bases): >> print('get_mro called') >> return type('K', bases, {}).__mro__[1

Re: Recursion error in metaclass

2011-06-10 Thread Terry Reedy
On 6/10/2011 11:34 PM, Steven D'Aprano wrote: I have a metaclass in Python 3.1: class MC1(type): @staticmethod def get_mro(bases): print('get_mro called') return type('K', bases, {}).__mro__[1:] The call to type figures out the proper metaclass from bases and forwa

Re: Recursion in Computer Science

2011-05-19 Thread rusi
On May 20, 10:18 am, Chris Angelico wrote: > On Fri, May 20, 2011 at 3:05 PM, rusi wrote: > >  - data can be code -- viruses > > It's not JUST viruses. There's plenty of legitimate reasons for your > data to actually be code... that's how compilers work! :) > > Chris Angelico Yes sure Thanks. An

Re: Recursion in Computer Science

2011-05-19 Thread Chris Angelico
On Fri, May 20, 2011 at 3:05 PM, rusi wrote: >  - data can be code -- viruses It's not JUST viruses. There's plenty of legitimate reasons for your data to actually be code... that's how compilers work! :) Chris Angelico -- http://mail.python.org/mailman/listinfo/python-list

Re: Recursion or iteration (was Fibonacci series recursion error)

2011-05-15 Thread Mark Dickinson
On May 15, 8:20 pm, Mark Dickinson wrote: > On May 15, 4:32 am, rusi wrote: > > > On May 15, 2:19 am, Ian Kelly wrote: > > > Yup, linear.  Assuming you optimize the even case so that it doesn't > > > actually call fib(n//2) twice, the call tree can be approximated as a > > > balanced binary tree

Re: Recursion or iteration (was Fibonacci series recursion error)

2011-05-15 Thread Mark Dickinson
On May 15, 4:32 am, rusi wrote: > On May 15, 2:19 am, Ian Kelly wrote: > > Yup, linear.  Assuming you optimize the even case so that it doesn't > > actually call fib(n//2) twice, the call tree can be approximated as a > > balanced binary tree with height log(n).  The total number of nodes in > >

Re: Recursion or iteration (was Fibonacci series recursion error)

2011-05-14 Thread rusi
On May 15, 2:19 am, Ian Kelly wrote: > On Sat, May 14, 2011 at 11:24 AM, rusi wrote: > > def fib(n): > >    if n==1 or n==2: > >        return 1 > >    elif even(n): > >        return sq(fib (n//2)) + 2 * fib(n//2) * fib(n//2 - 1) > >    else: > >        return sq(fib (n//2 + 1)) + sq(fib(n // 2)

Re: Recursion or iteration (was Fibonacci series recursion error)

2011-05-14 Thread Ian Kelly
On Sat, May 14, 2011 at 11:24 AM, rusi wrote: > def fib(n): >    if n==1 or n==2: >        return 1 >    elif even(n): >        return sq(fib (n//2)) + 2 * fib(n//2) * fib(n//2 - 1) >    else: >        return sq(fib (n//2 + 1)) + sq(fib(n // 2)) > > This is a strange algo  -- logarithmic because i

Re: Recursion or iteration (was Fibonacci series recursion error)

2011-05-14 Thread rusi
On May 14, 2:48 am, Mark Dickinson wrote: > I don't see this (or Hans' version) as cheating at all. Yeah sure -- cheating is a strong word :-) > This really *is* the power algorithm, just in a different number system from > the > usual one. Yes that was my point. If we take the standard log

Re: Recursion or iteration (was Fibonacci series recursion error)

2011-05-13 Thread Mark Dickinson
On May 11, 11:06 pm, Hans Mulder wrote: > On 03/05/2011 09:52, rusi wrote: > > > [If you believe it is, then try writing a log(n) fib iteratively :D ] > > It took me a while, but this one seems to work: > > from collections import namedtuple > > Triple = namedtuple('Triple', 'hi mid lo') > Triple.

Re: Recursion or iteration (was Fibonacci series recursion error)

2011-05-13 Thread Hans Mulder
On 13/05/2011 13:11, rusi wrote: On May 12, 3:06 am, Hans Mulder wrote: On 03/05/2011 09:52, rusi wrote: [If you believe it is, then try writing a log(n) fib iteratively :D ] It took me a while, but this one seems to work: from collections import namedtuple Triple = namedtuple('Triple', '

Re: Recursion or iteration (was Fibonacci series recursion error)

2011-05-13 Thread Ian Kelly
On Fri, May 13, 2011 at 5:11 AM, rusi wrote: > The tightest way I knew so far was this: > The 2x2 matrix > 0 1 > 1 1 > raised to the nth power gives the nth fibonacci number. [And then use > a logarithmic matrix mult] > Your version is probably tighter than this. Oh, nice! I did it this way once

Re: Recursion or iteration (was Fibonacci series recursion error)

2011-05-13 Thread rusi
On May 12, 3:06 am, Hans Mulder wrote: > On 03/05/2011 09:52, rusi wrote: > > > [If you believe it is, then try writing a log(n) fib iteratively :D ] > > It took me a while, but this one seems to work: > > from collections import namedtuple > > Triple = namedtuple('Triple', 'hi mid lo') > Triple._

Re: Recursion or iteration (was Fibonacci series recursion error)

2011-05-11 Thread Hans Mulder
On 03/05/2011 09:52, rusi wrote: [If you believe it is, then try writing a log(n) fib iteratively :D ] It took me a while, but this one seems to work: from collections import namedtuple Triple = namedtuple('Triple', 'hi mid lo') Triple.__mul__ = lambda self, other: Triple( self.hi * othe

Re: Recursion or iteration (was Fibonacci series recursion error)

2011-05-03 Thread Dan Stromberg
On Tue, May 3, 2011 at 5:49 AM, Steven D'Aprano < [email protected]> wrote: > On Tue, 03 May 2011 21:04:07 +1000, Chris Angelico wrote: > > > And that, Your Honour, is why I prefer bignums (especially for integers) > > to floating point. Precision rather than performance. > > I'

Re: Recursion or iteration (was Fibonacci series recursion error)

2011-05-03 Thread Chris Angelico
On Tue, May 3, 2011 at 10:49 PM, Steven D'Aprano wrote: > On Tue, 03 May 2011 21:04:07 +1000, Chris Angelico wrote: > >> And that, Your Honour, is why I prefer bignums (especially for integers) >> to floating point. Precision rather than performance. > > I'm intrigued by your comment "especially f

Re: Recursion or iteration (was Fibonacci series recursion error)

2011-05-03 Thread Steven D'Aprano
On Tue, 03 May 2011 21:04:07 +1000, Chris Angelico wrote: > And that, Your Honour, is why I prefer bignums (especially for integers) > to floating point. Precision rather than performance. I'm intrigued by your comment "especially for integers", which implies that you might use bignums for non-i

Re: Recursion or iteration (was Fibonacci series recursion error)

2011-05-03 Thread Chris Angelico
On Tue, May 3, 2011 at 8:32 PM, Dave Angel wrote: > What I'm surprised at is that nobody has pointed out that the logn version > is also generally more accurate, given traditional floats. Usually getting > the answer accurate (given the constraints of finite precision > intermediates) is more impo

Re: Recursion or iteration (was Fibonacci series recursion error)

2011-05-03 Thread rusi
On May 3, 3:32 pm, Dave Angel wrote: > What I'm surprised at is that nobody has pointed out that the logn > version is also generally more accurate, given traditional floats. > Usually getting the answer accurate (given the constraints of finite > precision intermediates) is more important than p

Re: Recursion or iteration (was Fibonacci series recursion error)

2011-05-03 Thread Dave Angel
On 01/-10/-28163 02:59 PM, rusi wrote: On May 3, 10:29 am, Chris Angelico wrote: On Tue, May 3, 2011 at 3:27 PM, Dan Stromberg wrote: Doh. Usually when someone gives a recursive solution to this problem, it's O(logn), but not this time. Here's a logn one: :-) Ok so you beat me to it :D

Re: Recursion or iteration (was Fibonacci series recursion error)

2011-05-03 Thread rusi
On May 3, 10:29 am, Chris Angelico wrote: > On Tue, May 3, 2011 at 3:27 PM, Dan Stromberg wrote: > Doh. > Usually when someone gives a recursive solution to this problem, it's > O(logn), but not this time. > Here's a logn one: :-) Ok so you beat me to it :D I was trying to arrive at an answer

Re: Recursion or iteration (was Fibonacci series recursion error)

2011-05-02 Thread Dan Stromberg
On Mon, May 2, 2011 at 10:29 PM, Chris Angelico wrote: > On Tue, May 3, 2011 at 3:27 PM, Dan Stromberg wrote: > > > > But the recursive solution has time complexity of O(logn). The iterative > > solution has time complexity of O(n). That's a significant difference > for > > large n - a signifi

Re: Recursion or iteration (was Fibonacci series recursion error)

2011-05-02 Thread Ian Kelly
On Mon, May 2, 2011 at 11:27 PM, Dan Stromberg wrote: > But the recursive solution has time complexity of O(logn).  The iterative > solution has time complexity of O(n).  That's a significant difference for > large n - a significant benefit of the recursive version. It's linear as written. I th

Re: Recursion or iteration (was Fibonacci series recursion error)

2011-05-02 Thread Chris Angelico
On Tue, May 3, 2011 at 3:27 PM, Dan Stromberg wrote: > > But the recursive solution has time complexity of O(logn).  The iterative > solution has time complexity of O(n).  That's a significant difference for > large n - a significant benefit of the recursive version. Are you sure? It will produce

Re: Recursion or iteration (was Fibonacci series recursion error)

2011-05-02 Thread Dan Stromberg
On Mon, May 2, 2011 at 7:13 PM, Chris Angelico wrote: > On Tue, May 3, 2011 at 11:48 AM, rusi wrote: > > What are their space/time complexities? > > Which do you prefer? > > They're pretty similar actually. If you rework the first one to not > use range() but instead have a more classic C style

Re: Recursion or iteration (was Fibonacci series recursion error)

2011-05-02 Thread Chris Angelico
On Tue, May 3, 2011 at 11:48 AM, rusi wrote: > What are their space/time complexities? > Which do you prefer? They're pretty similar actually. If you rework the first one to not use range() but instead have a more classic C style of loop, they'll be almost identical: def powI(x,n): result = 1

Re: Recursion head scratcher

2009-12-02 Thread Joel Madigan
On 12/2/09, Dave Angel wrote: > Joel Madigan wrote: >> Hi everyone! >> Sorry this isn't strictly a Python question but my algorithms professor >> contends that given the standard recursive-backtracking maze solving >> algorithm: >> >> width=6 >> height=4 >> maze=[[1,0,1,1,0,1], >> [0,0,1,0,0

Re: Recursion head scratcher

2009-12-02 Thread Tim Wintle
On Wed, 2009-12-02 at 02:07 -0500, Joel Madigan wrote: > > that it is possible to make it print the path to the finish in the > order the steps were taken. That is, the algorithm as written > produces: (4,0) (4,1) (3,1) (3,2) (3,3) (2,3) (1,3) (1,2) True > > Rather than (1,2) (1,3) (2,3) (3,3)

Re: Recursion head scratcher

2009-12-02 Thread Dave Angel
Joel Madigan wrote: Hi everyone! Sorry this isn't strictly a Python question but my algorithms professor contends that given the standard recursive-backtracking maze solving algorithm: width=6 height=4 maze=[[1,0,1,1,0,1], [0,0,1,0,0,0], [1,0,1,0,1,0], [0,0,0,0,1,1]] visited =

Re: Recursion, generate all pyramid-paths, not working

2008-10-04 Thread Terry Reedy
process wrote: http://projecteuler.net/index.php?section=problems&id=18 def recur(tree, pos): if not tree: return [] else: return [[tree[0][pos]] + recur(tree[1:], pos)] + \ [[tree[0][pos]] + recur(tree[1:], pos+1)] The backslash is not needed here or an

Re: recursion gotcha?

2008-09-14 Thread Boris Borcic
cnb wrote: this recursive definition of sum thrumped me, is this some sort of gotcha or am I just braindead today? and yes i know this is easy a a for x in xs acc += x or just using the builtin. def suma(xs, acc=0): if len(xs) == 0: acc else: suma(

Re: recursion gotcha?

2008-09-14 Thread Arnaud Delobelle
On Sep 14, 9:44 am, "Marco Bizzarri" <[EMAIL PROTECTED]> wrote: > On Sun, Sep 14, 2008 at 10:08 AM, Marco Bizzarri > > > > <[EMAIL PROTECTED]> wrote: > > On Sun, Sep 14, 2008 at 10:01 AM, cnb <[EMAIL PROTECTED]> wrote: > >> this recursive definition of sum thrumped me, is this some sort of > >> got

Re: recursion gotcha?

2008-09-14 Thread Marco Bizzarri
On Sun, Sep 14, 2008 at 10:08 AM, Marco Bizzarri <[EMAIL PROTECTED]> wrote: > On Sun, Sep 14, 2008 at 10:01 AM, cnb <[EMAIL PROTECTED]> wrote: >> this recursive definition of sum thrumped me, is this some sort of >> gotcha or am I just braindead today? >> and yes i know this is easy a a for x in xs

Re: recursion gotcha?

2008-09-14 Thread rs387
On Sep 14, 9:01 am, cnb <[EMAIL PROTECTED]> wrote: > def suma(xs, acc=0): >         if len(xs) == 0: >                 acc >         else: >                 suma(xs[1:], acc+xs[0]) > > it returns none. Yep, that's because there is no "return" statement anywhere. Python doesn't return expressions "

Re: recursion gotcha?

2008-09-14 Thread Marco Bizzarri
On Sun, Sep 14, 2008 at 10:01 AM, cnb <[EMAIL PROTECTED]> wrote: > this recursive definition of sum thrumped me, is this some sort of > gotcha or am I just braindead today? > and yes i know this is easy a a for x in xs acc += x or just using the > builtin. > > def suma(xs, acc=0): >if len(x

Re: Recursion Performance Question

2008-07-24 Thread Anders J. Munch
B wrote: > > # pass in window handle and parent node > def gwl(node, hwnd): > if hwnd: > yield node, hwnd > for nd, wnd in Wnd.gwl(node.children[-1], GetWindow(hwnd, > GW_CHILD)): > yield nd, wnd > for nd, wnd in Wnd.gwl(node, GetWind

Re: Recursion Performance Question

2008-07-24 Thread Tim Golden
B wrote: Now it works, but it runs quite slow (compared to the c++ app). I changed gwl from strait recursion to use a generator and that helped, but it still takes 0.5-1.0 seconds to populate the tree. What I'm wondering is am I doing it in a really inefficient way, or is it just python? W

Re: recursion in Class-methods?

2008-06-27 Thread Bruno Desthuilliers
defn noob a écrit : class Graph(object): where does anyone write like that? Almost everywhere nowadays. I've seen only examples like i have written. Most of the doc has still not been updated since the introduction of newstyle classes years ago. You'll find more here: http://docs.python.

Re: recursion in Class-methods?

2008-06-26 Thread Saul Spatz
defn noob wrote: if start == end: return path if not self.dictionary.has_key(start): if start not in self.dictionnary: return None for node in self.dictionary[start]: if node not in path: newpath = find_path

Re: recursion in Class-methods?

2008-06-26 Thread defn noob
> > > if start == end: > > return path > > if not self.dictionary.has_key(start): > >if start not in self.dictionnary: > > > return None > > for node in self.dictionary[start]: > > if node not in path: > > newpa

Re: recursion in Class-methods?

2008-06-26 Thread defn noob
class Graph(object): where does anyone write like that? I've seen only examples like i have written. is the object then passed to init? class Graph(object): def __init__(self, dictionary): self.structure = dictionary or class Graph(object): def __init__(self, object):

Re: recursion in Class-methods?

2008-06-26 Thread Bruno Desthuilliers
klant a écrit : do i need to call Graph.find_path? > g = Graph({'A': ['B', 'C'], 'B': ['C', 'D'], 'C': ['D'], 'D': ['C'], 'E': ['F'], 'F': ['C']}) g <__main__.Graph instance at 0x01D74378> g.find_all_paths('A', 'C') Traceb

Re: recursion in Class-methods?

2008-06-25 Thread Roopesh
Wrong: newpath = find_path(self.dictionary, node, end, path) newpaths = find_all_paths(self.dictionary, node, end, path) newpath = find_shortest_path(self.dictionary, node, end, path) Correct; newpath = self.find_path(self.dictionary, node, end, path) newpaths = self.find_all_paths(self.dictionary

Re: recursion with or without return?

2008-05-25 Thread Marc 'BlackJack' Rintsch
On Sun, 25 May 2008 00:00:14 -0700, notnorwegian wrote: > when using recursion should one use a return statement or not? This decision has nothing to do with recursion. It's the same as in non recursive functions. If the function calculates something that you want to return to the caller you ha

Re: Recursion limit of pickle?

2008-02-10 Thread Gabriel Genellina
En Sun, 10 Feb 2008 02:09:12 -0200, Victor Lin <[EMAIL PROTECTED]> escribió: > On 2月10日, 上午11時42分, "Gabriel Genellina" <[EMAIL PROTECTED]> > wrote: >> En Sat, 09 Feb 2008 09:49:46 -0200, Victor Lin <[EMAIL PROTECTED]> >> escribió: >> >> > I encounter a problem with pickle. >> > I download a html

Re: Recursion limit of pickle?

2008-02-09 Thread Victor Lin
On 2月10日, 上午11時42分, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote: > En Sat, 09 Feb 2008 09:49:46 -0200, Victor Lin <[EMAIL PROTECTED]> > escribi�: > > > I encounter a problem with pickle. > > I download a html from: > > >http://www.amazon.com/Magellan-Maestro-4040-Widescreen-Navigator/dp/B... > >

Re: Recursion limit of pickle?

2008-02-09 Thread Gabriel Genellina
En Sat, 09 Feb 2008 09:49:46 -0200, Victor Lin <[EMAIL PROTECTED]> escribi�: > I encounter a problem with pickle. > I download a html from: > > http://www.amazon.com/Magellan-Maestro-4040-Widescreen-Navigator/dp/B000NMKHW6/ref=sr_1_2?ie=UTF8&s=electronics&qid=1202541889&sr=1-2 > > and parse it w

Re: recursion

2007-09-14 Thread John Machin
On Sep 15, 3:06 am, "Terry Reedy" <[EMAIL PROTECTED]> wrote: > > There have been languages, for instance, Fortran IV, where local variables > were part of the function 'object' and which therefore prohibited recursion > because of the very problem you alluded to in your question. (My guess is > th

Re: recursion

2007-09-14 Thread Terry Reedy
"Marc 'BlackJack' Rintsch" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] |f([1, 2, 3]) | r1 f([2, 3]) + [1] | r2 f([3]) + [2] + [1] | r3 f([]) + [3] + [2] + [1] | r4 [] + [3] + [2] + [1] I might help to note that the above is effectively parenthesized ( ( ([]+{3]) + [2]) +

Re: recursion

2007-09-14 Thread Steve Holden
Gigs_ wrote: > Steve Holden wrote: [...] >> >> regards >> Steve > >>> def factorial(n): > print "n =", n > if n==0: > return 1 > else: > return n * factorial(n-1) > > >>> factorial(3) > n = 3 > n = 2 > n = 1 > n = 0 > 6 > > > now i understand. but one question at t

Re: recursion

2007-09-14 Thread Marc 'BlackJack' Rintsch
On Fri, 14 Sep 2007 15:58:39 +0200, Gigs_ wrote: > >>> def factorial(n): > print "n =", n > if n==0: > return 1 > else: > return n * factorial(n-1) > > >>> factorial(3) > n = 3 > n = 2 > n = 1 > n = 0 > 6 > > > now i understand. but one question at the end this fun

Re: recursion

2007-09-14 Thread Neil Cerutti
On 2007-09-14, John Machin <[EMAIL PROTECTED]> wrote: > On Sep 14, 10:04 pm, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote: >> On Fri, 14 Sep 2007 13:40:17 +0200, Gigs_ wrote: >> > sorry i think that i express wrong. having problem with english >> >> > what i mean is how python knows to add al

Re: recursion

2007-09-14 Thread J. Clifford Dyer
On Fri, Sep 14, 2007 at 01:40:17PM +0200, Gigs_ wrote regarding Re: recursion: > > what i mean is how python knows to add all thing at the end of recursion > > >>> def f(l): > if l == []: > return [] > else: > return f(l[1:]) + l[

Re: recursion

2007-09-14 Thread Gigs_
Steve Holden wrote: > Gigs_ wrote: >> sorry i think that i express wrong. having problem with english >> >> >> what i mean is how python knows to add all thing at the end of recursion >> >> >>> def f(l): >> if l == []: >> return [] >> else: >> return f(l[1:]) + l[:1] >>

Re: recursion

2007-09-14 Thread John Machin
On Sep 14, 10:04 pm, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote: > On Fri, 14 Sep 2007 13:40:17 +0200, Gigs_ wrote: > > sorry i think that i express wrong. having problem with english > > > what i mean is how python knows to add all thing at the end of recursion > > Because you have written

Re: recursion

2007-09-14 Thread Steve Holden
Gigs_ wrote: > sorry i think that i express wrong. having problem with english > > > what i mean is how python knows to add all thing at the end of recursion > > >>> def f(l): > if l == []: > return [] > else: > return f(l[1:]) + l[:1] > > > f([1,2,3]) > > recursi

Re: recursion

2007-09-14 Thread Marc 'BlackJack' Rintsch
On Fri, 14 Sep 2007 13:40:17 +0200, Gigs_ wrote: > sorry i think that i express wrong. having problem with english > > > what i mean is how python knows to add all thing at the end of recursion Because you have written code that tells Python to do so. ;-) > >>> def f(l): > if l == []: >

Re: recursion

2007-09-14 Thread Gigs_
sorry i think that i express wrong. having problem with english what i mean is how python knows to add all thing at the end of recursion >>> def f(l): if l == []: return [] else: return f(l[1:]) + l[:1] f([1,2,3]) recursion1 f([2,3]) + [1] recursion2 f([3]) +

Re: recursion

2007-09-13 Thread Neil Cerutti
On 2007-09-13, Ian Clark <[EMAIL PROTECTED]> wrote: > Neil Cerutti wrote: >> On 2007-09-13, Gigs_ <[EMAIL PROTECTED]> wrote: >>> Can someone explain me this >>> >> def f(l): >>> if l == []: >>> return [] >>> else: >>> return f(l[1:]) + l[:1] # <= cant figure thi

Re: recursion

2007-09-13 Thread James Stroud
Ian Clark wrote: > Neil Cerutti wrote: >> On 2007-09-13, Gigs_ <[EMAIL PROTECTED]> wrote: >>> Can someone explain me this >>> >> def f(l): >>> if l == []: >>> return [] >>> else: >>> return f(l[1:]) + l[:1] # <= cant figure this, how is all >>> sum at the end? >> >> In

Re: recursion

2007-09-13 Thread Ian Clark
Neil Cerutti wrote: > On 2007-09-13, Gigs_ <[EMAIL PROTECTED]> wrote: >> Can someone explain me this >> > def f(l): >> if l == []: >> return [] >> else: >> return f(l[1:]) + l[:1] # <= cant figure this, how is all sum >> at the end? > > In plain English, t

Re: recursion

2007-09-13 Thread Neil Cerutti
On 2007-09-13, Gigs_ <[EMAIL PROTECTED]> wrote: > Can someone explain me this > > >>> def f(l): > if l == []: > return [] > else: > return f(l[1:]) + l[:1] # <= cant figure this, how is all sum > at the end? In plain English, the above program says: The s

Re: recursion

2007-09-13 Thread Tom Wright
Gigs_ wrote: > Can someone explain me this > def f(l): > if l == []: > return [] > else: > return f(l[1:]) + l[:1] # <= cant figure this, how is > all sum at the end? If you think about building up from the simplest case: f([]) = [] f(['a']) = f([])

Re: recursion error using setattr and getattr

2007-06-07 Thread Gabriel Genellina
En Thu, 07 Jun 2007 08:23:48 -0300, Nathan Harmston <[EMAIL PROTECTED]> escribió: > I m trying to implement an object which contains lazy" variables. My > idea is to alter the getattr and the setattr methods. However I keep > on getting a recursion error. Instead of setattr/getattr, use a prope

Re: recursion error using setattr and getattr

2007-06-07 Thread Simon Brunning
On 6/7/07, Nathan Harmston <[EMAIL PROTECTED]> wrote: > Hi, > > I m trying to implement an object which contains lazy" variables. My > idea is to alter the getattr and the setattr methods. However I keep > on getting a recursion error. Simplifying radically, you are doing: def __setattr__(sel

Re: Recursion limit problems

2007-05-15 Thread Gabriel Genellina
En Tue, 15 May 2007 09:28:52 -0300, Diez B. Roggisch <[EMAIL PROTECTED]> escribió: >> with the same hash value. >> That is, you should define __hash__ and one of (__cmp__ or __eq__). >> __neq__ (inequality) isn't required nor used by dict/set implementation. >> (Anyway, Python will transform a!=

Re: Recursion limit problems

2007-05-15 Thread Diez B. Roggisch
> with the same hash value. > That is, you should define __hash__ and one of (__cmp__ or __eq__). > __neq__ (inequality) isn't required nor used by dict/set implementation. > (Anyway, Python will transform a!=b into not(a==b), if __neq__ isn't > defined). Neither <, <=, >, >= are used. No, it won'

Re: Recursion limit problems

2007-05-14 Thread elventear
On May 14, 2:03 pm, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote: > > Dicts and sets use the key's hash value to determine the "bucket" where > the key will be placed, and == to distingish between different objects > with the same hash value. > That is, you should define __hash__ and one of (_

Re: Recursion limit problems

2007-05-14 Thread elventear
On May 14, 1:20 pm, "Terry Reedy" <[EMAIL PROTECTED]> wrote: > > Dicts first compare hashes and if they are equal, then check equality. If > two unequal strings have the same hash value, as is possible of course > (given only 2**32 possible hashes and many more possible strings), both can > still

Re: Recursion limit problems

2007-05-14 Thread Gabriel Genellina
En Mon, 14 May 2007 12:35:16 -0300, elventear <[EMAIL PROTECTED]> escribió: > Since I am defining a hash for my object, it makes sense that I should > be able to define equality. But I am not sure about inequality, in my > specific case. The paragraph above mentions that __cmp__ should be > defi

Re: Recursion limit problems

2007-05-14 Thread Terry Reedy
"elventear" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] On May 12, 12:25 am, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote: > En Fri, 11 May 2007 19:17:57 -0300, elventear <[EMAIL PROTECTED]> > escribió: > "The only required property is that objects which compare equal have the >

Re: Recursion limit problems

2007-05-14 Thread elventear
On May 12, 12:25 am, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote: > En Fri, 11 May 2007 19:17:57 -0300, elventear <[EMAIL PROTECTED]> > escribió: > "The only required property is that objects which compare equal have the > same hash value; it is advised to somehow mix together (e.g., using

Re: Recursion limit problems

2007-05-14 Thread elventear
On May 11, 11:54 pm, "Terry Reedy" <[EMAIL PROTECTED]> wrote: > > Without seeing the full code and the exception traceback, my guess is that > your __hash__ somehow calls itself due to a refence loop in your object. A > simple example of a loop: > a = []; a.append(a) > Now, list objects are not ha

Re: Recursion limit problems

2007-05-11 Thread Gabriel Genellina
En Fri, 11 May 2007 19:17:57 -0300, elventear <[EMAIL PROTECTED]> escribió: > I am runing into recursion limit problems. I have found that the > culprit was related to the __hash__ function that I had assigned to > the objects that were added to a set. As T. Reedy said, probably you have a recu

Re: Recursion limit problems

2007-05-11 Thread Terry Reedy
"elventear" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] | Hello everyone, | | I am runing into recursion limit problems. I have found that the | culprit was related to the __hash__ function that I had assigned to | the objects that were added to a set. | | Basically my __hash__ fu

Re: recursion depth problem

2007-04-23 Thread Michael Bentley
On Apr 23, 2007, at 1:57 AM, proctor wrote: > On Apr 22, 5:51 pm, Michael Bentley <[EMAIL PROTECTED]> wrote: >> Oops! Note to self: *ALWAYS* try code before posting to a public >> forum :-( >> >> def binary(val, width): >> print '%10s = the sum of' % val >> for i in [2 ** x for x

Re: recursion depth problem

2007-04-23 Thread proctor
On Apr 22, 5:51 pm, Michael Bentley <[EMAIL PROTECTED]> wrote: > Oops! Note to self: *ALWAYS* try code before posting to a public > forum :-( > > def binary(val, width): > print '%10s = the sum of' % val > for i in [2 ** x for x in range(width - 1, -1, -1)]: > a = v

Re: recursion depth problem

2007-04-22 Thread proctor
On Apr 22, 8:23 pm, [EMAIL PROTECTED] (Alex Martelli) wrote: > Steven Bethard <[EMAIL PROTECTED]> wrote: > >... > > > > > > import sys > > > def ch4(item, n=0): > > >if n < len(item): > > >if item[n] == '0': > > >item[n] = '1' > > >

Re: recursion depth problem

2007-04-22 Thread proctor
On Apr 22, 9:28 pm, Dennis Lee Bieber <[EMAIL PROTECTED]> wrote: > On 22 Apr 2007 19:13:31 -0700, proctor <[EMAIL PROTECTED]> declaimed the > following in comp.lang.python: > > > > > :-) > > > this is good stuff. for learning especially! thank you again! > > Took me some time to find... M

Re: recursion depth problem

2007-04-22 Thread [EMAIL PROTECTED]
On Apr 22, 9:13�pm, proctor <[EMAIL PROTECTED]> wrote: > On Apr 22, 7:10 pm, Dennis Lee Bieber <[EMAIL PROTECTED]> wrote: > > > > > > > On 22 Apr 2007 17:06:18 -0700, proctor <[EMAIL PROTECTED]> declaimed the > > following in comp.lang.python: > > > > > � � else: > > > > � � � � # only one of carry

Re: recursion depth problem

2007-04-22 Thread Steven Bethard
Alex Martelli wrote: > Steven Bethard <[EMAIL PROTECTED]> wrote: >... >>> import sys >>> def ch4(item, n=0): >>>if n < len(item): >>>if item[n] == '0': >>>item[n] = '1' >>>print ''.join(item) >>>

Re: recursion depth problem

2007-04-22 Thread Alex Martelli
Steven Bethard <[EMAIL PROTECTED]> wrote: ... > > import sys > > def ch4(item, n=0): > >if n < len(item): > >if item[n] == '0': > >item[n] = '1' > >print ''.join(item) > >ch4(item) > >

Re: recursion depth problem

2007-04-22 Thread proctor
On Apr 22, 7:34 pm, Steven Bethard <[EMAIL PROTECTED]> wrote: > proctor wrote: > > On Apr 22, 2:06 pm, Steven Bethard <[EMAIL PROTECTED]> wrote: > >> proctor wrote: > >>> On Apr 22, 1:24 pm, Michael Bentley <[EMAIL PROTECTED]> wrote: > On Apr 22, 2007, at 1:49 PM, proctor wrote: > > i have

Re: recursion depth problem

2007-04-22 Thread proctor
On Apr 22, 7:10 pm, Dennis Lee Bieber <[EMAIL PROTECTED]> wrote: > On 22 Apr 2007 17:06:18 -0700, proctor <[EMAIL PROTECTED]> declaimed the > following in comp.lang.python: > > > > else: > > > # only one of carry in, b1, or b2 is set > > #or none is set! Missed t

Re: recursion depth problem

2007-04-22 Thread Steven Bethard
proctor wrote: > On Apr 22, 2:06 pm, Steven Bethard <[EMAIL PROTECTED]> wrote: >> proctor wrote: >>> On Apr 22, 1:24 pm, Michael Bentley <[EMAIL PROTECTED]> wrote: On Apr 22, 2007, at 1:49 PM, proctor wrote: > i have a small function which mimics binary counting. it runs fine as > lon

Re: recursion depth problem

2007-04-22 Thread proctor
On Apr 22, 5:51 pm, Dennis Lee Bieber <[EMAIL PROTECTED]> wrote: > On Sun, 22 Apr 2007 17:37:05 -0500, Michael Bentley > <[EMAIL PROTECTED]> declaimed the following in comp.lang.python: > > > Anything that can be done with recursion can be done without > > recursion. If you really wanted to mimic

Re: recursion depth problem

2007-04-22 Thread proctor
On Apr 22, 5:51 pm, Michael Bentley <[EMAIL PROTECTED]> wrote: > Oops! Note to self: *ALWAYS* try code before posting to a public > forum :-( > > def binary(val, width): > print '%10s = the sum of' % val > for i in [2 ** x for x in range(width - 1, -1, -1)]: > a = v

Re: recursion depth problem

2007-04-22 Thread Michael Bentley
Oops! Note to self: *ALWAYS* try code before posting to a public forum :-( def binary(val, width): print '%10s = the sum of' % val for i in [2 ** x for x in range(width - 1, -1, -1)]: a = val / i print ' ' * 13 + '%s * (2 ** %s)' % (a, width)

Re: recursion depth problem

2007-04-22 Thread Michael Bentley
On Apr 22, 2007, at 5:47 PM, proctor wrote: > On Apr 22, 4:37 pm, Michael Bentley <[EMAIL PROTECTED]> wrote: >> On Apr 22, 2007, at 4:08 PM, proctor wrote: >> >> >> >>> On Apr 22, 2:55 pm, [EMAIL PROTECTED] wrote: On Apr 22, 11:49 am, proctor <[EMAIL PROTECTED]> wrote: >> > hello, >> >>>

Re: recursion depth problem

2007-04-22 Thread proctor
On Apr 22, 5:05 pm, tac-tics <[EMAIL PROTECTED]> wrote: > Yes, you should use a for loop in this situation. > > Certain functional languages, such as Scheme and various LISP dialects > allow for what is called "tail recursion" which effectively eliminates > this problem by internally converting rec

Re: recursion depth problem

2007-04-22 Thread tac-tics
Yes, you should use a for loop in this situation. Certain functional languages, such as Scheme and various LISP dialects allow for what is called "tail recursion" which effectively eliminates this problem by internally converting recursion to iteration. Python isn't really cut out for heavy recurs

Re: recursion depth problem

2007-04-22 Thread proctor
On Apr 22, 4:37 pm, Michael Bentley <[EMAIL PROTECTED]> wrote: > On Apr 22, 2007, at 4:08 PM, proctor wrote: > > > > > On Apr 22, 2:55 pm, [EMAIL PROTECTED] wrote: > >> On Apr 22, 11:49 am, proctor <[EMAIL PROTECTED]> wrote: > > >>> hello, > > >>> i have a small function which mimics binary countin

Re: recursion depth problem

2007-04-22 Thread Michael Bentley
On Apr 22, 2007, at 4:08 PM, proctor wrote: > On Apr 22, 2:55 pm, [EMAIL PROTECTED] wrote: >> On Apr 22, 11:49 am, proctor <[EMAIL PROTECTED]> wrote: >> >> >> >>> hello, >> >>> i have a small function which mimics binary counting. it runs >>> fine as >>> long as the input is not too long, but

Re: recursion depth problem

2007-04-22 Thread proctor
On Apr 22, 2:06 pm, Steven Bethard <[EMAIL PROTECTED]> wrote: > proctor wrote: > > On Apr 22, 1:24 pm, Michael Bentley <[EMAIL PROTECTED]> wrote: > >> On Apr 22, 2007, at 1:49 PM, proctor wrote: > > >>> i have a small function which mimics binary counting. it runs fine as > >>> long as the input i

Re: recursion depth problem

2007-04-22 Thread proctor
On Apr 22, 2:55 pm, [EMAIL PROTECTED] wrote: > On Apr 22, 11:49 am, proctor <[EMAIL PROTECTED]> wrote: > > > > > hello, > > > i have a small function which mimics binary counting. it runs fine as > > long as the input is not too long, but if i give it input longer than > > 8 characters it gives >

Re: recursion depth problem

2007-04-22 Thread half . italian
On Apr 22, 11:49 am, proctor <[EMAIL PROTECTED]> wrote: > hello, > > i have a small function which mimics binary counting. it runs fine as > long as the input is not too long, but if i give it input longer than > 8 characters it gives > > RuntimeError: maximum recursion depth exceeded in cmp > > i

  1   2   >