do/while structure needed

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Salerno

    #1

    do/while structure needed

    1 random.shuffle( letters)
    2 trans_letters = ''.join(letters )[:len(original_s et)]
    3 trans_table = string.maketran s(original_set, trans_letters)

    So what I'd like to do is have lines 1 and 2 run once, then I want to do
    some comparison between original_set and trans_letters before running
    line 3. If the comparison passes, line 3 runs; otherwise, lines 1 and 2
    run again.

    A do/while would be good for this, but perhaps I'm looking at it in the
    wrong way? Or is there some kind of do/while type of idiom that I could use?

    Thanks.
  • George Sakkis

    #2
    Re: do/while structure needed

    John Salerno wrote:
    [color=blue]
    > 1 random.shuffle( letters)
    > 2 trans_letters = ''.join(letters )[:len(original_s et)]
    > 3 trans_table = string.maketran s(original_set, trans_letters)
    >
    > So what I'd like to do is have lines 1 and 2 run once, then I want to do
    > some comparison between original_set and trans_letters before running
    > line 3. If the comparison passes, line 3 runs; otherwise, lines 1 and 2
    > run again.
    >
    > A do/while would be good for this, but perhaps I'm looking at it in the
    > wrong way? Or is there some kind of do/while type of idiom that I could use?
    >
    > Thanks.[/color]

    I guess you want something like:

    while True:
    random.shuffle( letters)
    trans_letters = ''.join(letters )[:len(original_s et)]
    if some_compatison (original_set,t rans_letters):
    trans_table = string.maketran s(original_set, trans_letters)
    break


    HTH,
    George

    Comment

    • Ten

      #3
      Re: do/while structure needed

      On Sunday 14 May 2006 06:17, John Salerno wrote:[color=blue]
      > 1 random.shuffle( letters)
      > 2 trans_letters = ''.join(letters )[:len(original_s et)]
      > 3 trans_table = string.maketran s(original_set, trans_letters)
      >
      > So what I'd like to do is have lines 1 and 2 run once, then I want to do
      > some comparison between original_set and trans_letters before running
      > line 3. If the comparison passes, line 3 runs; otherwise, lines 1 and 2
      > run again.
      >
      > A do/while would be good for this, but perhaps I'm looking at it in the
      > wrong way? Or is there some kind of do/while type of idiom that I could
      > use?
      >
      > Thanks.[/color]

      while not comparison(orig inal_set, trans_letters):
      random.shuffle( letters)
      trans_letters = ''.join(letters )[:len(original_s et)]

      trans_table = string.maketran s(original_set, trans_letters)

      HTH,

      Ten

      --
      There are 10 types of people in this world,
      those who understand binary, and those who don't.

      Comment

      • Paul McGuire

        #4
        Re: do/while structure needed


        "Ten" <runlevelten@gm ail.com> wrote in message
        news:mailman.56 77.1147617486.2 7775.python-list@python.org ...[color=blue]
        > On Sunday 14 May 2006 06:17, John Salerno wrote:[color=green]
        > > 1 random.shuffle( letters)
        > > 2 trans_letters = ''.join(letters )[:len(original_s et)]
        > > 3 trans_table = string.maketran s(original_set, trans_letters)
        > >
        > > So what I'd like to do is have lines 1 and 2 run once, then I want to do
        > > some comparison between original_set and trans_letters before running
        > > line 3. If the comparison passes, line 3 runs; otherwise, lines 1 and 2
        > > run again.
        > >
        > > A do/while would be good for this, but perhaps I'm looking at it in the
        > > wrong way? Or is there some kind of do/while type of idiom that I could
        > > use?
        > >
        > > Thanks.[/color]
        >
        > while not comparison(orig inal_set, trans_letters):
        > random.shuffle( letters)
        > trans_letters = ''.join(letters )[:len(original_s et)]
        >
        > trans_table = string.maketran s(original_set, trans_letters)
        >[/color]

        I don't think the OP wants to call comparison until after the first pass
        through the loop. Here's a modification to your version that skips the
        comparison test on the first pass:

        first = True
        while first or not comparison(orig inal_set, trans_letters):
        first = False
        random.shuffle( letters)
        trans_letters = ''.join(letters )[:len(original_s et)]

        trans_table = string.maketran s(original_set, trans_letters)


        -- Paul


        Comment

        • John Salerno

          #5
          Re: do/while structure needed

          George Sakkis wrote:
          [color=blue]
          > while True:
          > random.shuffle( letters)
          > trans_letters = ''.join(letters )[:len(original_s et)]
          > if some_compatison (original_set,t rans_letters):
          > trans_table = string.maketran s(original_set, trans_letters)
          > break[/color]

          Thanks, that looks pretty good. Although I have to say, a do/while
          structure is the much more obvious way. I wonder why it hasn't been
          added to the language.

          Comment

          • Terry Reedy

            #6
            Re: do/while structure needed


            "John Salerno" <johnjsal@NOSPA Mgmail.com> wrote in message
            news:4467786c$0 $1956$c3e8da3@n ews.astraweb.co m...[color=blue]
            > Thanks, that looks pretty good. Although I have to say, a do/while
            > structure is the much more obvious way. I wonder why it hasn't been
            > added to the language.[/color]

            Been suggested many times, been considered, and rejected. Easily similated
            with while True: ... if exit_condition: break which also solves 'loop and a
            half' problem, which is more common. Requires new keyword for little gain.
            Etc.

            tjr



            Comment

            • John Salerno

              #7
              Re: do/while structure needed

              Terry Reedy wrote:[color=blue]
              > "John Salerno" <johnjsal@NOSPA Mgmail.com> wrote in message
              > news:4467786c$0 $1956$c3e8da3@n ews.astraweb.co m...[color=green]
              >> Thanks, that looks pretty good. Although I have to say, a do/while
              >> structure is the much more obvious way. I wonder why it hasn't been
              >> added to the language.[/color]
              >
              > Been suggested many times, been considered, and rejected. Easily similated
              > with while True: ... if exit_condition: break which also solves 'loop and a
              > half' problem, which is more common. Requires new keyword for little gain.
              > Etc.[/color]

              I know, I remember a discussion about it a while back. But IMO, at
              least, a do/while structure seems much cleaner and more readable than a
              while loop with an embedded if and break statement.

              Comment

              • Antoon Pardon

                #8
                Re: do/while structure needed

                Op 2006-05-15, Terry Reedy schreef <tjreedy@udel.e du>:[color=blue]
                >
                > "John Salerno" <johnjsal@NOSPA Mgmail.com> wrote in message
                > news:4467786c$0 $1956$c3e8da3@n ews.astraweb.co m...[color=green]
                >> Thanks, that looks pretty good. Although I have to say, a do/while
                >> structure is the much more obvious way. I wonder why it hasn't been
                >> added to the language.[/color]
                >
                > Been suggested many times, been considered, and rejected. Easily similated
                > with while True: ... if exit_condition: break which also solves 'loop and a
                > half' problem, which is more common. Requires new keyword for little gain.
                > Etc.[/color]

                It isn't rejected. PEP 315 is only deferred. Which as far as I
                understand means it is postponed because other things seemed
                more important.

                --
                Antoon Pardon

                Comment

                • John Salerno

                  #9
                  Re: do/while structure needed

                  Dennis Lee Bieber wrote:
                  [color=blue]
                  > And what syntax would you propose?[/color]

                  I guess something like:


                  do:
                  stuff goes here
                  and here
                  and here
                  while (condition)


                  The absence of a colon after the while statement can be the signal that
                  it isn't a regular while statement with a following block, but instead
                  the final statement of a do/while loop. Or would that cause an issue?

                  Comment

                  • Edward Elliott

                    #10
                    Re: do/while structure needed

                    Dennis Lee Bieber wrote:
                    [color=blue]
                    > Would you put the condition at the top of the loop -- and confuse
                    > those people who believe the exit condition should appear at the point
                    > the exit activates?[/color]

                    Confusion is not the issue. You can put the condition smack dab in the
                    middle of the loop, as long as the location is consistent people will know
                    where to look for it. Rejecting because "some are confused the first time
                    since other languages do it differently" gets you nowhere: some group will
                    always be confused by something. They learn, problem solved.

                    I agree that do/while is unnecessary. But I'd argue against it on grounds
                    of simplicity and readability instead of confusion.

                    --
                    Edward Elliott
                    UC Berkeley School of Law (Boalt Hall)
                    complangpython at eddeye dot net

                    Comment

                    Working...