Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
code help
#8
There are lot of accusations that programming sites are very hostile places. To prove that this is not the case and there are actually friendly and fun-loving people I provide following solution (with caution).

Caution: there are times when one needs 'blue-pill' which will make all problems go away. One should be aware of the toll such a pill takes. Following is for situation: 'this-is-not-homework-but-I-am-not-allowed-to-use-built-in-tools-and-there-are-some-mandatory-expressions-I-have-to-use-but-it-is-still-not-homework'. Use responsibly and avoid overdose.

Following monty-liner delivers number of days from start of year with taking account actual leap years (not imaginary ones which happen on every fourth year):

>>> date = '13.06.2019'
>>> day, month, year = (int(x) for x in date.split('.'))
>>> sum([[31, 30][i <= 6 and i % 2 == 0 or i > 7 and i % 2 != 0] if i != 2 else [28, 29][year % 4 == 0 and year % 100 != 0 or year % 400 == 0] for i in range(1 ,13)][:month-1] + [day])
164 
As one can see all the 'work' is done on one line using list comprehension, conditional expressions, some built-in functions and operators, indexing and slicing. No fancy calendar module etc. No need for data structures or subroutines (and yes, this is actual working Python code which delivers expected result)
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


Messages In This Thread
code help - by callanlove - Jun-13-2019, 09:41 AM
RE: code help - by perfringo - Jun-13-2019, 10:38 AM
RE: code help - by callanlove - Jun-13-2019, 10:45 AM
RE: code help - by perfringo - Jun-13-2019, 10:51 AM
RE: code help - by kotter - Jun-13-2019, 11:47 AM
RE: code help - by buran - Jun-13-2019, 12:32 PM
RE: code help - by jefsummers - Jun-13-2019, 01:26 PM
RE: code help - by perfringo - Jun-13-2019, 02:05 PM
RE: code help - by jefsummers - Jun-13-2019, 03:13 PM
RE: code help - by perfringo - Jun-13-2019, 04:15 PM

Forum Jump:

User Panel Messages

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