// fsi上で#load "foo.fsx";;してください。
// ITP1_4
let ABProblem(a, b) =
let af = float a
let bf = float b
(a/b, a%b, af/bf)
let circle(r) =
let pi = System.Math.PI
(pi * r * r, 2.0 * pi * r)
let simpleCalc(dataset) =
let body(exp : string) : int =
let ary = exp.Split()
let a = System.Int32.Parse(ary.[0])
let b = System.Int32.Parse(ary.[2])
if ary.[1] = "+" then a + b
else if ary.[1] = "-" then a - b
else if ary.[1] = "*" then a * b
else a / b
let isEnd(exp : string) = exp.Split().[1] "?"
List.takeWhile isEnd dataset |> List.map body
let minMaxAndSum(lst : List) =
let min = List.fold (fun x y -> if x if x > y then x else y) 0 lst
let sum = List.fold (fun x y -> x + y) 0 lst
(min, max, sum)
// ITP1_5
let printDataset(lst, endSeq, proc) =
List.takeWhile (fun x -> (not (endSeq x))) lst
|> List.map proc
|> String.concat System.Environment.NewLine
let printRect(lst : List) =
let p(line) =
let s = (String.init (snd line) (fun i -> "#")) + System.Environment.NewLine
String.init (fst line) (fun i -> s)
printDataset(lst, (fun x -> x = (0,0)), p)
let printFrame(lst) =
let frame(HW) =
let W = snd HW
let H = fst HW
let nl = System.Environment.NewLine
let hf = String.init W (fun x -> "#") + nl
let bd =
if W = 1 then "#"
else if W = 2 then "##"
else "#" + (String.init (W-2) (fun x -> ".")) + "#" + nl
if H = 1 then hf
else if H = 2 then hf + hf
else String.init H (fun i -> if i = 0 || i = H - 1 then hf else bd)
printDataset(lst, (fun x -> x = (0,0)), frame)
let printChessboard(lst) =
let nl = System.Environment.NewLine
let board(HW) =
let imaru =
String.init (snd HW) (fun i -> if i % 2 = 0 then "#" else ".")
let marui =
String.init (snd HW) (fun i -> if i % 2 = 0 then "." else "#")
String.init (fst HW)
(fun i -> (if i % 2 = 0 then imaru else marui) + nl)
printDataset(lst, (fun x -> x = (0,0)), board)
let call(n) =
let rec includeThree(i) = i 0 && (i % 10 = 3 || includeThree(i / 10))
List.filter (fun i -> (i % 3 = 0) || includeThree(i)) [1..n]
// テストコード
let test = printfn "%b"
let (d, r, f) = ABProblem(3, 2)
test(d = 1 && r = 1 && abs(f - 1.50) < 0.00001)
test(abs ((fst (circle 2.0)) - 12.566371) < 0.00001)
test(abs ((snd (circle 2.0)) - 12.566371) < 0.00001)
test(simpleCalc(["1 + 2"; "56 - 18"; "13 * 2"; "100 / 10"; "27 + 81"; "0 ? 0"])
= [3;38;26;10;108])
test(minMaxAndSum([10;1;5;4;17]) = (1, 17, 37))
test((printRect [(3,4);(5,6);(2,2);(0,0)]) = """####
####
####
######
######
######
######
######
##
##
""")
test((printFrame [(3,4);(5,6);(3,3);(2,2);(1,1);(0,0)]) = """####
#..#
####
######
#....#
#....#
#....#
######
###
#.#
###
##
##
#
""")
test((printChessboard [(3,4);(5,6);(3,3);(2,2);(1,1);(0,0)]) = """#.#.
.#.#
#.#.
#.#.#.
.#.#.#
#.#.#.
.#.#.#
#.#.#.
#.#
.#.
#.#
#.
.#
#
""")
test((call 30) = [3;6;9;12;13;15;18;21;23;24;27;30])
F# - AOJ ITP1_4~ITP1_5
F# - AOJ ITP1_4~ITP1_5
テストケースが長くなってきたので、次からはテストケースを別ファイルにします。
コメントはまだありません。