Created
August 17, 2012 19:07
-
-
Save taichi/3381668 to your computer and use it in GitHub Desktop.
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
class Baseball { | |
def strike = 0, ball = 0, out = 0 | |
def s = { | |
if(2 < ++strike) { | |
strike = 0; o() | |
} | |
} | |
def b = { | |
if(3 < ++ball) { | |
h() | |
} | |
} | |
def o = { | |
if(2 < ++out) { | |
strike = ball = out = 0 | |
} | |
} | |
def f = { | |
if(strike < 2) { | |
s() | |
} | |
} | |
def h = { strike = ball = 0 } | |
def p = { h(); o() } | |
def resolve = { | |
it.collect { | |
this[it](); "$out$strike$ball" | |
}.join(",") | |
} | |
} |
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
import static org.junit.Assert.* | |
import org.junit.Test | |
class BaseballTest { | |
@Test | |
void test() { | |
'''s -> 010 | |
sss -> 010,020,100 | |
bbbb -> 001,002,003,000 | |
ssbbbb -> 010,020,021,022,023,000 | |
hsbhfhbh -> 000,010,011,000,010,000,001,000 | |
psbpfpbp -> 100,110,111,200,210,000,001,100 | |
ppp -> 100,200,000 | |
ffffs -> 010,020,020,020,100 | |
ssspfffs -> 010,020,100,200,210,220,220,000 | |
bbbsfbppp -> 001,002,003,013,023,000,100,200,000 | |
sssbbbbsbhsbppp -> 010,020,100,101,102,103,100,110,111,100,110,111,200,000,100 | |
ssffpffssp -> 010,020,020,020,100,110,120,200,210,000'''.eachLine { | |
def (i,o) = it.split(' -> ') | |
Baseball target = new Baseball() | |
assert target.resolve(i) == o | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment