22
\$\begingroup\$

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
\$\endgroup\$
16
  • 2
    \$\begingroup\$ Can we output 0? What about [0, 9] (array or list of 2 numbers)? \$\endgroup\$
    – DELETE_ME
    Dec 22, 2017 at 15:51
  • 1
    \$\begingroup\$ Assuming 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! \$\endgroup\$
    – NK1406
    Dec 22, 2017 at 15:53
  • 3
    \$\begingroup\$ Is just ? a possible input? \$\endgroup\$
    – xnor
    Dec 22, 2017 at 16:33
  • 2
    \$\begingroup\$ Is it necessary to support leading zeros? \$\endgroup\$
    – mbomb007
    Dec 22, 2017 at 16:37
  • 1
    \$\begingroup\$ @NK1406 I don't recommend requiring leading zeroes, I can see additional bytes needed to support it at least for some languages. \$\endgroup\$ Dec 22, 2017 at 19:53

31 Answers 31

17
\$\begingroup\$

Python, 21 bytes

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

Try it online!

I used I to represent a missing digit.

\$\endgroup\$
9
\$\begingroup\$

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.
\$\endgroup\$
1
  • \$\begingroup\$ The & can be removed, since cardinal mode interprets the original input as at most 2 integers. \$\endgroup\$
    – Nitrodon
    Dec 23, 2017 at 3:10
6
\$\begingroup\$

JavaScript (ES6), 40 bytes

Outputs 9 if could be 0 or 9.

f=_=>9-([..._].reduce((a,b)=>a+~~b,0)%9)
\$\endgroup\$
3
  • 2
    \$\begingroup\$ Welcome to PPCG! Nice first post! \$\endgroup\$
    – Riker
    Dec 22, 2017 at 20:54
  • 2
    \$\begingroup\$ You don't need to count f=; anonymous functions are perfectly valid. \$\endgroup\$
    – Shaggy
    Dec 23, 2017 at 9:52
  • \$\begingroup\$ The outermost parentheses are not necessary because of the operator precedence ;) \$\endgroup\$ Dec 27, 2017 at 1:00
5
\$\begingroup\$

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.

\$\endgroup\$
5
\$\begingroup\$

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

\$\endgroup\$
0
4
\$\begingroup\$

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
\$\endgroup\$
4
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ Hm, in all the test cases I tried it outputs 0 for an ambiguous case, which makes sense since 9%9 == 0 \$\endgroup\$
    – fluffy
    Dec 23, 2017 at 8:24
  • \$\begingroup\$ if you do int(c) -> ord(c) - 3 that you don't need if, if you choose appropriate character for bad character \$\endgroup\$
    – RiaD
    Dec 25, 2017 at 10:57
3
\$\begingroup\$

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.
\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
8
  • \$\begingroup\$ 9 bytes. Welcome to Jelly golfing! \$\endgroup\$
    – Mr. Xcoder
    Dec 22, 2017 at 16:54
  • \$\begingroup\$ 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. \$\endgroup\$
    – dylnan
    Dec 22, 2017 at 17:03
  • \$\begingroup\$ @dylnan Addressed this \$\endgroup\$
    – Poke
    Dec 22, 2017 at 17:05
  • \$\begingroup\$ @Poke oh, nevermind, I didn't see the requirement had been changed \$\endgroup\$
    – dylnan
    Dec 22, 2017 at 17:06
  • \$\begingroup\$ |0 works instead of fØDV€. \$\endgroup\$
    – Dennis
    Dec 22, 2017 at 23:12
3
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ I'm amazed that you can still keep squeezing bytes off of this. Well done! \$\endgroup\$ Dec 28, 2017 at 16:55
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ You don't need to ravel before you sum ;) \$\endgroup\$ Dec 27, 2017 at 9:08
  • 1
    \$\begingroup\$ Also, ". 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. \$\endgroup\$ Dec 27, 2017 at 9:10
1
\$\begingroup\$

Swift, 51 bytes

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

Try it online!

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$ Dec 23, 2017 at 17:23
1
\$\begingroup\$

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
\$\endgroup\$
0
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

APL (Dyalog), 13 bytes

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

Try it online!

The missing digit is .

\$\endgroup\$
1
\$\begingroup\$

Ruby, 46, 41 bytes

-5 thanks to @Unihedron

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

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ 1. we have chars instead of .split(//), 2. '<' can be replaced with ?< \$\endgroup\$
    – Unihedron
    Dec 27, 2017 at 16:09
  • \$\begingroup\$ 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: codegolf.stackexchange.com/a/151869/21830 \$\endgroup\$
    – Unihedron
    Dec 27, 2017 at 16:23
  • \$\begingroup\$ @Unihedron thanks, can't believe that I forgot about chars \$\endgroup\$
    – Tom Lazar
    Dec 27, 2017 at 20:37
1
\$\begingroup\$

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.

\$\endgroup\$
5
  • \$\begingroup\$ -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 \$\endgroup\$
    – Jo King
    Dec 27, 2017 at 16:08
  • \$\begingroup\$ @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. \$\endgroup\$ Dec 28, 2017 at 2:49
  • \$\begingroup\$ 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. \$\endgroup\$
    – Jo King
    Dec 28, 2017 at 13:47
  • \$\begingroup\$ @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. \$\endgroup\$ Dec 28, 2017 at 14:40
  • \$\begingroup\$ Posted it! Managed to shave off one last byte too! I think that’s the last one I can do \$\endgroup\$
    – Jo King
    Dec 28, 2017 at 16:42
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

Vyxal, 7 bytes

19βN9%‹

Try it Online!

\$\endgroup\$
1
  • \$\begingroup\$ 5 bytes \$\endgroup\$
    – lyxal
    Jul 12, 2021 at 4:49
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

Haskell, 35 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
6
  • \$\begingroup\$ I believe the \d can be changed into just . and the following line into $*. \$\endgroup\$
    – user41805
    Dec 22, 2017 at 17:15
  • \$\begingroup\$ Ah, right. I hadn't removed the ? yet when I wrote that. \$\endgroup\$
    – mbomb007
    Dec 22, 2017 at 18:04
  • \$\begingroup\$ 33 bytes. \$\endgroup\$ Dec 22, 2017 at 18:16
  • \$\begingroup\$ 25: tio.run/##K0otycxL/K@q4Z7wX49LRYvLsNqylosrjstSRcuaSzvB2pCLS@//… \$\endgroup\$ Dec 22, 2017 at 19:03
  • \$\begingroup\$ @totallyhuman Ah, yeah. Another remnant from an earlier version. \$\endgroup\$
    – mbomb007
    Dec 22, 2017 at 20:06
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

Pyth, 8 bytes

%_iz19 9

Try it online!

Uses I instead of ?.

  • Convert to base 19.
  • Negate.
  • Modulo 9.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.