32
3
In honor of how much rep I had several hours ago, when I first thought of this challenge:
Numbers like this that are made up of a single digit repeating are called repdigits. Repdigits are fun! Every body would be more happy if the amount of rep they had was a repdigit¹, but I am impatient, so you need to help me find out the fastest way to get to a repdigit.
Here is your challenge:
Given a positive integers representing reputation, output the minimum amount of rep they need to gain to get to a repdigit. For example, at the time of writing this challenge, user Martin Ender had 102,856 rep. The nearest rep-digit is 111,111, so he would need to gain: 8255 rep to be at a repdigit.
Since people dislike losing rep, we will only consider non-negative changes. This means that, for example, if someone is at 12 rep, rather than losing 1 rep, the solution is to gain 10 rep. This allows '0' to be a valid output, since anyone who has 111 rep is already at a repdigit.
Input and output can be in any reasonable format, and since it is impossible to have less than 1 rep on any Stack Exchange site, you can assume no inputs will be less than 1.
One cornercase to note:
If a user has less than 10 rep, they are already at a repdigit, and so they also need '0'.
Test IO:
#Input #Ouput
8 0
100 11
113 109
87654321 1234567
42 2
20000 2222
11132 11090
Standard loopholes apply, and the shortest solution in bytes wins!
Can I return the answer as a singleton array? It wouldn't actually save bytes, but I could make my answer a lot faster. – Dennis – 2016-08-24T06:43:54.617
1@Dennis I don't see why not. – James – 2016-08-24T06:53:52.727
Can we take input as a string? – Dennis – 2016-08-24T06:55:35.853
1@Dennis Why would I say no? I always try to avoid restrictive IO in my challenges, and a lot of languages (like my own) don't distinguish between string and integer input, so I don't see any reason I would restrict it. – James – 2016-08-24T07:10:49.537
2
Related: http://codegolf.stackexchange.com/q/73916/17602
– Neil – 2016-08-24T07:51:59.683@DJMcMayhem Mind adding what a repdigit is in case the link is dead at some point? – Buffer Over Read – 2016-08-24T17:15:56.970
6@ColdGolf I highly doubt Wikipedia will die any time soon, but I added some more info. – James – 2016-08-24T17:18:06.623
The smallest possible rep gain is 2 rep from editing, right? Since the question says only positive changes are valid, wouldn't this affect the minimum actually possible increase to a repdigit in some cases, like 10 requiring +12 to get to 22, because there is no +1? The tests don't seem to consider this. – Anko – 2016-08-26T00:33:06.183
@Anko you can gain 2 by edit and lose 1 through negative vote, so it is possible to have one more rep than what you currently have, and while there is a loss involved, the absolute is still positive. – Brian H. – 2017-09-13T08:44:15.343
@BrianH. I guess the specificity of "we will only consider non-negative changes" is up to interpretation… I took it as "imagine nobody ever loses rep for anything". – Anko – 2017-09-13T10:13:52.850
@Anko, if it were impossible to lose rep, that would mean the lowest rep gain that turns odds to evens and viceversa would be 15 though, so the 100 would give 122 as result instead of 11 – Brian H. – 2017-09-13T10:18:07.077
1@brianh No, the smallest rep gain that turns odds to evens is 5 (question upvote). However, for the sake of this challenge, we're ignoring the fact that there are only certain amounts to gain rep. So
110
should give1
, even though there isn't a way to gain one rep. – James – 2017-09-13T12:09:53.577