Comment detail

除算・余剰を使わずに閏年 (Nested Flatten)
組み込み「メソッド」は使ってもいいのかな。
(でないとRuby/Smalltalkあたりは解答のしようが無いし)
1
2
3
4
5
6
7
function doukaku124a(y){
  return new Date(y, 1, 29).getMonth() < 2;
}

function doukaku124b(y){
  return !((y +'').replace(/00$/, '') & 3);
}

その手がありましたか!って、もはや悪のりでしかありませんね(^_^;)。

1
2
3
4
5
6
7
8
| 閏年か? |

閏年か? := [:int | int asYear daysInYear = 366].

閏年か? value: 1900.   "=> false "
閏年か? value: 2000.   "=> true "
閏年か? value: 2008.   "=> true "
閏年か? value: 2100.   "=> false "
そうか、その手が。。。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:fn="http://www.w3.org/2005/xpath-functions"
  >

  <xsl:output method="text" />

  <xsl:template match="/" >
    <xsl:for-each select="1900 to 2100">
      <xsl:variable name="thisYear" as="xs:date"
        select="xs:date(fn:concat(xs:string(.), '-01-01'))" />
      <xsl:variable name="nextYear" as="xs:date"
        select="xs:date(fn:concat(xs:string(1+.), '-01-01'))" />
      <xsl:if test="fn:days-from-duration($nextYear - $thisYear)=366">
        <xsl:value-of select="." />
        <xsl:text> 年は閏年♪&#xA;</xsl:text>
      </xsl:if>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>

Dateクラスをそのまま使うというのは思いつきませんでした。コロンブスの卵すぎる・・・!

一応正攻法も載せておきますね。

1
2
3
4
5
6
7
is.leap.year1 <- function(y){
    !is.na(ISOdate(y,2,29))
}

is.leap.year2 <- function(y){
    y %in% union(setdiff(seq(0,y,4), seq(0,y,100)), seq(0,y,400))
}

Index

Feed

Other

Link

Pathtraq

loading...