Maybe a stupid idea

S

sebb

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.
 
K

Kirk Strauser

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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?
.... return range(a,b)+range(b,a,-1)
....[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+Y0CpvERAmRzAJ49ZjF7tBXPrMQ8VnpeB0tvMHb5gQCeJ7cO
+r9MHpgTAA9nZmdrTGBDGOo=
=Apc/
-----END PGP SIGNATURE-----
 
J

John Roth

sebb said:
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
 
M

Mark McEahern

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
 
M

Michael Geary

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
 
J

Jeff Epler

... return range(a,b)+range(b,a,-1)
...[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
Furthermore, you might want to treat the one-argument version similarly
to range:.... if b is None: a, b = 0, a
.... return itertools.chain(xrange(a, b), xrange(b, a, -1))[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:.... if b is None: a, b = 0, a
.... return itertools.chain(xrange(a, b, c), xrange(b, a, -c))[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
 
C

Christos TZOTZIOY Georgiou

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 "." :)
 
D

Dennis Lee Bieber

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)


--
 
P

Paul Rubin

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,174
Messages
2,570,940
Members
47,486
Latest member
websterztechnologies01

Latest Threads

Top