2
A Math.SE user have a funny game explained as such:
Pick a random positive integer X.
Add +1, 0, -1 to make it divisible by 3Keep track of how much you've added and subtracted. That is your "score"..
Divide by 3 to create a new X.
Repeat steps 2 and 3 until you reach one.
I'm expecting the shortest code to give the correct score and the decomposition (as a list of the following values [+1;0;-1]) given an integer input.
Input => Expected score => Expected decomposition #explanation
12 => -1 => 0-1 #(12/3 => (4 - 1) / 3 = 1
^^^
25 => 0 => -1+10 (25 - 1) / 3 => (8 + 1) / 3 => (3 + 0) / 3 => 1
^^^ ^^^ ^^^
9320 => -1 => +1+1-10-1+1-1-1 #(9320 + 1) / 3 => (3107 + 1) / 3 => (1036 - 1) / 3 => (345 + 0) /3 => (115 - 1) / 3 => (38 + 1) / 3 => (13 - 1) / 3 => (4 - 1) / 3
^^^ ^^^ ^^^ ^^^ ^^^ ^^^ ^^^ ^^^
Related. (This challenge basically asks for the sum of digits of the balanced ternary representation.) – Martin Ender – 2018-05-28T14:25:30.273
1@MartinEnder it even sounds like a dup... I should enlarge my vocabulary in order to find them more easily. Thanks. – Thomas Ayoub – 2018-05-28T14:33:36.463
I personally don't think it's a dupe because the expected output isn't the bal-ternary representation so you still have to do a slightly different task. Very related though, I agree. – HyperNeutrino – 2018-05-28T14:46:26.330
Does our code have to include the "pick a random positive integer" part or can we take the number as input? – nimi – 2018-05-28T16:06:56.533
@nimi I think that "given an integer input" overrides what's said in the quote from Math.SE. – Arnauld – 2018-05-28T16:10:41.603
Suggested test cases:
121: [ -1, -1, -1, -1 ] --> -4
,122: [ 1, 1, 1, 1, 1 ] --> 5
– Arnauld – 2018-05-28T16:17:31.6771@HyperNeutrino, asking for the decomposition and its sum is a pretty minor variant on asking for the decomposition. – Peter Taylor – 2018-05-28T16:57:00.807
@PeterTaylor I may be misinterpreting the challenge, but asking for the decomposition and asking for the digits seem like completely different tasks to me... – HyperNeutrino – 2018-05-28T17:48:35.803
1@Arnauld you're right about 12, and the fact that the ouptut code must give both score & decomposition. I'll make my next questions proof-read – Thomas Ayoub – 2018-05-28T18:55:38.523
@HyperNeutrino, isn't the decomposition just the digits? Actually, on trying it, it seems to be the digits (except the leading +1) reversed. That's possibly accumulating enough trivial modifications (skip the first digit, reverse, also output the sum) to add up to something non-trivial, but it's still borderline. – Peter Taylor – 2018-05-28T19:06:08.073
@PeterTaylor I personally think that finding the decomposition on its own is much more competitive than finding the digits and then doing that whole reversal procedure, but it is a very similar challenge and doesn't really add too much new to the existing challenge – HyperNeutrino – 2018-05-29T16:40:19.550