Skip to content

Instantly share code, notes, and snippets.

@takasek
Last active October 30, 2020 01:09
Show Gist options
  • Save takasek/16ec0f27ae1b6c9b6ee5b9834bafad10 to your computer and use it in GitHub Desktop.
Save takasek/16ec0f27ae1b6c9b6ee5b9834bafad10 to your computer and use it in GitHub Desktop.
SwiftでAtCoderに挑戦するためのPlayground用テンプレート
import Foundation
struct Example {
let input: String
let expectation: String
}
// テストケース列挙
let examples: [(String, Example)] = [
("1", Example(
input: """
1 1
""",
expectation: """
Odd
""")),
("2", Example(
input: """
1 2
""",
expectation: """
Even
""")),
("3", Example(
input: """
2 1
""",
expectation: """
EvenEven
""")),
("4", Example(
input: """
2 2
""",
expectation: """
Even
""")),
]
// ----- この関数内が提出コードになる -----
func run(readLine: () -> String?, print: (Any...) -> Void) {
// utils
func readInts() -> [Int] { readLine()!.split(separator: " ").map{Int($0)!} }
func readInt1() -> Int { Int(readLine()!)! }
func readInt2() -> (Int, Int) { let ps = readLine()!.split(separator:" ").map{Int($0)!}; return (ps[0], ps[1]) }
func readInt3() -> (Int, Int, Int) { let ps = readLine()!.split(separator:" ").map{Int($0)!}; return (ps[0], ps[1], ps[2]) }
// code
let (a, b) = readInt2()
print(a.isMultiple(of: 2) || b.isMultiple(of: 2) ? "Even" : "Odd")
}
func main(label: String, example: Example) {
// boilerplates
var inputLines = example.input.split(separator: "\n")
var outputLines: [String] = []
defer {
var expectedLines = example.expectation
.split(separator: "\n")
.map(String.init)
let isSucceeded = expectedLines == outputLines
Swift.print("== Test[\(label)] =========")
Swift.print(isSucceeded ? "succeeded." : "failed.")
if !isSucceeded {
Swift.print("expected | actual")
while !outputLines.isEmpty && !expectedLines.isEmpty {
let o = outputLines.remove(at: 0)
let e = expectedLines.remove(at: 0)
Swift.print("\(o == e ? " " : "!") \(e) | \(o)")
}
}
Swift.print("")
}
run(
readLine: { String(inputLines.remove(at: 0)) },
print: { outputLines.append($0.map {"\($0)"}.joined(separator: " ")) }
)
}
examples.forEach {
main(label: $0.0, example: $0.1)
}
@takasek
Copy link
Author

takasek commented Oct 24, 2020

PlaygroundでRunされたとき、print結果とexpected文字列の比較を行うテストが走り、以下のようなレポートが得られる
! がついているのが間違えている行

== Test[1] =========
succeeded.

== Test[2] =========
succeeded.

== Test[3] =========
failed.
expected | actual
! EvenEven | Even

== Test[4] =========
succeeded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment