Comment detail

LL Golf Hole 3 - 13日の金曜日を数え上げる (Nested Flatten)

 素直に書いてみましたが,長いですね。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import    java.util.Calendar
import    java.text.SimpleDateFormat

class Friday13(from:Calendar) {
    
    var    f13:List[Calendar] = null
    val    to:Calendar = Calendar.getInstance
    to.setTime((new SimpleDateFormat("yyyy/MM/dd")).parse("2014/01/01"))
    
    def this() = this(Calendar.getInstance)
    
    def search:List[Calendar] = {
        val    d:Calendar = from.clone.asInstanceOf[Calendar]
        if (d.get(Calendar.DAY_OF_MONTH) > 13) {
            d.set(Calendar.DAY_OF_MONTH,1)
            d.add(Calendar.MONTH,1)
        }
        def search(l:List[Calendar],d:Calendar,to:Calendar):List[Calendar] = {
            d.before(to) match {
                case false => l
                case _ => {
                    val    n = d.clone.asInstanceOf[Calendar]
                    n.add(Calendar.MONTH,1)
                    (d.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) match {
                        case true => search(l+d,n,to)
                        case _ => search(l,n,to)
                    }
                }
            }
        }
        d.set(Calendar.DAY_OF_MONTH,13)
        f13 = search(List(),d,to)
        f13
    }
    
    def print:Unit = {
        def print(l:List[Calendar]):Unit = l match {
            case List() => ()
            case d::rest => {
                Console.printf("%04d/%02d/%02d\n",d.get(Calendar.YEAR),d.get(Calendar.MONTH) + 1,d.get(Calendar.DAY_OF_MONTH))
                print(rest)
            }
        }
        print(f13)
        Console.printf("%dday matched\n",f13.size)
    }
}

object Main extends Application {
    try {
        val    f:Friday13 = new Friday13(Calendar.getInstance)
        f.search
        f.print
    } catch {
        case e:Exception => e.printStackTrace
    }
}

Index

Feed

Other

Link

Pathtraq

loading...