fork download
  1. MOD = 1000000000
  2.  
  3. def f(a, update):
  4. for key in update:
  5. a = ''.join(update[key] if c == key else c for c in a)
  6. return eval(a) % MOD
  7.  
  8. t = int(raw_input())
  9. for tc in range(t):
  10. print "Case #" + str(tc + 1) + ": ",
  11.  
  12. a = raw_input()
  13. # prepend + and append * before every digit
  14. a = ''.join('+' + c + '*' if c.isdigit() else c for c in a)
  15. # add mod after every bracket to avoid large values
  16. a = ''.join(c + '%' + str(MOD) if c == ')' else c for c in a)
  17. # solve independently for both x and y co-ordinates
  18. y = f(a, {'S': '+1', 'N': '-1', 'E': '+0', 'W': '-0'})
  19. x = f(a, {'S': '+0', 'N': '-0', 'E': '+1', 'W': '-1'})
  20. print str(x + 1) + " " + str(y + 1)
Success #stdin #stdout 0.01s 7256KB
stdin
4
SSSEEE
N
N3(S)N2(E)N
2(3(NW)2(W2(EE)W))
stdout
Case #1:  4 4
Case #2:  1 1000000000
Case #3:  3 1
Case #4:  3 999999995