473,418 Members | 2,174 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

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.
Jul 18 '05 #1
8 1612
-----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
"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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Julian Campbell | last post by:
I have for a few weeks now been looking to improove my computer skills. I have been looking at learning VB but would like to know what exactly I can use it for!!! ( I said it may be stupid ) I...
11
by: Steve Jorgensen | last post by:
Now, I don't mean that it's stupid in general. I'm actually pretty impressed with the whole MS Networking thing, but there's one stupid thing that keeps getting in my way, and I can't believe no...
2
by: Dutchy | last post by:
Hi there, After spending several hours trying all I could imagine and search for in Google I gave up. In a continuous form I want to sort the choosen column by clicking the header (label) of...
3
by: Amil | last post by:
I must be missing something very simple. I've had a web site running for a long time (anonymous access). Web.config authentication is original (anyone gets in): <authentication mode="Windows"...
28
by: liorm | last post by:
Hi everyone, I need to write a web app, that will support millions of user accounts, template-based user pages and files upload. The client is going to be written in Flash. I wondered if I coudl...
135
by: Xah Lee | last post by:
Tabs versus Spaces in Source Code Xah Lee, 2006-05-13 In coding a computer program, there's often the choices of tabs or spaces for code indentation. There is a large amount of confusion about...
7
by: badc0de4 | last post by:
Is this a stupid hack or a clever hack? Is it a "hack" at all? ==================== #include <stdio.h> /* !!HACK!! */ /* no parenthesis on the #define'd expression */ #define...
9
by: Alec | last post by:
Sorry guys, stupid question.... Am no programming expert and have only just started using php for creating dynamic news pages. Then I see a dynamic website without the php extension. ...
10
by: canabatz | last post by:
Hello friends , i got this code that works perfect in google chrome,but not in IE7 or FireFox!! <HTML> <META HTTP-EQUIV="refresh"...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.