Python Forum
List with "or" value?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List with "or" value?
#1
I'm having trouble with an assignment about leap years. I had a basic (ugly) code that did the job, however when I asked for feedback from the teacher he sent me a corrected version of my code, asking for a list with the number of days in the months with an option between 28 and 29 for february.
What I had initially done was lump all 30 days months together in a list, lump 31 days months in another and leave february alone. But here he's asking me to do something like
days_in_months = [31, 28 or 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
and I have no clue how to create such a list or even worse how to access it later... how do I call for 28 or 29?
days_in_months[1]???
I've tried looking it up on the internet but I guess I'm not using the right keywords since I couldn't find anything relevant.
Reply
#2
def get_days_in_months(leap_year=False):
    feb = 29 if leap_year else 28
    return [31, feb, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
My personal experience with assignments is that they teach you solve problems with tools you currently studying and not in creative or effective way. It often leads to bad habits in solving problems.

Why would I invent a wheel when there are cars? I would write utility function to get number of days in month (of course, one can use monthrange directly) using built-in calendar module:

import calendar

def days_in_month(y, m):
    return calendar.monthrange(y, m)[1]
Calendar is 'battaries included' module and there is function monthrange:

Quote:calendar.monthrange(year, month)
Returns weekday of first day of the month and number of days in month, for the specified year and month.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
Thanks Dead_Eye, I hadn't thought of that.

@perfringo: we're not allowed to use the calendar module, we have to do it "manually" so yeah, we're asked to invent the wheel although cars exist. I'm not against that, it's just that I really had no clue how to do it.
Reply
#5
(Jul-21-2018, 01:16 AM)WolfWayfarer Wrote: @perfringo: we're not allowed to use the calendar module, we have to do it "manually" so yeah, we're asked to invent the wheel although cars exist. I'm not against that, it's just that I really had no clue how to do it.

If it's all about not using proper tools then I would suggest to blow your teachers mind with following Monty-liner*:

year = 2014
months = sum([[[31, 30][i <= 6 and i % 2 == 0 or i > 7 and i % 2 != 0] if i / 2 != 1 else [28, 29][year % 4 == 0]] for i in range(1, 13)], [])
months value will be:

Output:
[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
it works with leap years, so year = 2016 value will be:

Output:
[31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
* Monty is Python's evil twin and his mission is to oppose Zen of Python whenever possible and abuse Python syntax.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
(Jul-23-2018, 08:13 AM)perfringo Wrote: If it's all about not using proper tools then I would suggest to blow your teachers mind with following Monty-liner*:

year = 2014
months = sum([[[31, 30][i <= 6 and i % 2 == 0 or i > 7 and i % 2 != 0] if i / 2 != 1 else [28, 29][year % 4 == 0]] for i in range(1, 13)], [])
* Monty is Python's evil twin and his mission is to oppose Zen of Python whenever possible and abuse Python syntax.

Big Grin it reminded me of code-obfuscation contests.
Reply
#7
(Jul-23-2018, 09:05 AM)WolfWayfarer Wrote: Big Grin it reminded me of code-obfuscation contests.

Yeah, I am quite Monty about it Smile

Nevertheless, it is basically very simple conditional expression and list comprehension:


[[days_in_month if not Feburary else days_in_february] for monthnum in year]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020