a little bit of recursion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dominik Kaspar

    a little bit of recursion

    I'm trying to write a quite big recursive function, but the baseline
    is the following:

    def rek(N, L):
    N = N + 1
    if N >= 9: return L
    else: return rek(N, L = L.append(N))

    print rek(0, [])

    Why isn't this working?
    In my opinion it should return [1, 2, ..., 9]

    Thanks for any answers.
    Dominik
  • rzed

    #2
    Re: a little bit of recursion

    Dominik Kaspar wrote:[color=blue]
    > I'm trying to write a quite big recursive function, but the baseline
    > is the following:
    >
    > def rek(N, L):
    > N = N + 1
    > if N >= 9: return L
    > else: return rek(N, L = L.append(N))
    >
    > print rek(0, [])
    >
    > Why isn't this working?
    > In my opinion it should return [1, 2, ..., 9]
    >
    > Thanks for any answers.[/color]


    One problem is that L = L.append(N) does not have the effect you want.
    Try it on its own.
    Another is that it won't return [1,2,...,9] even when that is
    corrected.




    Comment

    • Skip Montanaro

      #3
      Re: a little bit of recursion


      Dominik> def rek(N, L):
      Dominik> N = N + 1
      Dominik> if N >= 9: return L
      Dominik> else: return rek(N, L = L.append(N))

      In the recursive calls, L is None. Just pass L as the second arg:

      def rek(N, L):
      N = N + 1
      if N >= 9:
      return L
      L.append(N)
      return rek(N, L)

      Skip


      Comment

      • Gregor Lingl

        #4
        Re: a little bit of recursion



        rzed schrieb:[color=blue]
        > Dominik Kaspar wrote:
        >[color=green]
        >>I'm trying to write a quite big recursive function, but the baseline
        >>is the following:
        >>
        >>def rek(N, L):
        >> N = N + 1
        >> if N >= 9: return L
        >> else: return rek(N, L = L.append(N))
        >>
        >>print rek(0, [])
        >>
        >>Why isn't this working?
        >>In my opinion it should return [1, 2, ..., 9]
        >>
        >>Thanks for any answers.[/color]
        >
        >
        >
        > One problem is that L = L.append(N) does not have the effect you want.
        > Try it on its own.
        > Another is that it won't return [1,2,...,9] even when that is
        > corrected.[/color]

        That certainly depends on the way one corrects it.
        However Dominik first has to notice, that append is a
        method, which *changes* L but doesn't return L. Instead
        it returns None. So it may be ok to call L.append(N),
        but not on the right side of an assignment statement.

        You can obsrve what I mean here:
        [color=blue][color=green][color=darkred]
        >>> L = [1,2,3]
        >>> print L.append(4)[/color][/color][/color]
        None[color=blue][color=green][color=darkred]
        >>> L[/color][/color][/color]
        [1, 2, 3, 4][color=blue][color=green][color=darkred]
        >>>[/color][/color][/color]

        HTH, Gregor[color=blue]
        >
        >
        >
        >[/color]

        Comment

        • Gregor Lingl

          #5
          Re: a little bit of recursion



          Skip Montanaro schrieb:[color=blue]
          > Dominik> def rek(N, L):
          > Dominik> N = N + 1
          > Dominik> if N >= 9: return L
          > Dominik> else: return rek(N, L = L.append(N))
          >
          > In the recursive calls, L is None. Just pass L as the second arg:
          >
          > def rek(N, L):
          > N = N + 1
          > if N >= 9:
          > return L
          > L.append(N)
          > return rek(N, L)
          >
          > Skip
          >[/color]
          Nice, but will not append 9 as desired
          Gregor[color=blue]
          >[/color]

          Comment

          • Gregor Lingl

            #6
            Re: a little bit of recursion



            Skip Montanaro schrieb:[color=blue]
            > Dominik> def rek(N, L):
            > Dominik> N = N + 1
            > Dominik> if N >= 9: return L
            > Dominik> else: return rek(N, L = L.append(N))
            >
            > In the recursive calls, L is None. Just pass L as the second arg:
            >
            > def rek(N, L):
            > N = N + 1
            > if N >= 9:
            > return L
            > L.append(N)
            > return rek(N, L)
            >
            > Skip
            >[/color]
            Nice, but will not append 9 as desired
            Gregor[color=blue]
            >[/color]


            Comment

            • Skip Montanaro

              #7
              Re: a little bit of recursion


              Gregor> Skip Montanaro schrieb:
              Dominik> def rek(N, L):
              Dominik> N = N + 1
              Dominik> if N >= 9: return L
              Dominik> else: return rek(N, L = L.append(N))
              [color=blue][color=green]
              >> In the recursive calls, L is None. Just pass L as the second arg:
              >>
              >> def rek(N, L):
              >> N = N + 1
              >> if N >= 9:
              >> return L
              >> L.append(N)
              >> return rek(N, L)[/color][/color]

              Gregor> Nice, but will not append 9 as desired

              Sure, but that's a logic error, not a misunderstandin g about what L.append()
              returns. I suspect the latter was what confused Dominik. Once he runs
              something that doesn't raise an AttributeError, he'll quickly discover that
              his test should be "if N > 9". He couldn't even get to that point with his
              original code.

              Skip

              Comment

              • Georgy Pruss

                #8
                Re: a little bit of recursion


                "Gregor Lingl" <glingl@aon.a t> wrote in message news:3F95A439.4 060102@aon.at.. .[color=blue]
                >[color=green]
                > > <...>
                > >
                > > def rek(N, L):
                > > N = N + 1
                > > if N >= 9:
                > > return L
                > > L.append(N)
                > > return rek(N, L)
                > >
                > > Skip
                > >[/color]
                > Nice, but will not append 9 as desired
                > Gregor[color=green]
                > >[/color]
                >[/color]

                def rek(N, L):
                if N >= 9: return L
                N += 1
                L.append(N)
                return rek(N,L)

                G-:

                --
                Georgy Pruss
                E^mail: 'ZDAwMTEyMHQwMz MwQGhvdG1haWwuY 29t\n'.decode(' base64')


                Comment

                Working...