[Tutor] Help with range of months spanning across years

Hugo Arts hugo.yoshi at gmail.com
Wed Feb 2 03:55:25 CET 2011


On Wed, Feb 2, 2011 at 2:30 AM, ian douglas <ian.douglas at iandouglas.com> wrote:
> It bugs me that so many people are quick to jump on the "we wont' do your
> homework" bandwagon -- I was accused of the same thing when I posted a
> question to the list myself. I've been programming professionally for many
> years but learning Python in my spare time... I sent this reply to Sean
> privately, but frankly I'm so annoyed at the 'homework' replies, I figured
> I'd just post it here.
>

Okay, this has gotten rather long, but I'll post it anyway because I
think the "we won't do your homework" response is valid and there's
good reasoning behind it.

It's not just homework. The thing is, the point of this list is to
teach people how to program (in general, but in python specifically).
When someone posts a question, responding with a lump of code and a
crisp "This is how you do it" just isn't a very effective teaching
method. More of the equivalent of giving a man a fish, as in the old
saying.

Another point, mentioned in Eric Raymond's "How to ask questions the
smart way"[1], is that I generally dislike answering questions for
people who don't appear to have put in any work in solving the problem
themselves. It leaves us with little options to give pointers, and
we're stuck with the lump of code mentioned above, or a few vague
hints as to how to approach the problem.

This isn't a place that solves your coding problems for free. But I'm
happy to help you learn python. For those reasons, I *never* give out
a straight answer, especially not to someone who doesn't show their
own attempts. In those cases, I will tell them that I won't and why,
give them some hints, and encourage them to try some things on their
own and get back here, at which point we can help them further. That's
what tutoring is all about after all.

So, in short, we're not "accusing" you of trying to make us do your
homework, and we're certainly not dismissing your questions. We're
simply saying "show us what you have, and we'll give you tips. if
you're totally at a loss, try doing so-and-so, or look at this and
that documentation."

[1]: http://www.catb.org/~esr/faqs/smart-questions.html, everybody
should read this before posting to a mailing list, imho.

> I'm still very junior to Python, but this seems to work for me using
> recursion. I'm sure there's a much more elegant way of doing this, but like
> I said, I'm still pretty new to the language.
>
> def makelist(startmonth,startyear,endmonth,endyear):
>     mylist = []
>     if (startyear == endyear):
>         for month in range (startmonth,endmonth+1):
>             mylist += [(startyear,month)]
>     else:
>         for month in range (startmonth,13):
>             mylist += [(startyear,month)]
>         mylist += makelist(1,startyear+1, endmonth, endyear)
>     return mylist ;
>
> print makelist(8,2009,1,2010)
>
> I'd love to hear any replies from the experts on the list on how to make
> this more efficient in Python 'cause I'm still learning myself.
>

Your solution feels rather lispy to me. I really like that. I actually
had a simple iterative solution in mind when I wrote that first post,
but while trying to write it now I'm running into some complications I
hadn't considered before, mea culpa (I'm such a good armchair
programmer </sarcasm>)

In any case, you can replace the for loops with a list comprehensions,
and factor them out into a single one since they're so similar. Let me
write it out:

def datelist(startyear, startmonth, endyear, endmonth):
    em = endmonth + 1 if startyear == endyear else 13
    dates = [(startyear, m) for m in range(startmonth, em)]
    if startyear < endyear:
        dates += datelist(startyear + 1, 1, endyear, endmonth)
    return dates

>>> print(datelist(2008, 8, 2009, 1))
[(2008, 8), (2008, 9), (2008, 10), (2008, 11), (2008, 12), (2009, 1)]

Hugo


More information about the Tutor mailing list