What is the missing number (divisibility by 9)

22

Challenge

Given a whole number divisible by 9 and one missing digit, find the missing digit.

The missing digit may be represented by any character other than the digits 0-9 as long as it is consistent.

If the missing digit could be 0 or 9, output something to signify so.

Assume the user is intelligent enough to put only one missing digit and one number etc.

Test Cases

In the test cases the missing digit is represented by a question mark

123? -> 3
?999 -> 0 or 9
?0023 -> 4
000?1 -> 8

NK1406

Posted 2017-12-22T15:49:29.037

Reputation: 739

2Can we output 0? What about [0, 9] (array or list of 2 numbers)? – user202729 – 2017-12-22T15:51:46.230

1Assuming 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.870

2Is 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, if 0 makes the number a multiple of 9, then so does 9. (Because 90, 900, and so on are all multiples of 9). So there's no ambiguity with 0 or 9 vs. 0, because 0 but not 9 isn't a possibility. Using 9 as the filler is bogus though: 000x needs to be 9, not 0. – 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 where 9 but not 0 is the only possible answer? – Peter Cordes – 2017-12-23T07:17:43.920

1@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 whether 0 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.887

1@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 allows x == 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

Answers

17

Python, 21 bytes

lambda s:-int(s,19)%9

Try it online!

I used I to represent a missing digit.

xnor

Posted 2017-12-22T15:49:29.037

Reputation: 115 687

9

Alice, 12 bytes

/o&
\i@/+R9%

Try it online!

Outputs 0 if the result can be either 0 or 9.

Explanation

/   Switch to Ordinal mode.
i   Read all input as a string.
&   Fold the next operation over the characters of this strings.
/   Switch to Cardinal mode (not an operation).
+   Add. Implicitly converts each character to the integer value it represents
    and adds it to the top of the stack. The top of the stack is initially, implicitly
    zero. When the character is "?", it is simply discarded, which adds the top of
    the stack to another implicit zero beneath, so the "?" is effectively skipped.
R   Negate. Multiplies the sum by -1.
9%  Modulo 9. This gives the result.
\   Switch to Ordinal mode.
o   Implicitly convert the result to a string and print it.
@   Terminate the program.

Martin Ender

Posted 2017-12-22T15:49:29.037

Reputation: 184 808

The & can be removed, since cardinal mode interprets the original input as at most 2 integers. – Nitrodon – 2017-12-23T03:10:24.420

6

JavaScript (ES6), 40 bytes

Outputs 9 if could be 0 or 9.

f=_=>9-([..._].reduce((a,b)=>a+~~b,0)%9)

Quinton Miller

Posted 2017-12-22T15:49:29.037

Reputation: 61

2Welcome to PPCG! Nice first post! – Rɪᴋᴇʀ – 2017-12-22T20:54:39.160

2You don't need to count f=; anonymous functions are perfectly valid. – Shaggy – 2017-12-23T09:52:36.247

The outermost parentheses are not necessary because of the operator precedence ;) – Shieru Asakoto – 2017-12-27T01:00:20.857

5

Japt, 7 bytes

Tried a few solutions but the shortest was similar to most others except we don't need to replace the ? at the start.

Can take any non-numeric character as the missing digit. Outputs 0 when the solution can be that or 9.

¬x*J u9

Try it


Explanation

Implicit input of string U. ¬ splits to an array of individual characters, x reduces the array by addition ignoring any non-numeric elements, *J multiplies each element by -1 and u9 gets the positive modulus of the result.

Shaggy

Posted 2017-12-22T15:49:29.037

Reputation: 24 623

5

JavaScript (ES6), 18 bytes

Expects a + as the missing digit. Returns 9 for 0 or 9.

s=>9-eval(9+s+9)%9

Test cases

let f =

s=>9-eval(9+s+9)%9

console.log(f('123+'))  // -> 3
console.log(f('+999'))  // -> 0 or 9
console.log(f('+0023')) // -> 4
console.log(f('000+1')) // -> 8

Arnauld

