# new in programing

Discussion in 'Python' started by Efrain Marrero, Dec 9, 2005.

1. ### Efrain MarreroGuest

i want to now how to do this in python
this is java

for(int i=1 ; i<=lim ; i++){

for(int j=i+1; j<=lim+1; j++){

for(int k =j+1; k<=lim+2;k++){

for(int l=k+1 ; l<=lim+3;l++){

for(int m=l+1 ; m<=lim+4;m++){

for(int o=m+1 ; o<=lim+5;o++){

Efrain Marrero, Dec 9, 2005

2. ### Brett HoernerGuest

Efrain Marrero wrote:
> i want to now how to do this in python
> this is java
>
>
> for(int i=1 ; i<=lim ; i++){
>
> for(int j=i+1; j<=lim+1; j++){
>
> for(int k =j+1; k<=lim+2;k++){
>
> for(int l=k+1 ; l<=lim+3;l++){
>
> for(int m=l+1 ; m<=lim+4;m++){
>
> for(int o=m+1 ; o<=lim+5;o++){

That code wouldn't run on a JVM, do you have the rest? Or are we to
assume these loops do nothing and just close all the braces?

Brett Hoerner, Dec 9, 2005

3. ### Mike C. FletcherGuest

Python iterates over "things" (objects), of which integer numbers are
just one possible choice. The range built-in command produces ranges of
integers which are useful for tasks such as this.

lim = 3

for i in range( 1, lim+1 ):
for j in range( i+1, lim+2):
for k in range( j+1, lim+3):
for l in range( k+1, lim+4):
for m in range( l+1, lim+5):
for n in range( m+1, lim+6):
print i,j,k,l,m,n

Would be a direct translation of your code (with a few lines to make it
actually do something and a fix for the last variable name).

HTH,
Mike

Efrain Marrero wrote:

>i want to now how to do this in python
>this is java
>
>
>for(int i=1 ; i<=lim ; i++){
>
> for(int j=i+1; j<=lim+1; j++){
>
>

....

--
________________________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://www.vrplumber.com
http://blog.vrplumber.com

Mike C. Fletcher, Dec 9, 2005
4. ### Cameron LairdGuest

In article <>,
Mike C. Fletcher <> wrote:
>Python iterates over "things" (objects), of which integer numbers are
>just one possible choice. The range built-in command produces ranges of
>integers which are useful for tasks such as this.
>
>lim = 3
>
>for i in range( 1, lim+1 ):
> for j in range( i+1, lim+2):
> for k in range( j+1, lim+3):
> for l in range( k+1, lim+4):
> for m in range( l+1, lim+5):
> for n in range( m+1, lim+6):
> print i,j,k,l,m,n
>
>Would be a direct translation of your code (with a few lines to make it
>actually do something and a fix for the last variable name).

.
.
.
for hextuple in [(i, j, k, l, m, n)
for i in range(1, lim + 1) \
for j in range (1, lim + 2) \
for k in range (1, lim + 3) \
for l in range (1, lim + 4) \
for m in range (1, lim + 5) \
for n in range (1, lim + 6)]:
print hextuple

I don't think the list comprehension helps, in this case--although
it hints at the temptation of an eval-able expression which is
briefer. More on that, later.

Cameron Laird, Dec 9, 2005
5. ### James StroudGuest

Cameron Laird wrote:
> In article <>,
> Mike C. Fletcher <> wrote:
>
>>Python iterates over "things" (objects), of which integer numbers are
>>just one possible choice. The range built-in command produces ranges of
>>integers which are useful for tasks such as this.
>>

[...clip (something that begs of recursion)...]

> I don't think the list comprehension helps, in this case--although
> it hints at the temptation of an eval-able expression which is
> briefer. More on that, later.

This goes backwards. Going forwards is left as an exercise for whomever:

def do_something(*args):
print args

def do_deeply(first, depth, lim, todo, inc, *args):
if depth < lim:
do_deeply(first+inc, depth+inc, lim, todo, inc, *args)
if first < depth:
do_deeply(first+inc, depth, lim, todo, inc, *args + (first,))
else:
do_something(*args)

do_deeply(first=1, depth=6, lim=8, todo=do_something, inc=1)

James

James Stroud, Dec 9, 2005
6. ### Dan BishopGuest

Cameron Laird wrote:
....
> for hextuple in [(i, j, k, l, m, n)
> for i in range(1, lim + 1) \
> for j in range (1, lim + 2) \
> for k in range (1, lim + 3) \
> for l in range (1, lim + 4) \
> for m in range (1, lim + 5) \
> for n in range (1, lim + 6)]:
> print hextuple
>
> I don't think the list comprehension helps, in this case--although
> it hints at the temptation of an eval-able expression which is
> briefer. More on that, later.

from the recent "N-uples from list of lists" thread import cross

for hextuple in cross(*[xrange(1, lim+p) for p in xrange(1, 7)]):
print hextuple

Dan Bishop, Dec 10, 2005
7. ### James StroudGuest

Cameron Laird wrote:
> In article <>,
> Mike C. Fletcher <> wrote:
>
>>Python iterates over "things" (objects), of which integer numbers are
>>just one possible choice. The range built-in command produces ranges of
>>integers which are useful for tasks such as this.

Cameron Laird wrote:

> In article <>,
> Mike C. Fletcher <> wrote:
>
>> Python iterates over "things" (objects), of which integer numbers are just

one possible choice. The range built-in command produces ranges of integers
which are useful for tasks such as this.
>>

[...clip (something that begs of recursion)...]

> I don't think the list comprehension helps, in this case--although
> it hints at the temptation of an eval-able expression which is briefer. More

on that, later.

I noticed my last one had duplicates. I have wasted an embarrassingly large
amount of time fixing it:

def do_something(*args):
print args

def do_deeply(first, depth, lim, inc, doit=True, *args):
if depth < lim:
do_deeply(first+inc, depth+inc, lim, inc, False, *args)
if first <= depth:
do_deeply(first+inc, depth, lim, inc, True, *args + (first,))
elif doit:
do_something(*args)

do_deeply(first=1, depth=3, lim=4, inc=1)

James Stroud, Dec 10, 2005
8. ### Cameron LairdGuest

Nested loops, cross products, and so on (was: new in programing)

In article <>,
Dan Bishop <> wrote:
>Cameron Laird wrote:
>...
>> for hextuple in [(i, j, k, l, m, n)
>> for i in range(1, lim + 1) \
>> for j in range (1, lim + 2) \
>> for k in range (1, lim + 3) \
>> for l in range (1, lim + 4) \
>> for m in range (1, lim + 5) \
>> for n in range (1, lim + 6)]:
>> print hextuple
>>
>> I don't think the list comprehension helps, in this case--although
>> it hints at the temptation of an eval-able expression which is
>> briefer. More on that, later.

>
>from the recent "N-uples from list of lists" thread import cross
>
>for hextuple in cross(*[xrange(1, lim+p) for p in xrange(1, 7)]):
> print hextuple
>

Tangential remarks: cross product is *very* important; why
isn't it built in? Yes, I recognize that the recipes supplied
elsewhere are quite nice.

I've seen a lot of Fortran and C code of the "for (...) {for (...) { ..."
variety. I have a deep suspicion that most of them betray
fundamental miscomprehension of physical realities. It's very,
*very* unusual for any meaningful measurement to arise across a
medium-dimension product of real intervals. I invite
counterexamples. In the meantime, I'll persist in suspecting
that such computations are symptoms of a misunderstanding.

So: cross products are valuable, but particularly so when not
limited to crosses over numeric intervals.

Cameron Laird, Dec 16, 2005