I realized that I don't need to use ?
to represent the absent digit, so I used one that is a multiple of 9 after subtracting 48: x
This let me golf off the 3+
, but it only saved me 1 byte because of the length of the first line before the conditional :(
A port of my Befunge-98 answer:
5 more bytes in order to check if we've reached EOF,
1 more byte to push 48 ("0"
vs '0
),
1 more byte to print the answer with .@
,
and 1 more byte, because the second line has a space
for a total of 8 more bytes.
~:0`!#|_"0"-+
@.%9-<
Try it online!
Outputs 0
if the missing digit could be 0 or 9.
This only works in the PyFunge interpreter for reasons explained below.
Explanation
Much of this explanation is copy-pasted from my Befunge-98 explanation, as that program is very similar to this one. shameless plug
In the first line of the program, we sum the digits, including the x, which is treated like a 72 because of it's ASCII value. However, the sum will be the same once we've modded by 9, so this is irrelevant.
~:0`!#|_"0"-+ THE FIRST LINE
~ Gets a character from input - If it is negative, we've reached EOF
:0`! Pushes 0 if the character is positive, 0 otherwise
#|_ Goes to the next line if the value if 0
This also gets the negative value off the stack by using a |
"0"- Subtracts 48 to account for taking in ASCII values
+ Adds this adjusted value to the sum
If we just modded by 9, we would be left with the wrong digit, because we want 9 - (sum % 9)
. However, we can do better than 9\-
, which would subtract the remainder from 9: if we make the sum negative before modding by 9, we will get a positive result, equivalent to 9 - (sum % 9)
in some interpreters. This is what requires us to use the PyFunge interpreters for both Befunge 93 and 98, as it is the only one on TIO that does this. The others give us a value between -8 and 8 instead of 0 and 8
@.%9-< THE SECOND LINE
< Redirects the IP onto this line
- Subtracts the sum from an implicit 0, making it negative
%9 Mods the sum by 9
@. Prints the digit and exits
2Can we output
0
? What about[0, 9]
(array or list of 2 numbers)? – user202729 – 2017-12-22T15:51:46.2301Assuming you mean instead of 0 or 9, I think I will edit this requirement to be any value indicating it could be either. Thank you! – NK1406 – 2017-12-22T15:53:26.337
3Is just
?
a possible input? – xnor – 2017-12-22T16:33:34.8702Is it necessary to support leading zeros? – mbomb007 – 2017-12-22T16:37:16.153
@xnor yes, it could be. It would output the 0 or 9 ouput – NK1406 – 2017-12-22T18:44:45.223
@mbomb007 The program should, I don't foresee any bytes lost on account of it. Test case #2 is mainly why. – NK1406 – 2017-12-22T18:46:55.277
1@NK1406 I don't recommend requiring leading zeroes, I can see additional bytes needed to support it at least for some languages. – Erik the Outgolfer – 2017-12-22T19:53:32.943
Wait, so the question mark is input? Or is the challenge to find any place where the digit could go? – Dúthomhas – 2017-12-22T21:16:35.450
All of the cases on the left for the test cases are valid input. The goal is to find the digit represented by the question mark for each case (which can be any non-numerical character as you choose). You can do this because all of the digits of a number divisible by 9 add up to a multiple of 9. – NK1406 – 2017-12-22T21:19:53.337
1The condition that you signify “0 or 9” instead of just “0” is significant. By saying, “anything” that signifies both is a big loophole. At least one of the answers below says “0 means 0 or 9”. – Dúthomhas – 2017-12-23T06:25:28.047
@Dúthomhas: I think
0
is legit: at any place-value position, if0
makes the number a multiple of 9, then so does 9. (Because90
,900
, and so on are all multiples of 9). So there's no ambiguity with0 or 9
vs.0
, because0
but not9
isn't a possibility. Using9
as the filler is bogus though:000x
needs to be9
, not0
. – Peter Cordes – 2017-12-23T07:05:03.007@PeterCordes I agree, but the question is pretty specific about it, devoting both a complete sentence and an example to it. – Dúthomhas – 2017-12-23T07:17:03.180
@NK1406: should
0000
be considered divisible by 9? Or should?00
(or any pattern with all zeros other than the missing digit) be considered a corner case where9
but not0
is the only possible answer? – Peter Cordes – 2017-12-23T07:17:43.9201@Dúthomhas: Yeah, but the right solution in code golf (and programming in general) is often to recognize that a corner case isn't really a corner case. The question was even edited to remove the wording that required the signifier to be
0 or 9
exactly, so I think this is intentionally being allowed. What I'm not sure about is whether0
should be considered divisible by 9. It wouldn't normally be considered a multiple of 9, but that's not quite the same thing as evenly divisible. – Peter Cordes – 2017-12-23T07:19:41.8871@PeterCordes Point taken. And, since 0 ≡ 9 (mod 9), then yes, zero is evenly divisible by nine. This challenge is to find the missing number (mod 9), so the missing digit could be either zero or nine. Also not a corner case. :O) – Dúthomhas – 2017-12-23T07:24:58.677
1@Dúthomhas: Agreed, I'm leaning towards the
x % 9 == 0
criterion now, which allowsx == 0
. Also, disallowing it now would invalidate all the answers by requiring extra conditional behaviour, so there's a stack-exchange reason for allowing this interpretation, as well as sound mathematics. I was just getting hung up on 0 not being a whole-number multiple of 9. – Peter Cordes – 2017-12-23T07:30:21.583