By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,967 Members | 1,217 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,967 IT Pros & Developers. It's quick & easy.

Maybe a stupid idea

P: n/a
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.
Jul 18 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

At 2003-12-31T17:49:55Z, se**@linuxcult.com (sebb) writes:
I thought about that because it's not very easy to program a cycle.
Your example doesn't really define a cycle. What is it? A loop from
start-val to end-val to start-val?
def cycle(a,b): .... return range(a,b)+range(b,a,-1)
.... cycle(0,10)

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
Here is a simple example :

b=0

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

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


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.
http://www.strausergroup.com/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)

iD8DBQE/8xj95sRg+Y0CpvERAmRzAJ49ZjF7tBXPrMQ8VnpeB0tvMHb5gQ CeJ7cO
+r9MHpgTAA9nZmdrTGBDGOo=
=Apc/
-----END PGP SIGNATURE-----
Jul 18 '05 #2

P: n/a
"sebb" <se**@linuxcult.com> wrote in message
news:56**************************@posting.google.c om...
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 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.
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.


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
Jul 18 '05 #3

P: n/a
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:
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.

Jul 18 '05 #4

P: n/a
On Wed, 2003-12-31 at 11:49, sebb wrote:
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.


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.write('^')

def doDown(i):
sys.stdout.write('v')

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

Cheers,

// m
Jul 18 '05 #5

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


You might also define cycle as an iterator
def cycle(a, b): .... return itertools.chain(xrange(a, b), xrange(b, a, -1)) list(cycle(0, 10)
Furthermore, you might want to treat the one-argument version similarly
to range: def cycle(a, b=None): .... if b is None: a, b = 0, a
.... return itertools.chain(xrange(a, b), xrange(b, a, -1)) list(cycle(10)) [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: def cycle(a, b=None, c=1): .... if b is None: a, b = 0, a
.... return itertools.chain(xrange(a, b, c), xrange(b, a, -c)) list(cycle(5, 10, 2))

[5, 7, 9, 10, 8, 6]
.... though that last result looks a bit odd. This must be the problem
with hypergeneralization I hear so much about.

Jeff

Jul 18 '05 #6

P: n/a
On 31 Dec 2003 09:49:55 -0800, rumours say that se**@linuxcult.com
(sebb) might have written:
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.


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
Jul 18 '05 #7

P: n/a
sebb fed this fish to the penguins on Wednesday 31 December 2003 09:49
am:

It takes two while blocks to do a cycle.
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)
-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Bestiaria Home Page: http://www.beastie.dm.net/ <
Home Page: http://www.dm.net/~wulfraed/ <


Jul 18 '05 #8

P: n/a
se**@linuxcult.com (sebb) writes:
I know that cycles is not a structure of any programming language,
Maybe there's a reason for that.
but I want some opinions the know if my idea is stupid or not.


Well, why would you want such a feature? What real programming
situations would you use it in?
Jul 18 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.