Task:
Given sample index, x, calculate sample value f(x) of triangular wave, with period of 4 samples and amplitude 1. Offset can be negative and sample value could be either {0, 1, -1}.
Test cases:
-5 -> -1
-4 -> 0
-3 -> 1
-2 -> 0
-1 -> -1
0 -> 0
1 -> 1
2 -> 0
3 -> -1
4 -> 0
5 -> 1
Personally I know two approaches in C - the first is using lookup table, the second is using conditional instructions. For brownie points, could you impress me with a pure "math" approach? (I mean a pure functional approach, e.g. not using conditional instructions or using memory for LUT.) But this is not an restriction. If you can't, or your language does not support it - just post any solution