Maybe a stupid idea

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

    #1

    Maybe a stupid idea

    I'm kind of newbie to programming, but I thought of something and I
    want some opinions on that.

    It's about a new instruction block to do some cycles.

    I thought about that because it's not very easy to program a cycle.
    Here is a simple example :

    b=0

    while b < 50:
    b+=1
    print "*" * b

    while b > 0:
    b-= 1
    print "*" * b

    It takes two while blocks to do a cycle.

    I know that cycles is not a structure of any programming language, but
    I want some opinions the know if my idea is stupid or not.

    If the idea is not so stupid, python may be the first language to have
    a completely new structure block :-)

    Note : English is not my mother tongue so the message can have
    mistakes.
  • Kirk Strauser

    #2
    Re: Maybe a stupid idea

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    At 2003-12-31T17:49:55Z, sebb@linuxcult. com (sebb) writes:
    [color=blue]
    > I thought about that because it's not very easy to program a cycle.[/color]

    Your example doesn't really define a cycle. What is it? A loop from
    start-val to end-val to start-val?
    [color=blue][color=green][color=darkred]
    >>> def cycle(a,b):[/color][/color][/color]
    .... return range(a,b)+rang e(b,a,-1)
    ....[color=blue][color=green][color=darkred]
    >>> cycle(0,10)[/color][/color][/color]
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
    [color=blue]
    > Here is a simple example :
    >
    > b=0
    >
    > while b < 50:
    > b+=1
    > print "*" * b
    >
    > while b > 0:
    > b-= 1
    > print "*" * b[/color]

    for length in cycle(0,50):
    print "*" * length

    There you go, and it didn't even take a new language structure. :)
    - --
    Kirk Strauser
    The Strauser Group
    Open. Solutions. Simple.

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.3 (GNU/Linux)

    iD8DBQE/8xj95sRg+Y0CpvE RAmRzAJ49ZjF7tB XPrMQ8VnpeB0tvM Hb5gQCeJ7cO
    +r9MHpgTAA9nZmd rTGBDGOo=
    =Apc/
    -----END PGP SIGNATURE-----

    Comment

    • John Roth

      #3
      Re: Maybe a stupid idea

      "sebb" <sebb@linuxcult .com> wrote in message
      news:56f42e53.0 312310949.39826 ffd@posting.goo gle.com...[color=blue]
      > I'm kind of newbie to programming, but I thought of something and I
      > want some opinions on that.
      >
      > It's about a new instruction block to do some cycles.
      >
      > I thought about that because it's not very easy to program a cycle.
      > Here is a simple example :
      >
      > b=0
      >
      > while b < 50:
      > b+=1
      > print "*" * b
      >
      > while b > 0:
      > b-= 1
      > print "*" * b
      >
      > It takes two while blocks to do a cycle.[/color]

      I take it you want to go up a sequence and then back down.

      A somewhat simpler (at least, fewer lines) implementation
      of your example:

      for b in range(1, 50):
      print "*" * b
      for b in range (49, 0, -1):
      print "*" * b

      This eliminates the need to control the variable.
      [color=blue]
      > I know that cycles is not a structure of any programming language, but
      > I want some opinions the know if my idea is stupid or not.[/color]

      It's the type of thing that, while useful, isn't of enough general
      utility to put into a language, especially as it's easy enough to
      create it whenever you need it.

      Python development has a definite bias in favor of not adding
      things to the language unless they would be widely useful.

      John Roth


      Comment

      • Michael Geary

        #4
        Re: Maybe a stupid idea

        Hi Sebb,

        Your idea is not stupid at all, and your English is very good. :-)

        I don't know how much luck you would have getting this built into the core
        language, but fortunately you don't really need to. It is very easy to add
        it yourself. Here are a couple of ways you could do it.

        The easiest would be something like this, a function that returns a list
        containing your cycle of values. Note that "first" is the first and last
        value in the list, and "limit" is one more than the largest value in the
        list. I did it this way to be consistent with Python's range function.

        def cycle( first, limit ):
        "Return list of values from first up to limit-1 and down to first."
        return range( first, limit ) + range( limit - 2, first - 1, -1 )

        print cycle( 1, 5 ) # print the list itself

        for b in cycle( 1, 5 ):
        print "*" * b

        Another approach would use a generator (a function that returns a series of
        values by using a yield statement):

        def cycle( first, limit ):
        "Return series of values from first up to limit-1 and down to first."
        for value in xrange( first, limit ):
        yield value
        for value in xrange( limit - 2, first - 1, -1 ):
        yield value

        for b in cycle( 1, 5 ):
        print "*" * b

        This approach doesn't construct and return the actual list of values as the
        first one does (that's why I didn't put the "print cycle( 1, 5 )" statement
        in this test). It calculates and returns the values on the fly. This would
        use less memory for a very long cycle, but it's not quite as simple as the
        first approach. Both techniques are useful to know about.

        Hope that helps!

        -Mike

        sebb wrote:[color=blue]
        > I'm kind of newbie to programming, but I thought of
        > something and I want some opinions on that.
        >
        > It's about a new instruction block to do some cycles.
        >
        > I thought about that because it's not very easy to
        > program a cycle. Here is a simple example :
        >
        > b=0
        >
        > while b < 50:
        > b+=1
        > print "*" * b
        >
        > while b > 0:
        > b-= 1
        > print "*" * b
        >
        > It takes two while blocks to do a cycle.
        >
        > I know that cycles is not a structure of any programming
        > language, but I want some opinions the know if my idea
        > is stupid or not.
        >
        > If the idea is not so stupid, python may be the first language
        > to have a completely new structure block :-)
        >
        > Note : English is not my mother tongue so the message can
        > have mistakes.[/color]


        Comment

        • Mark McEahern

          #5
          Re: Maybe a stupid idea

          On Wed, 2003-12-31 at 11:49, sebb wrote:[color=blue]
          > I'm kind of newbie to programming, but I thought of something and I
          > want some opinions on that.
          >
          > It's about a new instruction block to do some cycles.[/color]

          Regarding the subject, I find it comforting to think that all ideas are
          stupid. Just like all questions are. ;-)

          Heh, I don't really know what you mean by cycle. It'd be helpful to
          know what problem you're trying to solve. Anyway, consider this
          admittedly pointless example:

          #!/usr/bin/env python

          import sys

          def makecycle(doUp, doDown):
          def cycle(start, end):
          for i in range(start, end):
          yield i, doUp
          for i in range(end, start, -1):
          yield i, doDown
          return cycle

          def doUp(i):
          sys.stdout.writ e('^')

          def doDown(i):
          sys.stdout.writ e('v')

          cycle = makecycle(doUp, doDown)
          for i, func in cycle(1, 10):
          func(i)

          Cheers,

          // m


          Comment

          • Jeff Epler

            #6
            Re: Maybe a stupid idea

            On Wed, Dec 31, 2003 at 06:45:05PM +0000, Kirk Strauser wrote:[color=blue][color=green][color=darkred]
            > >>> def cycle(a,b):[/color][/color]
            > ... return range(a,b)+rang e(b,a,-1)
            > ...[color=green][color=darkred]
            > >>> cycle(0,10)[/color][/color]
            > [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1][/color]

            You might also define cycle as an iterator[color=blue][color=green][color=darkred]
            >>> def cycle(a, b):[/color][/color][/color]
            .... return itertools.chain (xrange(a, b), xrange(b, a, -1))[color=blue][color=green][color=darkred]
            >>> list(cycle(0, 10)[/color][/color][/color]

            Furthermore, you might want to treat the one-argument version similarly
            to range:[color=blue][color=green][color=darkred]
            >>> def cycle(a, b=None):[/color][/color][/color]
            .... if b is None: a, b = 0, a
            .... return itertools.chain (xrange(a, b), xrange(b, a, -1))[color=blue][color=green][color=darkred]
            >>> list(cycle(10))[/color][/color][/color]
            [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

            You could also change it to support a step argument:[color=blue][color=green][color=darkred]
            >>> def cycle(a, b=None, c=1):[/color][/color][/color]
            .... if b is None: a, b = 0, a
            .... return itertools.chain (xrange(a, b, c), xrange(b, a, -c))[color=blue][color=green][color=darkred]
            >>> list(cycle(5, 10, 2))[/color][/color][/color]
            [5, 7, 9, 10, 8, 6]
            .... though that last result looks a bit odd. This must be the problem
            with hypergeneraliza tion I hear so much about.

            Jeff

            Comment

            • Christos TZOTZIOY Georgiou

              #7
              Re: Maybe a stupid idea

              On 31 Dec 2003 09:49:55 -0800, rumours say that sebb@linuxcult. com
              (sebb) might have written:
              [color=blue]
              >Here is a simple example :
              >
              >b=0
              >
              >while b < 50:
              > b+=1
              > print "*" * b
              >
              >while b > 0:
              > b-= 1
              > print "*" * b
              >
              >It takes two while blocks to do a cycle.[/color]

              Another way to do the above in a single block:

              for a in xrange(-49, 50):
              b= 50-abs(a)
              print "*" * b

              or the more cryptic (which you really should ignore):

              from itertools import imap
              for a in imap(50 .__sub__, imap(abs, xrange(-49, 50))):
              print "*" * b

              Note that significant space between "50" and "." :)
              --
              TZOTZIOY, I speak England very best,
              Ils sont fous ces Redmontains! --Harddix

              Comment

              • Dennis Lee Bieber

                #8
                Re: Maybe a stupid idea

                sebb fed this fish to the penguins on Wednesday 31 December 2003 09:49
                am:
                [color=blue]
                >
                > It takes two while blocks to do a cycle.
                >[/color]
                Given the nature of the sample, I'd not use while blocks anyways...

                for b in xrange(50):
                print "*" * (b + 1)
                for b in xrange(50):
                print "*" * (50 - b)


                --[color=blue]
                > =============== =============== =============== =============== == <
                > wlfraed@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
                > wulfraed@dm.net | Bestiaria Support Staff <
                > =============== =============== =============== =============== == <
                > Bestiaria Home Page: http://www.beastie.dm.net/ <
                > Home Page: http://www.dm.net/~wulfraed/ <[/color]

                Comment

                • Paul Rubin

                  #9
                  Re: Maybe a stupid idea

                  sebb@linuxcult. com (sebb) writes:[color=blue]
                  > I know that cycles is not a structure of any programming language,[/color]

                  Maybe there's a reason for that.
                  [color=blue]
                  > but I want some opinions the know if my idea is stupid or not.[/color]

                  Well, why would you want such a feature? What real programming
                  situations would you use it in?

                  Comment

                  Working...