new in programing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Efrain Marrero

    #1

    new in programing

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

  • Brett Hoerner

    #2
    Re: new in programing

    Efrain Marrero wrote:[color=blue]
    > 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++){[/color]

    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?

    Comment

    • Mike C. Fletcher

      #3
      Re: new in programing

      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:
      [color=blue]
      >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++){
      >
      >[/color]
      ....

      --
      _______________ _______________ _______________ ___
      Mike C. Fletcher
      Designer, VR Plumber, Coder



      Comment

      • Cameron Laird

        #4
        Re: new in programing

        In article <mailman.1914.1 134158780.18701 .python-list@python.org >,
        Mike C. Fletcher <mcfletch@vrplu mber.com> wrote:[color=blue]
        >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).[/color]

        Comment

        • James Stroud

          #5
          Re: new in programing

          Cameron Laird wrote:[color=blue]
          > In article <mailman.1914.1 134158780.18701 .python-list@python.org >,
          > Mike C. Fletcher <mcfletch@vrplu mber.com> wrote:
          >[color=green]
          >>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.
          >>[/color][/color]

          [...clip (something that begs of recursion)...]
          [color=blue]
          > 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.[/color]

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


          def do_something(*a rgs):
          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(*a rgs)

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


          James

          Comment

          • Dan Bishop

            #6
            Re: new in programing

            Cameron Laird wrote:
            ....[color=blue]
            > 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.[/color]

            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

            Comment

            • James Stroud

              #7
              Re: new in programing

              Cameron Laird wrote:[color=blue]
              > In article <mailman.1914.1 134158780.18701 .python-list@python.org >,
              > Mike C. Fletcher <mcfletch@vrplu mber.com> wrote:
              >[color=green]
              >>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.[/color][/color]

              Cameron Laird wrote:
              [color=blue]
              > In article <mailman.1914.1 134158780.18701 .python-list@python.org >,
              > Mike C. Fletcher <mcfletch@vrplu mber.com> wrote:
              >[color=green]
              >> Python iterates over "things" (objects), of which integer numbers are just[/color][/color]
              one possible choice. The range built-in command produces ranges of integers
              which are useful for tasks such as this.[color=blue][color=green]
              >>[/color][/color]

              [...clip (something that begs of recursion)...]
              [color=blue]
              > 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[/color]
              on that, later.

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

              def do_something(*a rgs):
              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(*a rgs)

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

              Comment

              • Cameron Laird

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

                In article <1134171597.484 596.147510@f14g 2000cwb.googleg roups.com>,
                Dan Bishop <danb_83@yahoo. com> wrote:[color=blue]
                >Cameron Laird wrote:
                >...[color=green]
                >> 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.[/color]
                >
                >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
                >[/color]

                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 miscomprehensio n 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 misunderstandin g.

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

                Comment

                Working...