Posted 2017-12-22T15:49:29.037

Reputation: 111 334

4

Python 2, 44 41 35 bytes

-6 bytes thanks to RiaD

lambda n:-sum(ord(c)-3for c in n)%9

Try it online!

Users ] for missing digit.
Outputs 0 if the missing digit could be 0 or 9.

Rod

Posted 2017-12-22T15:49:29.037

Reputation: 17 588

Hm, in all the test cases I tried it outputs 0 for an ambiguous case, which makes sense since 9%9 == 0 – fluffy – 2017-12-23T08:24:38.727

if you do int(c) -> ord(c) - 3 that you don't need if, if you choose appropriate character for bad character – RiaD – 2017-12-25T10:57:12.940

4

05AB1E, 7 6 bytes

An output of 0 means the result could be either 0 or 9.

þSO(9%

Try it online! or as a Test suite

Explanation

þ        # remove non-digits from input
 S       # split to list of digits
  O      # sum list
   (     # negate
    9%   # mod by 9

Emigna

Posted 2017-12-22T15:49:29.037

Reputation: 50 798

3

Pyth, 9 7 bytes

%_s-Qd9

Uses space as the delimiter and returns 0 if the result could be 0 or 9.

Try it online

Explanation

%_s-Qd9
   -Qd   Remove the space from the input.
  s      Convert it to an integer.
%_    9  Negate mod 9.

user48543

Posted 2017-12-22T15:49:29.037

Reputation:

3

Prolog (SWI), 59 bytes

0*[].
X*[H|T]:-between(0,9,H),U is mod(X+H,9),U*T.
+X:-0*X.

Try it online!

Yay for Logical programming!

Explanation

First we make a predicate *, which holds when applied to zero and the empty list. Our predicate also holds when the front of the list is between 0 and 9 and when we add the leading digit mod 9 the predicate holds.

We then define + to just be this predicate with 0 as the first argument. That is we want the digital sum to be a multiple of 9.

Prolog does all the grunt work of actually finding solutions for us.

Post Rock Garf Hunter

Posted 2017-12-22T15:49:29.037

Reputation: 55 382

3

Befunge-93, 16 bytes

1+!_#@3#.--9%~:#

Try it online!

A one line version of James Holderness’ Befunge answer that manages to shave off two bytes. This essentially compresses the code down to one line, reverses the direction and takes advantage of the fact that Befunge doesn’t skip at the end of the line. He suggested I post a separate answer with an explanation. The code takes an * as representing the missing digit and outputs a 9 for either 0 or 9.

How It Works

1+!_  3  --9% Initialises the pointer going right and initialises the digitsum with 3

1+!_         ~: Gets input and checks whether it is end of input (-1)

      3  - Subtract three from the input. 
           This turns the ASCII values of digits into their mod 9 equivalent ("0"(48)=>45(mod 9)=>0)

          -9% Subtract the value from the digitsum and mod it with 9
              This keeps the digitsum as a negative digit greater than -9
              Repeat this until the input ends

Now this is where it gets tricky.
At the end of input, the stack looks like this:
  -(Digitsum%9), Copy of EOF input (-1)
The pointer is moving left from the _
 +!_ Invert the -1 to get 0 and add it to the digitsum (does nothing but get rid of the -1)
1           %~ Adds 1 mod -1 (no input) = 0 to the stack
         --9 Subtracts 9 from 0, yielding -9 and subtracts that from the digitsum. 
             This makes the digitsum positive and equal to 9-(digitsum%9), our desired value
     @  . Finally, print and exit

* (ASCII value 42) was chosen as the missing character because it counteracts the initial value of the digitsum, 3.

Jo King

Posted 2017-12-22T15:49:29.037

Reputation: 38 234

I'm amazed that you can still keep squeezing bytes off of this. Well done! – James Holderness – 2017-12-28T16:55:09.457

2

Jelly, 11 9 6 bytes

|0SN%9

Explanation

|0     # Read input while removing non-digits (question marks) and removing leading zeros
  S    # Sum the digits
   N   # Negate
    %9 # Mod by 9

An output of 0 means the result could be either 0 or 9.

Try it online!

Saved 2 bytes thanks to Mr. Xcoder. When using the Each quick () during evaluation, splitting the number into digits was redundant.

Saved 3 bytes thanks to Dennis. Can bitwise OR the input with 0 instead of manually parsing the input as a number while removing leading zeros and non-digits.

Poke

Posted 2017-12-22T15:49:29.037

Reputation: 3 075

9 bytes. Welcome to Jelly golfing! – Mr. Xcoder – 2017-12-22T16:54:43.243

This doesn't handle the case where the answer could be 0 or 9. Also, indeed, welcome to Jelly. I am new too but it's been very fun so far. – dylnan – 2017-12-22T17:03:38.673

@dylnan Addressed this – Poke – 2017-12-22T17:05:34.540

@Poke oh, nevermind, I didn't see the requirement had been changed – dylnan – 2017-12-22T17:06:53.843

|0 works instead of fØDV€. – Dennis – 2017-12-22T23:12:37.983

@Dennis it does indeed. How does that work? – Poke – 2017-12-27T15:11:00.357

My explanation is my best guess – Poke – 2017-12-27T15:18:15.680

Bitwise operators attempt to cast to integer and replace the argument with 0 if they cannot, so digits get mapped to their numerical values and non-digits to 0. – Dennis – 2017-12-27T16:32:35.623

2

J, 14 12 bytes

-2 bytes thanks to @BolceBussiere

9|9-[:+/"."0

I honestly don't know why "."0 interprets ? as 0 but it does so on my interpreter and on TIO so I'm not going to question it. (Update: see comments for an explanation why).

This approach very simply takes the sum of the digits, negates it by subtracting from 9, and takes it modulo 9.

Try it online!

cole

Posted 2017-12-22T15:49:29.037

Reputation: 3 526

1You don't need to ravel before you sum ;) – Bolce Bussiere – 2017-12-27T09:08:51.473

1Also, ". doesn't interpret '?' as 0, it interprets it as an empty list. In order to make it fit, J pads it with filler characters, which in this case (a numeric array) is 0. – Bolce Bussiere – 2017-12-27T09:10:58.940

2

LaTeX, many bytes (1000 628 614)

\RequirePackage{expl3}
\ExplSyntaxOn
\tl_new:N \l_divnine_input_tl
\int_new:N \l_divnine_sum_int
\def \getnum {
  \typein [ \l_divnine_input_tl ] { Input\space a\space number }
  \tl_map_function:NN \l_divnine_input_tl \divnine_sum:n
  \int_set:Nn \l_tmpa_int { 9 - \int_mod:nn { \l_divnine_sum_int } { 9 } }
  \typeout
    { The\space missing\space digit\space is:\space
      \int_use:N \l_tmpa_int
      \int_compare:nNnT { \l_tmpa_int } = { 0 } { \space or\space 9 }
    }
}
\cs_new:Nn \divnine_sum:n {
  \regex_match:nnT { \d } { #1 } { \int_add:Nn \l_divnine_sum_int { #1 } }
}
\ExplSyntaxOff
\getnum
\stop

LaTeX, illegible (348 334 bytes)

\RequirePackage{expl3}\ExplSyntaxOn\int_new:N\1\tl_new:N\2\def\3{\typein[\2]{Input\space a\space number}\tl_map_function:NN\2\4\int_set:Nn\1{9-\int_mod:nn{\1}{9}}\typeout{The\space missing\space digit\space is:\space\int_use:N\1\int_compare:nNnT{\1}={0}{\space or\space9}}}\def\4#1{\regex_match:nnT{\d}{#1}{\int_add:Nn\1{#1}}}\3\stop

LaTeX, 132 bytes

\RequirePackage{expl3}\typein[\1].\newcount\2{\ExplSyntaxOn\int_gset:Nn\2{9-\int_mod:nn\19}}\typeout{\the\2 \ifnum\2=9or 0\fi}\stop

Only space is allowed as unknown digit in this code.

Skillmon

Posted 2017-12-22T15:49:29.037

Reputation: 431

1

Befunge-93, 28 27 19 18 bytes

Credit must go to Mistah Figgins, whose PyFunge answer showed me that you didn't need a special check for the missing digit character, if you just made sure the ASCII value was a multiple of nine.

Additional thanks to Jo King who showed that you didn't need to fully convert the characters to their numeric equivalent, and could simply subtract 3 to get a value that is relative to base 9 (ASCII 0 minus 3 is 45, a multiple of 9).

3_v#`0:~--
%9_@.+9

Try it online!

For this to work, you should use the character * for the missing digit (there are others that could also work, but that's the nicest).

Outputs 9 if the missing digit could be 0 or 9.

Explanation

3             Push 3 onto the stack.
 _            Since this is non-zero, drop the 3 and branch left.
3             Now executing right to left, push 3 again.
        --    Negate twice, leaving the value unchanged as our starting sum.

       ~      Now we start the main loop, reading a character from stdin.
    `0:       Duplicate and check if it's > 0, i.e. not end-of-stream.
 _ #          If so, continue to the left.
3        -    Subtract 3 to make the number relative to base 9.
        -     Then subtract it from the total, and repeat the loop.

  v           Once we reach the end of the input, we go down.
  _           Drop the EOF character from the stack and go left.
%9   +9       Mod the total with 9, and add 9.
   @.         Output the result and exit.

Essentially we are calculating the sum of all digits, plus 45 per digit (which will ultimately be cancelled out when we mod with 9). This sum is subtracted from 3 (our starting total), and an additional 39 is subtracted by the missing digit (ASCII * minus three). Again that 3 minus 39 is a multiple of 9, so it's cancelled out when we mod with 9.

So in the end we're calculating the negative sum of all the digits, mod 9, plus 9, i.e.

9 - (digitsum % 9)

And that's gives us the missing digit.

James Holderness

Posted 2017-12-22T15:49:29.037

Reputation: 8 298

-1 byte if you subtract 3 instead of 48. Most of it gets cancelled out by needing to get the add 9 to the modulo – Jo King – 2017-12-27T16:08:08.563

@JoKing Thanks! Never would have thought of that. Pity we still need the +9 now, but I can't see any way to get rid of that. – James Holderness – 2017-12-28T02:49:25.680

Managed to remove one more byte by compressing it down to one line! I changed it to %9 the total every loop and then reuses the 9-- on the way back to add 9 to the total.

– Jo King – 2017-12-28T13:47:29.903

@JoKing I know it's only saving one more byte, but that's brilliant! It's worth posting that as a new answer. You'll definitely get my vote, if nothing else. – James Holderness – 2017-12-28T14:40:49.063

Posted it! Managed to shave off one last byte too! I think that’s the last one I can do – Jo King – 2017-12-28T16:42:19.487

1

Swift, 51 bytes

{9-Int("0"+$0.filter{"?" != $0})!%9}as(String)->Int

Try it online!

Herman L

Posted 2017-12-22T15:49:29.037

Reputation: 3 611

1

Befunge-98 (PyFunge), 15 13 bytes

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+.

#v~'0-+
q>-9%

Try it online!

Uses an x as the missing digit, because it's ASCII value is divisible by 9 after subtracting 48 (and it's nice because its commonly used as a variable in math).

Outputs via exit code (because q is one byte shorter than .@)
Outputs 0 if the missing digit could be 0 or 9.

This only works in the PyFunge interpreter for reasons explained below.

Explanation

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.

#v~'0-+    THE FIRST LINE
#v~        Pushes the next character from input, and goes to the second line on EOF
   '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.

q>-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
q        Outputs via exit code, ending the program

MildlyMilquetoast

Posted 2017-12-22T15:49:29.037

Reputation: 2 907

1

Befunge-93 (PyFunge), 22 21 bytes

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

MildlyMilquetoast

Posted 2017-12-22T15:49:29.037

Reputation: 2 907

Nice answer! Never thought about using a custom character for the missing digit to avoid the extra check. I hope you don't mind that I stole that trick in my updated answer - I wanted to see if I could get the same concept working in the reference interpreter. – James Holderness – 2017-12-23T17:23:19.860

1

Ruby, 46, 41 bytes

-5 thanks to @Unihedron

->s{-s.chars.sum{|i|?<>i ? i.to_i : 0}%9}

Try it online!

Tom Lazar

Posted 2017-12-22T15:49:29.037

Reputation: 41

>

  • we have chars instead of .split(//), 2. '<' can be replaced with ?<
  • < – Unihedron – 2017-12-27T16:09:09.147

    I have crafted a shorter program in the same tool (Ruby) that is different enough (it deals with input and output) to be its own submission, you can find it here: https://codegolf.stackexchange.com/a/151869/21830

    – Unihedron – 2017-12-27T16:23:20.447

    @Unihedron thanks, can't believe that I forgot about chars – Tom Lazar – 2017-12-27T20:37:12.633

    1

    Ruby, 22 bytes

    Uses ' (any char that has a distance to 0 that is divisible by "0" will do, including 0 itself).

    An output of 0 signifies either 0 or 9.

    p -(gets.sum+6*~/$/)%9
    

    Try it online!

    Explanation

    p     # print after inspecting
      -(  # unary negative, for reversing result of modulus (a-b%a)
        gets.sum # ascii value of string
        + 6*~/$/ # add six for each size (in chars) of input, so "0" translates to 0
      )%9 # mod 9
          # find remainder after dividing 9
          # except it's inverted so it's remainder to add to divide 9
    

    Unihedron

    Posted 2017-12-22T15:49:29.037

    Reputation: 1 115

    1

    Befunge-98 (PyFunge), 8 bytes

    #q~3--9%
    

    Try it online!

    Outputs via exit code. As with Mistah Figgin's answer, it only works with Pyfunge, where a negative number mod 9 becomes positive. Uses an x as the missing digit.

    Jo King

    Posted 2017-12-22T15:49:29.037

    Reputation: 38 234

    0

    PowerShell, 40 bytes

    param($a)0..9|?{!(($a-replace'x',$_)%9)}
    

    Try it online! or Verify all test cases

    Takes input like '123x' into $a. Constructs a range 0 to 9 and uses Where-Object (here abbreviated as |?) to pull out those integers that match the clause. The clause takes $a, performs a regex -replace to replace the x with the current digit $_ and gets the mod 9 with %9. Thus if 9 evenly divides, this will be zero. We take the Boolean-not thereof, which turns the zeros truthy and everything else falsey, so that satisfies the Where-Object clause. Those result(s) are left on the pipeline and output is implicit.

    AdmBorkBork

    Posted 2017-12-22T15:49:29.037

    Reputation: 41 581

    0

    Retina, 35 34 25 bytes

    If the ? can be 0 or 9, the result is shown as 9.

    .
    $*
    1{9}
    
    ^
    9$*;
    +`;1
    
    .
    

    Try it online

    Explanation

    .       Sum of the digits in unary + 1
    $*
    1{9}    Modulo 9
    
    ^       Prepend 9 semicolons, for subtracting
    9$*;
    +`;1    Subtract from 9
    
    .       Back to decimal
    

    mbomb007

    Posted 2017-12-22T15:49:29.037

    Reputation: 21 944

    I believe the \d can be changed into just . and the following line into $*. – user41805 – 2017-12-22T17:15:02.060

    Ah, right. I hadn't removed the ? yet when I wrote that. – mbomb007 – 2017-12-22T18:04:17.523

    33 bytes. – totallyhuman – 2017-12-22T18:16:51.680

    @totallyhuman Ah, yeah. Another remnant from an earlier version. – mbomb007 – 2017-12-22T20:06:39.293

    @MartinEnder Nice! – mbomb007 – 2017-12-22T20:06:44.830

    0

    ><>, 35 33 25 21 15 bytes

    An output of 0 means the result could be either 0 or 9.

    0i:&0(?n&3--9%!
    

    Try it online!

    Saved 6 bytes thanks to Jo King by using ' to represent missing digits.

    Emigna

    Posted 2017-12-22T15:49:29.037

    Reputation: 50 798

    15 bytes if you use a ' character instead of a ? – Jo King – 2017-12-28T12:47:18.580

    0

    Pyth, 8 bytes

    %_iz19 9
    

    Try it online!

    Uses I instead of ?.

    • Convert to base 19.
    • Negate.
    • Modulo 9.

    Mr. Xcoder

    Posted 2017-12-22T15:49:29.037

    Reputation: 39 774

    0

    Haskell, 35 bytes

    f s=9-sum[read[i]|i<-s,i<':']`mod`9
    

    Try it online!

    totallyhuman

    Posted 2017-12-22T15:49:29.037

    Reputation: 15 378

    0

    Perl 5, 23 bytes

    $_=- s/\D//r%9;s/0/0|9/
    

    Try it online!

    Doesn't care what character signifies the missing digit as long as it isn't a digit.

    Xcali

    Posted 2017-12-22T15:49:29.037

    Reputation: 7 671

    0

    Tcl, 53 bytes

    puts [expr 9-([regsub -all (\\d)\\D? 0$argv +\\1])%9]
    

    As with other answers, this is made shorter by not explicitly saying “0 or 9”.
    Instead, a result of “9” means either 0 or 9.

    Try it online!

    Explanation

    It works pretty simply. It employs a regular expression to:

    • split the argument into individual digits
    • eliminate any question mark(s) non-digit(s)
    • interleave the digits with plus signs

    It then evaluates 9 - (sum_of_digits mod 9) to arrive at a final value in 1..9, which it then puts.

    The leading 0 (in 0$argv) is required just in case the question mark comes first in the input; a leading plus sign in the transformed sequence is not a problem for expr.

    Dúthomhas

    Posted 2017-12-22T15:49:29.037

    Reputation: 541

    0

    APL (Dyalog), 13 bytes

    {9-9|+/,↑⍎¨⍵}
    

    Try it online!

    The missing digit is .

    Uriel

    Posted 2017-12-22T15:49:29.037

    Reputation: 11 708

    0

    brainfuck, 50 bytes

    +>,[---[<-[<<]>[<+++++++++<]>>-],]-[<+>-----]<---.
    

    Try it online!

    Prints a 9 for either 0 or 9. Missing character is represented by :

    How It Works

    Tape Format:
        0 Total Input 0
    
    The total is represented as 9-digitSum%9
    
    +>,[ Start loop with 1 as the total
        --- Subtract 3 from the inputted byte to make the value%9 equal to the digit
        [   While the input byte exists
          <-                 Decrement the total
          [<<]>[<+++++++++<] If the total is 0, reset it to 9
          >>-                Decrement the input byte
        ]
    ,] Continue loop until there is no input
    -[<+>-----]<---. Add 48 to the total to convert the digit to the ascii value and output it
    

    The missing character needs to be a character that has a mod 9 of 4, +3 because we subtract 3 from the normal digits and +1 for the initialisation of the total as 1.

    As a side note, there is a lot of inefficiency in the code for the sake of golfing, as each digit will reset the total 5 times each, rather than just once sometimes if I subtracted 48 instead of 3.

    Jo King

    Posted 2017-12-22T15:49:29.037

    Reputation: 38 234

    0

    Java 8, 36 34 bytes

    s->9-s.map(c->c>57?0:c-48).sum()%9
    

    Returns 9 when both 0 and 9 are valid.

    Explanation:

    Try it online.

    s->                               // Method with IntStream parameter and int return-type
       9-                             //  Return 9, minus:
         s.map(c->c>57?0:c-48).sum()  //   The sum of the digits (ignoring the question mark)
         %9                           //   modulo-9
    

    Kevin Cruijssen

    Posted 2017-12-22T15:49:29.037

    Reputation: 67 575