27
3
If you don't know already, a quaternion is basically a 4-part number. For the purposes of this challenge, it has a real component and three imaginary components. The imaginary components are represented by the suffix i
, j
, k
. For example, 1-2i+3j-4k
is a quaternion with 1
being the real component and -2
, 3
, and -4
being the imaginary components.
In this challenge you have to parse the string form of a quaternion (ex. "1+2i-3j-4k"
) into a list/array of coefficients (ex. [1 2 -3 -4]
). However, the quaternion string can be formatted in many different ways...
- It may be normal:
1+2i-3j-4k
- It may have missing terms:
1-3k
,2i-4k
(If you have missing terms, output0
for those terms) - It may have missing coefficients:
i+j-k
(In this case, this is equivalent to1i+1j-1k
. In other words, ai
,j
, ork
without a number in front is assumed to have a1
in front by default) - It may not be in the right order:
2i-1+3k-4j
- The coefficients may be simply integers or decimals:
7-2.4i+3.75j-4.0k
There are some things to note while parsing:
- There will always be a
+
or-
between terms - You will always be passed valid input with at least 1 term, and without repeated letters (no
j-j
s) - All numbers can be assumed to be valid
- You can change numbers into another form after parsing if you want (ex.
3.0 => 3
,0.4 => .4
,7 => 7.0
)
Parsing/quaternion builtins and standard loopholes are disallowed. This includes eval
keywords and functions. Input will be a single string and output will be a list, an array, values separated by whitespace, etc.
As this is code-golf, shortest code in bytes wins.
Tons o' test cases
1+2i+3j+4k => [1 2 3 4]
-1+3i-3j+7k => [-1 3 -3 7]
-1-4i-9j-2k => [-1 -4 -9 -2]
17-16i-15j-14k => [17 -16 -15 -14]
7+2i => [7 2 0 0]
2i-6k => [0 2 0 -6]
1-5j+2k => [1 0 -5 2]
3+4i-9k => [3 4 0 -9]
42i+j-k => [0 42 1 -1]
6-2i+j-3k => [6 -2 1 -3]
1+i+j+k => [1 1 1 1]
-1-i-j-k => [-1 -1 -1 -1]
16k-20j+2i-7 => [-7 2 -20 16]
i+4k-3j+2 => [2 1 -3 4]
5k-2i+9+3j => [9 -2 3 5]
5k-2j+3 => [3 0 -2 5]
1.75-1.75i-1.75j-1.75k => [1.75 -1.75 -1.75 -1.75]
2.0j-3k+0.47i-13 => [-13 0.47 2.0 -3] or [-13 .47 2 -3]
5.6-3i => [5.6 -3 0 0]
k-7.6i => [0 -7.6 0 1]
0 => [0 0 0 0]
0j+0k => [0 0 0 0]
-0j => [0 0 0 0] or [0 0 -0 0]
1-0k => [1 0 0 0] or [1 0 0 -0]
Will there ever be unnecessary
+
signs in the input? Like:+1k
? – FryAmTheEggman – 2016-03-30T03:53:16.910@FryAmTheEggman No. inputs will never start with a
+
. – GamrCorps – 2016-03-30T03:54:13.6771Is
-0
a part of the legal output for the last two examples? – isaacg – 2016-03-30T06:47:48.103What exactly constitutes a "parsing built-in"? – LLlAMnYP – 2016-03-30T12:20:53.423
1@isaacg yes that is fine – GamrCorps – 2016-03-30T13:46:26.647
@LLlAMnYP anything that trivializes this challenge, ie that can parse this quaternion and return the array of coefficients – GamrCorps – 2016-03-30T13:47:30.557
@GamrCorps that doesn't make things clearer for me. Sure
ToQuaternion
is obviously off limits (quaternion built-in), but what of my use ofToExpression
? It interprets the string as actual input to the interpreter ("1+i-2j+k" -> 1 + i - 2 j + k
). Does mathematica's capability of working with actual mathematical expressions trivialize the challenge? – LLlAMnYP – 2016-03-30T13:58:56.103@LLlAMnYP in that case, I would take that command to be a form of eval which is prohibited. Sorry about the confusions – GamrCorps – 2016-03-30T14:03:26.247
@GamrCorps Interesting. That would imply, that the input should be dealt with only with string-manipulating functions, regex and the like. I'm then curious, the output array has to be a list of explicit numbers? Because any kind of conversion like
"2.5" -> 2.5
is a (possibly restricted) form ofeval
. – LLlAMnYP – 2016-03-30T14:27:48.1171@LLlAMnYP You bring up a good point. Lets define the
eval
restriction to be takes in a string, interprets as code and/or input. Any conversions do not count under this because you cant pass, for example, the string"test"
to an integer conversion function to receive an integer, buttest
would be interpreted as code in a normaleval
function. TLDR: eval: no, type conversions: yes. – GamrCorps – 2016-03-30T14:34:19.930@GamrCorps I see. That makes Mathematica a bad choice for this then, because it isn't really a typeified language and most type conversions are done by the rather blanketing
ToExpression
. There is of course the (undocumented)Internal`StringToDouble
, but that's hardly a good fit for golf :-) – LLlAMnYP – 2016-03-30T14:40:01.833FromDigits
Golookitup – CalculatorFeline – 2016-03-31T12:52:38.573@CatsAreFluffy that's only for integers. But I fully agree, there's plenty of ways to do it, e.g.
ImportString
too. In this case it just feels like reinventing the wheel, instead of taking advantage of the strengths of the language. But who knows, maybe there's still a beautiful MMA solution out there. – LLlAMnYP – 2016-04-01T06:15:04.567