Charcoal, 32 2929 27 bytes
≔⁰θFS≧⁺⁺∧﹪℅ι²⊗θ℅ιθ‹³﹪θ⁸§1ijkθ≔⁰θFS≔⁺×θX³℅ι℅ιθ‹³﹪θ⁸§1ijkθ
Try it online!Try it online! Link is to verbose version of code. Explanation: The values are encoded to integers equivalent to 0..7
(modulo 8
) in the order 1
, i
, j
, k
, -1
, -i
, -j
, -k
. The multiplications by i
, j
and k
have the following effect on the integer:
- Multiplying by
i
is equivalent to tripling the integer and adding1
. - Multiplying by
j
is equivalent to adding2
to the integer. - Multiplying by
k
is equivalent to tripling the integer and adding3
.
Edit: Thanks to @NickKennedy for saving 3 bytes by pointing out that the ordinals of i
, j
and k
are equivalent (mod 8) to 1
, 2
and 3
respectively. This means that the integer needs to be tripled if the ordinal is odd, and then the ordinal can be added to it.
Edit: Furthermore, tripling the integer (mod 8) if the ordinal is odd is equivalent to multiplying the integer by 3 to the power of the ordinal, for a further 2 byte saving.
≔⁰θ
Start with 0
.
FS
Loop over the input...
≧⁺⁺∧﹪℅ι²⊗θ℅ιθ≔⁺×θX³℅ι℅ιθ
... adding twicemultiply the current value ifby 3 to the power of the ordinal is odd, plusthen add the ordinal.
‹³﹪θ⁸
Output a -
sign if the result (mod 8
) is greater than 3. (There are other ways to express this in Charcoal but sadly I couldn't do better than 5 bytes.)
§1ijkθ
Output 1
, i
, j
, or k
, depending on the result (mod 4
).