Looping The Alphabet and Numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pepsimax
    New Member
    • Mar 2008
    • 1

    Looping The Alphabet and Numbers

    Hey,

    I want to create a list of letters, starting at AA1 to ZZ15.

    IE,

    AA1, AA2, AA3, AA4, AA5, AA6, AA7, AA8, AA9, AA10, AA11, AA12, AA13, AA14, AA15, AB1, AB2 etc etc.

    I am familiar with loop processes but can't get my head around the logic to this one.

    Help appreciated. Thanks.
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    #2
    [php]<?php
    $abc = array ("A","B","C","D ","E",.../*upto*/.....,"X","Y"," Z");
    $output = array();
    for ($i=0; $i<26; $i++)
    {
    for ($j=0; $j<26; $j++)
    {
    for ($k=1; $k<=15; $k++)
    {
    array_push($out put, $abc[$i].$abc[$j].$k);
    }
    }
    }
    ?>[/php]

    See if the $output array give you the right thing.
    Last edited by hsriat; Mar 2 '08, 07:28 AM. Reason: not 25, but 26

    Comment

    • dlite922
      Recognized Expert Top Contributor
      • Dec 2007
      • 1586

      #3
      [PHP]
      <?php
      $number = 15;
      $result = array();
      $alpha = array('A','B',' C','D','E','F', 'G','H','I','J' ,'K','L','M','N ','O','P','Q',' R','S','T','U', 'V','W','X','Y' ,'Z');
      for ($first = 0; $first < count($alpha); $first++) {
      for ($second = 0; $second < count($alpha); $second++) {
      for ($k = 1; $k <= $number; $k++) {
      array_push($res ult,$alpha[$first].$alpha[$second].$k);
      }
      }
      }
      print_r($result ); // here's the result, do what you want with it. 10139 Results!!!
      ?>

      [/PHP]

      let me know if any questions.

      Comment

      • dlite922
        Recognized Expert Top Contributor
        • Dec 2007
        • 1586

        #4
        damn. you beat me to it!

        :P mines better!!

        /joke, its almost exactly the same.

        then again, i don't know how else you can do this.

        Comment

        • hsriat
          Recognized Expert Top Contributor
          • Jan 2008
          • 1653

          #5
          Originally posted by dlite922
          damn. you beat me to it!

          :P mines better!!

          /joke, its almost exactly the same.

          then again, i don't know how else you can do this.
          Out of two code doing the same thing, the one having less number of lines is better. Going to the depth, you will find mine will consume less memory and machine cycles. :p
          (j/k)

          But this used to be thought many years ago, when memory and CPU speed were limited. Now, no one cares!, neither its required to care, unless its Embedded C or J2ME.

          :)

          Harpreet

          Comment

          • dlite922
            Recognized Expert Top Contributor
            • Dec 2007
            • 1586

            #6
            Originally posted by hsriat
            Out of two code doing the same thing, the one having less number of lines is better. Going to the depth, you will find mine will consume less memory and machine cycles. :p
            (j/k)

            But this used to be thought many years ago, when memory and CPU speed were limited. Now, no one cares!, neither its required to care, unless its Embedded C or J2ME.

            :)

            Harpreet
            good point! but I have a better one.

            (Sorry for being so competitive, its 1:00 AM and i'm bored)

            Better was in the definition that it was easier for him to understand, and even more importantly that a code should be extensible.

            As you can see in yours if i choose to drop out letters, the 25 is hardcoded as well as the 15 where in mine you can change those values in their declaration and my code does not have to be changed.

            PS: Also i'm trying to knock "dafodil" off the board ;) ....sshhh!! don't tell him I said that.

            Comment

            • hsriat
              Recognized Expert Top Contributor
              • Jan 2008
              • 1653

              #7
              I hard coded it to 26 (later edited form 25), coz there are 26 English alphabets. :D

              Ok, your is better. Happy now?

              (It's 1:00 PM here and I have many other things to do) :p

              Regards,
              Harpreet :)

              Comment

              Working...