new in programing

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

  1. 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
    #1
    1. Advertising

  2. 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
    #2
    1. Advertising

  3. 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
    #3
  4. 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
    #4
  5. Efrain Marrero

    James Stroud Guest

    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
    #5
  6. Efrain Marrero

    Dan Bishop Guest

    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
    #6
  7. Efrain Marrero

    James Stroud Guest

    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
    #7
  8. 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
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. valli

    structural programing

    valli, Nov 15, 2004, in forum: VHDL
    Replies:
    2
    Views:
    2,201
    Jim Lewis
    Nov 15, 2004
  2. Support

    Simple asp.net programing question

    Support, Jan 27, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    1,421
    Karl Seguin [MVP]
    Jan 27, 2006
  3. Murray J

    Some Programing Required

    Murray J, Dec 18, 2004, in forum: Java
    Replies:
    5
    Views:
    325
  4. zegga
    Replies:
    0
    Views:
    609
    zegga
    Aug 1, 2009
  5. Replies:
    2
    Views:
    486
    Thomas 'PointedEars' Lahn
    Mar 11, 2008
Loading...

Share This Page