Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #78398 > unrolled thread
| Started by | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| First post | 2014-09-26 23:49 -0400 |
| Last post | 2014-09-29 08:05 -0700 |
| Articles | 7 — 6 participants |
Back to article view | Back to comp.lang.python
Leap year Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-09-26 23:49 -0400
Re: Leap year Gene Heskett <gheskett@wdtv.com> - 2014-09-27 00:08 -0400
Re:Leap year Dave Angel <davea@davea.name> - 2014-09-27 05:07 -0400
Re: Leap year Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-09-28 16:22 +1000
Re: Leap year Rustom Mody <rustompmody@gmail.com> - 2014-09-28 23:58 -0700
Re: Leap year wxjmfauth@gmail.com - 2014-09-29 01:27 -0700
Re: Leap year wxjmfauth@gmail.com - 2014-09-29 08:05 -0700
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2014-09-26 23:49 -0400 |
| Subject | Leap year |
| Message-ID | <vocc2a17rl5odoasq7ae317aju8de9ejcn@4ax.com> |
Still practicing. Since this is listed as a Pseudocode, I assume this
is a good way to explain something. That means I can also assume my
logic is fading with age.
http://en.wikipedia.org/wiki/Leap_year#Algorithm
Me trying to look at the algorithm, it would lead me to try something
like:
if year % 4 !=0:
return False
elif year % 100 !=0:
return True
elif year % 400 !=0:
return False
**** Since it is a practice problem I have the answer:
def is_leap_year(year):
return ((year % 4) == 0 and ((year % 100) != 0 or (year % 400) == 0))
I didn't have any problem when I did this:
if year % 400 == 0:
print ("Not leap year")
elif year % 100 == 0:
print ("Leap year")
elif year % 4 == 0:
print ("Leap year")
else:
print ("Not leap year")
[toc] | [next] | [standalone]
| From | Gene Heskett <gheskett@wdtv.com> |
|---|---|
| Date | 2014-09-27 00:08 -0400 |
| Message-ID | <mailman.14410.1411791330.18130.python-list@python.org> |
| In reply to | #78398 |
On Friday 26 September 2014 23:49:43 Seymore4Head did opine
And Gene did reply:
> Still practicing. Since this is listed as a Pseudocode, I assume this
> is a good way to explain something. That means I can also assume my
> logic is fading with age.
> http://en.wikipedia.org/wiki/Leap_year#Algorithm
>
> Me trying to look at the algorithm, it would lead me to try something
> like:
> if year % 4 !=0:
> return False
> elif year % 100 !=0:
> return True
> elif year % 400 !=0:
> return False
>
> **** Since it is a practice problem I have the answer:
> def is_leap_year(year):
> return ((year % 4) == 0 and ((year % 100) != 0 or (year % 400) == 0))
>
> I didn't have any problem when I did this:
>
> if year % 400 == 0:
> print ("Not leap year")
> elif year % 100 == 0:
> print ("Leap year")
> elif year % 4 == 0:
> print ("Leap year")
> else:
> print ("Not leap year")
Which is, except for language syntax to state it, exactly the same as is
quoted for this problem in the original K&R C manual. Is there anything
new?
Cheers, Gene Heskett
--
"There are four boxes to be used in defense of liberty:
soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author)
Genes Web page <http://geneslinuxbox.net:6309/gene>
US V Castleman, SCOTUS, Mar 2014 is grounds for Impeaching SCOTUS
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2014-09-27 05:07 -0400 |
| Message-ID | <mailman.14420.1411808694.18130.python-list@python.org> |
| In reply to | #78398 |
Seymore4Head <Seymore4Head@Hotmail.invalid> Wrote in message:
> Still practicing. Since this is listed as a Pseudocode, I assume this
> is a good way to explain something. That means I can also assume my
> logic is fading with age.
> http://en.wikipedia.org/wiki/Leap_year#Algorithm
>
<cut>
>
> I didn't have any problem when I did this:
>
> if year % 400 == 0:
> print ("Not leap year")
> elif year % 100 == 0:
> print ("Leap year")
> elif year % 4 == 0:
> print ("Leap year")
> else:
> print ("Not leap year")
>
But the first two prints are incorrect, so I'm not sure what you
mean by "didn't have any problem."
Incidentally, it's usually easier to test a function that does one
thing. So if you replace all those prints with appropriate return
True or return False, then it's easy to exhaustively compare two
variations.
--
DaveA
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2014-09-28 16:22 +1000 |
| Message-ID | <5427a919$0$12982$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #78398 |
Seymore4Head wrote:
> Still practicing. Since this is listed as a Pseudocode, I assume this
> is a good way to explain something. That means I can also assume my
> logic is fading with age.
> http://en.wikipedia.org/wiki/Leap_year#Algorithm
>
> Me trying to look at the algorithm, it would lead me to try something
> like:
> if year % 4 !=0:
> return False
> elif year % 100 !=0:
> return True
> elif year % 400 !=0:
> return False
You can only have the "return" statement inside a function, not in top-level
code, and that looks like top-level code. So you need to start with
something like:
def is_leapyear(year):
if year % 4 != 0:
return False
...
> **** Since it is a practice problem I have the answer:
> def is_leap_year(year):
> return ((year % 4) == 0 and ((year % 100) != 0 or (year % 400) == 0))
Or in English:
- years which are divisible by 4 are leap years, *unless* they are also
divisible by 100, in which case they aren't leap years, *except* that years
divisible by 400 are leap years.
Converting to Python code:
def is_leap_year(year):
return (
# Years which are divisible by 4
(year % 4 == 0)
# and not divisible by 100
and (year % 100 != 0)
# unless divisible by 400
or (year % 400 == 0)
)
> I didn't have any problem when I did this:
>
> if year % 400 == 0:
> print ("Not leap year")
> elif year % 100 == 0:
> print ("Leap year")
> elif year % 4 == 0:
> print ("Leap year")
> else:
> print ("Not leap year")
You might not have had any problems, but neither did you get the right
answers. According to your code, 2000 is not a leap year (since 2000 % 400
equals 0) but in reality it is. On the other hand, your code says 1900 was
a leap year, which it was not.
The lesson here is, code isn't working until you've actually tested it, and
to test it sufficiently you need to check the corner cases, not just the
obvious ones.
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Rustom Mody <rustompmody@gmail.com> |
|---|---|
| Date | 2014-09-28 23:58 -0700 |
| Message-ID | <72778568-0205-47c5-8b60-163b09823170@googlegroups.com> |
| In reply to | #78398 |
On Saturday, September 27, 2014 9:21:15 AM UTC+5:30, Seymore4Head wrote:
> Still practicing. Since this is listed as a Pseudocode, I assume this
> is a good way to explain something. That means I can also assume my
> logic is fading with age.
> http://en.wikipedia.org/wiki/Leap_year#Algorithm
> Me trying to look at the algorithm, it would lead me to try something
> like:
> if year % 4 !=0:
> return False
> elif year % 100 !=0:
> return True
> elif year % 400 !=0:
> return False
> **** Since it is a practice problem I have the answer:
> def is_leap_year(year):
> return ((year % 4) == 0 and ((year % 100) != 0 or (year % 400) == 0))
> I didn't have any problem when I did this:
> if year % 400 == 0:
> print ("Not leap year")
> elif year % 100 == 0:
> print ("Leap year")
> elif year % 4 == 0:
> print ("Leap year")
> else:
> print ("Not leap year")
Python has an if-expression distinct from the if-statement.
However its confusing because its order is 'upside down'
So below I use C's if-expression a?b:c to state some 'laws' of programming and leave it as an exercise to pythonify them
a?T:F ≡ a A
a?F:T ≡ not a B
a?T:b ≡ a or b C
a?b:F ≡ a and b D
if p: E
return x
else:
return y
≡
return (p ? x : y)
Likewise
if p F
print x
else
print y
≡
print (p ? x : y)
----------------------------
Now putting:
a ≜ y%4==0
b ≜ y%100!=0
c ≜ y%400 == 0
the expression that is the (given) answer is
a and (b or c)
Lets use the above laws to open it up
"by C"
a and (b ? T: c)
"by D"
a?(b?T:c):F
year%4==0 ? (y%100!=0 ? T: y%400==0) : F
--------------------------
Now lets take your version:
if year % 400 == 0:
print ("Not leap year")
elif year % 100 == 0:
print ("Leap year")
elif year % 4 == 0:
print ("Leap year")
else:
print ("Not leap year")
And now 'de-print' it [A good idea though I wont suggest it to you again!]
print (!c ? F : (b? T : (a? T : F)))
Forget about the print since its irrelevant and concentrate on
(!c ? F : (b? T : (a? T : F)))
"by A"
= (!c ? F : (b? T : a))
"by B"
= (c ? (b? T : a) : F)
"by D"
= (c and (b?T:a)
"by C"
= c and (b or a)
Lets re-substitute a,b,c
= y%400==0 and (y%100 !=0 or y%4 == 0)
which interpreted says that ONLY years divisible by 400 are leap
and not even all those!!
[toc] | [prev] | [next] | [standalone]
| From | wxjmfauth@gmail.com |
|---|---|
| Date | 2014-09-29 01:27 -0700 |
| Message-ID | <d7e02a2b-d0df-4c4c-8ea5-b276d65452e4@googlegroups.com> |
| In reply to | #78447 |
------ >>> def z(y): ... if y <= 1582: # or < ... return 'not a leap year, Julian calendar' ... else: ... return 'process, Gregorian calendar' ... >>> z(1300) 'not a leap year, Julian calendar' >>> z(2014) 'process, Gregorian calendar' >>>
[toc] | [prev] | [next] | [standalone]
| From | wxjmfauth@gmail.com |
|---|---|
| Date | 2014-09-29 08:05 -0700 |
| Message-ID | <975bd109-3a7e-4bbc-bd93-6f0940995775@googlegroups.com> |
| In reply to | #78447 |
Le lundi 29 septembre 2014 08:59:11 UTC+2, Rustom Mody a écrit : > > Python has an if-expression distinct from the if-statement. > However its confusing because its order is 'upside down' > Ditto for Unicode. Python is just doing the opposite of the implementation of the utf coding schemes endorsed by unicode. On the side of memory (very easy to check, performance (a little bit harder) and plenty of specific unicode tasks like normalization. ;-)
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web