new in programing

E

Efrain Marrero

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++){
 
B

Brett Hoerner

Efrain said:
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?
 
M

Mike C. Fletcher

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 said:
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
 
C

Cameron Laird

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

James Stroud

[...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
 
D

Dan Bishop

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
 
J

James Stroud

Cameron said:
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)
 
C

Cameron Laird

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.
 

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

Similar Threads

Blue J Ciphertext Program 2
C programing code 6
Something is wrong 1
Java matrix problem 3
Need help with this Python code. 2
Fibonacci 0
I Need Fix In Code 1
Drawing missing in bitmap in a pure C win32 program 4

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top