Created
September 10, 2018 06:16
-
-
Save takano32/31fc0853fb65aacad282ac9cd29d7fd1 to your computer and use it in GitHub Desktop.
AtCoder のサンプルコード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# | |
# AtCoder の環境では '--disable-gems' で Ruby が起動 | |
def at_coder? | |
not defined? Gem | |
end | |
# ローカルでは MiniTest でテストが走るようにする | |
unless at_coder? | |
require 'minitest' | |
require 'minitest/unit' | |
class TestExample < MiniTest::Test | |
end | |
end | |
class TestExample | |
def test(input, expect) | |
variables = parse input | |
output = solve(*variables) | |
assert_equal expect, output | |
end | |
def test_example01 | |
input = <<~INPUT | |
1 | |
2 3 | |
test | |
INPUT | |
.chomp | |
expect = <<~EXPECT | |
6 test | |
EXPECT | |
.chomp | |
test(input, expect) | |
end | |
def test_example02 | |
input = <<~INPUT | |
72 | |
128 256 | |
myonmyon | |
INPUT | |
.chomp | |
expect = <<~EXPECT | |
456 myonmyon | |
EXPECT | |
.chomp | |
test(input, expect) | |
end | |
end | |
def solve(a, b, c, s) | |
"#{a + b + c} #{s}" | |
end | |
def parse(input) | |
variables = [] | |
index = 0 | |
input.each_line do |line| | |
line = line.chomp | |
case index | |
when 0 | |
a = line | |
variables << a.to_i | |
when 1 | |
b, c = line.split(' ') | |
variables << b.to_i | |
variables << c.to_i | |
when 2 | |
s = line | |
variables << s | |
else | |
raise | |
end | |
index += 1 | |
end | |
variables | |
end | |
def main | |
input = ARGF.read | |
variables = parse(input) | |
puts solve(*variables) | |
end | |
if __FILE__ == $0 | |
if at_coder? | |
main | |
else | |
MiniTest.autorun | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment