• (cs) in reply to Rob
    Anonymous:
    months[z]=x%12 works in one line.


    months[z] = x % 12 is off-by-one
    Perhaps months[z] = (x -1) % 12 + 1 is what you were after? But gross...  if () statement might be easier to read.
  • (cs) in reply to bat
    bat:
    A couple of the comments in reply to this had off-by-one errors (which I also call Obi-Wan errors -- "these are not the integers you're looking for"), but never mind that.

    I'm just wondering: is there a language in common use that allows automagic caching of immutable values?  So you'd write something link this:

    immutable int months(int i)
    {
      return 1 + (i+6)%12;
    }


    ... and it would compile as something like this:

    int _months(int i)
    {
      static int _result[];
      if (!defined _result[i]) {
        _result[i] = 1 + (i+6)%12;
      }
      return _result[i];
    }


    That would be an interesting opportunity for optimisation.


    Oracle PL/SQL knows a deterministic keyword; this is especially usefull if a function is used in a sql-statement, like
    select somecolumn from a, b where mypkg.myfunction(a.somecolumn) = b.anothercolumn;
  • SgtPepper (unregistered) in reply to Rob
    Anonymous:
    Why would you use a while loop???  A for loop is designed to initialize variables within a counter and do your incrementing all in one line.  Plus, what is that if(x==12)?  months[z]=x%12 works in one line.

    I hope you were joking :P


    I wanted to keep in the 'theme' of the original code ;)  
  • anon (unregistered) in reply to Chris Brien

    #define MONTHS_PER_YEAR 12

    // [snip]

    // All numbers are 0-indexed
    int AddMonths(const int nStartMonth, const int ncMonths)
    {
        return( (nStartMonth + ncMonths) % MONTHS_PER_YEAR );
    }

    And, voila! No magic numbers, no OB1 errors (as far as I can tell), and it's even const-correct.

    Although chances are your OS/language may already have functions to do this for you.

  • (cs) in reply to Taryn East

    Anonymous:
    Le Poete:
    Actually talking about school, they often have applications where calendar year starts in July or August, as this is the normal start of a school year.  I'm working in a school board and we actually have to display a full year calendar that way.


    Ahem... normal if you are in the US, maybe, but everywhere else in the world (AFAIK) normally has their calendar start at the beginning of the year... schools included. Please be aware of US regionalisms when making sweeping statements.

    I'm actually in Canada.  Close enough to US, but not it.  I think it's probably most nothern hemisphere's countries which have a cold winter time between October and April that have the "August to July" calendar for schools.  I could easily admit equator and southern hemisphere's countries doing differently.  To goal here is to enjoy the warmest time of the year in vacation, which is June-September in most northern hemisphere's countries.

  • The Centaur (unregistered) in reply to loneprogrammer
    loneprogrammer:
    Alex Papadimoulis:

    int[] months = { 7, 8, 9, 10, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6 };


    In Perl, this can be

    @months = (7..12, 1..12, 1..6);

    In J, this can be:

    6|.24$1+i.12

    The APL would be similar: 6{shift}24{shape}1+{range}12

    -The Centaur

  • brazzy (unregistered) in reply to Taryn East
    Anonymous:

    Ahem... normal if you are in the US, maybe, but everywhere else in the world (AFAIK) normally has their calendar start at the beginning of the year... schools included. Please be aware of US regionalisms when making sweeping statements.


    Actually, pretty much anywhere in the world, corporations have their "fiscal year" begin just about anywhere EXCEPT January 1st. And I very much doubt that schools having their calender start at the beginning of the SCHOOL YEAR is in any way US-specific.
  • asqui (unregistered) in reply to SgtPepper

    Anonymous:
    Hey all, new here *waves*  someone posted a one for loop to do this, but assuming you wanted to stay with a while loop, this does the trick as well:

    int[] months = new int[24];
    int x = 7;
    int z = 0;
    while ( z != 24 )
    {
        months[z] = x;
        if(x == 12)
        {
            x = 0;
        };
       
        x = x + 1;
        z = z + 1;

     }

     

    <FONT face=Arial>Any <FONT face="Courier New">for</FONT> loop can be translated into a <FONT face="Courier New">while</FONT> loop: move the declaration out in front, put the increment statement at the end of your loop, leave the condition where it is, and replace "for" with "while".</FONT>

    <FONT face=Arial>That transformation is pretty pointless in itself (a deterministic iteration from x to y makes more sense as a <FONT face="Courier New">for</FONT> loop). The only other difference I can see is that you've abandoned the modulo function in preference to that <FONT face="Courier New">if</FONT> statement.</FONT>

  • SgtPepper (unregistered) in reply to asqui
    Anonymous:

    Anonymous:
    Hey all, new here *waves*  someone posted a one for loop to do this, but assuming you wanted to stay with a while loop, this does the trick as well:

    int[] months = new int[24];
    int x = 7;
    int z = 0;
    while ( z != 24 )
    {
        months[z] = x;
        if(x == 12)
        {
            x = 0;
        };
       
        x = x + 1;
        z = z + 1;

     }

     

    <font face="Arial">Any <font face="Courier New">for</font> loop can be translated into a <font face="Courier New">while</font> loop: move the declaration out in front, put the increment statement at the end of your loop, leave the condition where it is, and replace "for" with "while".</font>

    <font face="Arial">That transformation is pretty pointless in itself (a deterministic iteration from x to y makes more sense as a <font face="Courier New">for</font> loop). The only other difference I can see is that you've abandoned the modulo function in preference to that <font face="Courier New">if</font> statement.</font>



    My only point for redoing it as a while loop was to make the assertation that if you were going to do it this way (considering that was the original author's state of mind), there was /still/ a much more simpler intuitive way of doing it. In other words, even if the original author was convinced it had to be done in a while loop with an if statement, it could still have been done in one loop instead of three.
  • (cs) in reply to Le Poete

    Le Poete:
     I think it's probably most nothern hemisphere's countries which have a cold winter time between October and April that have the "August to July" calendar for schools. 

    Most people here seem to be missing the point of the original complaint.

    Someone said that their school had a "Calendar year" starting in August. 

    "Calandar Years" start in January. Period.

    If you have a 12-month period starting on any month beside January, you need to use a different adjective with it.

    Like "School Year"

    Or "Fiscal Year"

    Or "Year of Linving Dangerously"

     

  • mcbain (unregistered) in reply to anon

    Up and AT THEM!

  • cm (unregistered)

    Interesting that people pointed out the one-liner's problems.  I submitted the wtf with the explanation for what it did, but I didn't go into the context at all.  The first thing to note is that this was for a financial application where the budget is biennal running from July to June, hence the 7.  The other, more interesting and yet more WTF!?! thing to know is that this code turned out to only generate the numbers in order to populate a static label in a GUI component.  When I finally figured out the context, I just went to the GUI and put in the numbers in the form, and deleted the static array completely.

    I did ask, there was some discussion about a desire to dynamically generate the months, but nothing else required it and it was merely a "nice to have" anyway.

  • (cs) in reply to Anonymous Coward

    Anonymous:
    www.expertsexchange.com

    The address to this website has changed to http://www.experts-exchange.com [:D]

  • cake aka your mom (unregistered) in reply to Taryn East

    ahem...last time i checked school starts around september and lasts untill about june.
    world wide.
    Please take note that the US also starts their calandar 'at the begining of the year' which you have so blatently pointed out to us.8-)
    Thanks for your helpful information.

  • asqui (unregistered) in reply to cake aka your mom

    <FONT face=Arial>I guess your definition of "world wide" doesn't include continents such as Australia.</FONT>

  • polarbear (unregistered) in reply to asqui

    yeah. thats a WTF in itself

  • Swannie (unregistered) in reply to Rob

    Wow, I hope to god I am never in the position of hiring many of you. (Ahh, ok, you're not as bad as some of my Indian colleagues...)

    With regards to:

    months[z]=x%12

    Thinking this was nice code was a mistake I made in my first 3 months of Java programming. As it is Java, it doesn't really matter, but % is an expensive divide operation. If (x == 13) x=1 will pretty much always be faster than a divide.

    Thankfully I had good tutors that explained why % is a fairly unneeded and wasteful in this situation. (Any why in Java it doesn't really matter so much, as it really depends on the implementation of the JVM.)

    Swannie

    (BTW:

    void monthNumbers ( int[] months, int monthsLength, int start ) {
        for (int monthNumber = start , int arrayIndex = 0; arrayIndex < monthsLength; monthsNumber++ , arrayIndex++  ) {
            if (monthNumber == 13 ) {
                monthNumber = 1;
           }
           months [ arrayIndex ] = monthNumber;
        }
    }

    might not be 100% correct, but you get the idea)

  • Swannie (unregistered) in reply to Rob

    Wow, I hope to god I am never in the position of hiring many of you. (Ahh, ok, you're not as bad as some of my Indian colleagues...)

    With regards to:

    months[z]=x%12

    Thinking this was nice code was a mistake I made in my first 3 months of Java programming. As it is Java, it doesn't really matter, but % is an expensive divide operation. If (x == 13) x=1 will pretty much always be faster than a divide.

    Thankfully I had good tutors that explained why % is a fairly unneeded and wasteful in this situation. (Any why in Java it doesn't really matter so much, as it really depends on the implementation of the JVM.)

    Swannie

    (BTW:

    void monthNumbers ( int[] months, int monthsLength, int start ) {
        for (int monthNumber = start , int arrayIndex = 0; arrayIndex < monthsLength; monthsNumber++ , arrayIndex++  ) {
            if (monthNumber == 13 ) {
                monthNumber = 1;
           }
           months [ arrayIndex ] = monthNumber;
        }
    }

    might not be 100% correct, but you get the idea)

  • Dagur (unregistered) in reply to Swannie

    Let me be the first to do it in python (not that anyone cares anymore)


    [1 + (i + 6)%12 for i in range(24)]

  • Micky (unregistered)

    OK, I know I'm a bit late, but this is something that reminds me of an old computer game called "The Incredible Machine", maybe it's is developed by the very same company!!!!

  • nb (unregistered) in reply to asqui
    asqui:
    I guess your definition of "world wide" doesn't include continents such as Australia.

    maybe that is because Australia isn't a continent?

  • Andrea Raimondi (unregistered) in reply to Dave Lewis

    I really wonder what use has that!

    I mean, what's wrong with using a plain calendar?

    Maybe it's just me, but if I was to code a school year management application, I would rely on a timeline rather than a custom calendar :P

    I can understand computing the first school day, that actually makes sense, but then just use that on a plain calendar - why doing something else?

    Am I missing anything?

    Andrew

  • G (unregistered) in reply to CornedBee

    Not sure I agree with compressing the operation into a single line. In terms of readability and ease of maintenance, I'd prefer to write out the three operations separately (though I agree with using ++ syntax). Especially as either way, the program will still be executing the same instructions, so there's no gain in efficiency.

    months[i] = cnt; ++cnt; ++i;

    Also, pretty sure Australia is a continent...

Leave a comment on “... wait ... that's it?!?”

Log In or post as a guest

Replying to comment #:

« Return to Article