F# - AOJ ITP1_1~ITP1_3

あんどーなつ
記事: 171
登録日時: 8年前
連絡を取る:

F# - AOJ ITP1_1~ITP1_3

投稿記事 by あんどーなつ » 8年前

CODE:

// テストの実行は、fsi上で#load "foo.fsx";;とします。
// 関数の呼び出しは、Foo.sayHello();;などとします。

// ITP1_1
let sayHello() = printfn "Hello, World"
let xCubic(x) = x*x*x
let rectangle(a, b) =
    (a*b, 2*(a+b))
let watch(S) =
    (S / (60*60), (S % (60*60)) / 60, S % 60)

// ITP1_2
let printComp(a, b) =
  if a  b then "a > b"
  else "a == b"
let isRange(a, b, c) = (a  b then (b, a, c) else (a, b, c)
  let bc(a, b, c) = if b > c then (a, c, b) else (a, b, c)
  ab(bc(ab(a, b, c)))
let inRect(W, H, x, y, r) =
  let inW(x) = isRange(0, x, W)
  let inH(y) = isRange(0, y, H)
  inW(x - r) && inW(x + r) && inH(y - r) && inH(y + r)

// ITP1_3
let printManyHelloWorld() =
  List.fold (fun x y -> x+y) ""
            [for s in [1..1000] -> "Hello, World\n"]
let printTestCases(lines) =
  let lns = List.takeWhile (fun x -> x  0) lines
  let newline = System.Environment.NewLine
  List.map2
    (fun i x -> "Case "+ (i).ToString() + ": " + (x).ToString())
    [1..lns.Length] lns
  |> List.fold
       (fun x y -> x + (if x  "" then newline else "") + y) ""
let swappingTwoNumbers(lst) =
  lst
  |> List.takeWhile (fun x -> (fst x)  0 || (snd x)  0)
  |> List.map (fun x -> if (fst x) > (snd x) then (snd x, fst x) else x)
let howManyDivisors(a, b, c) =
  (List.filter (fun x -> c%x = 0) [a..b]).Length
            

// テストコード
let test = printfn "%b"

sayHello()
test((xCubic 2) = 8)
test((xCubic 3) = 27)
test(rectangle(3, 5) = (15, 16))
test((watch 46979) = (13, 2, 59))
test(printComp(1, 2) = "a  b")
test(printComp(5, 5) = "a == b")
test(isRange(1, 3, 8) = true)
test(isRange(3, 8, 1) = false)
test(sort(3, 8, 1) = (1, 3, 8))
test(sort(1, 3, 8) = (1, 3, 8))
test(sort(1, 8, 3) = (1, 3, 8))
test(sort(3, 1, 8) = (1, 3, 8))
test(sort(8, 1, 3) = (1, 3, 8))
test(sort(8, 3, 1) = (1, 3, 8))
test(inRect(5, 4, 2, 2, 1) = true)
test(inRect(5, 4, 2, 4, 1) = false)
//printManyHelloWorld()
test(printTestCases([3;5;11;7;8;19;0]) = """Case 1: 3
Case 2: 5
Case 3: 11
Case 4: 7
Case 5: 8
Case 6: 19""")
test(swappingTwoNumbers([(3,2);(2,2);(5,3);(0,0)]) = [(2,3);(2,2);(3,5)])
test(howManyDivisors(5, 14, 80) = 3)

コメントはまだありません。