34
\$\begingroup\$

You will be given a string. It will contain 9 unique integers from 0-9. You must return the missing integer. The string will look like this:

123456789
 > 0

134567890
 > 2

867953120
 > 4
\$\endgroup\$
  • 5
    \$\begingroup\$ @riker That seems to be about finding a number missing in a sequence. This seems to be about finding a digit missing from a set. \$\endgroup\$ – DJMcMayhem Mar 27 '17 at 19:48
  • 10
    \$\begingroup\$ @Riker I wouldn't think it's a duplicate, given that the linked challenge has a strictly incrementing sequence (of potentially multi-digit numbers), whereas here it's in arbitrary order. \$\endgroup\$ – AdmBorkBork Mar 27 '17 at 19:50
  • 3
    \$\begingroup\$ Hi Josh! Since no one else has mentioned it so far, I'll direct you to the Sandbox where you can post future challenge ideas and get meaningful feedback before posting to main. That would have helped iron out any details (like STDIN/STDOUT) and resolved the duplicate dilemma before you received downvotes here. \$\endgroup\$ – AdmBorkBork Mar 27 '17 at 20:23
  • 1
    \$\begingroup\$ It's such a shame that 9-x%9 works for any digit except 0. Maybe someone more clever than me will find a way to make it work. \$\endgroup\$ – Bijan Mar 28 '17 at 0:45
  • 2
    \$\begingroup\$ Several answers take an integer as function input. Is that allowed? \$\endgroup\$ – Dennis Mar 28 '17 at 0:52

68 Answers 68

0
\$\begingroup\$

D, 24 bytes

writeln(477-readln.sum);

Just add up all characters in the given string (not terminated by a newline). The sum is s = 48 * 9 + (0 + 1 + 2 + ... + 9 - x) = 477 - x, therefore, x = 477 - s.

The calls make use of optional parentheses (-4 bytes), and .sum invocation uses uniform function call syntax (-1 more byte).

Try it online!

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

Q, 24 bytes

{1#iasc"0123456789"in x}

Gets a boolean vector between the expectation and input, then sorts the indices of the expectation by that boolean vector in ascending order, first value always being the missing element since boolean for it is set to 0 and we know that value=index in this case.

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

awk, 18 bytes

{$0=/0/?9-$0%9:0}1

Explanation: If 0 exists in the input, return 9 - modulo 9 of the input, otherwise return 0. Implicit print.

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

Bash, 36 bytes

sort <(fold -1<<<$a;seq 0 9)|uniq -u

Try it online!

Posting to get golfing tips over this.

fold -1<<$a writes one char of input per line

seq 0 9 writes 0..9 one per line after this.

Those lines are fed to sort and filtered by uniq -u displaying only not duplicated lines.

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

Perl 5, 16 15 bytes

Execution:

perl -pe '$_=/0/?9-$_%9:0'

perl -pe '$_=-hex($_)%15'

15 14 bytes of code + 1 byte for -p switch.

Explanation:

If 0 exists in the input, return 9 - modulo 9 of the input, otherwise return 0.

Interprets input as hex string, negates it, and calculates modulo 15 (based on xnor's Python answer).

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

Racket 106 bytes:

(filter-not(λ(x)(member x(string->list(number->string n))))
(for/list((i(range 48 58)))(integer->char i)))

Ungolfed:

(define (f n)
  (filter-not
   (λ(x)
     (member x
             (string->list
              (number->string n))))
   (for/list((i(range 48 58)))(integer->char i))))

Testing:

(f 867953120)

Output:

'(#\4)
\$\endgroup\$
0
\$\begingroup\$

C#, 21 bytes

z=>45-z.Sum(v=>v-'0')

The expression v-'0' takes the char value such as '3' and subtracts the char value for '0', leaving the value 3, essentially converting the character 3 into the integer 3.

45 is the sum of all the numbers 0 through 9. 45 - the sum of the passed-in integers yields the missing integer.

Sample code

public static void Main()
{
    Func<string, int> X = z=>45-z.Sum(v=>v-'0');

    Console.WriteLine(X("123456789"));
    Console.WriteLine(X("134567890"));
    Console.WriteLine(X("867953120"));
}

Test Here: https://dotnetfiddle.net/yanR5S

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

Noodel, 8 bytes

ɲdFḶṡạĖ⁻

Try it:)


How it works

ɲdFḶṡạĖ⁻
         # Input implicitly pushed onto the stack.
ɲd       # Pushes the string "0123456789" onto the stack.

  FḶṡạĖ⁻ # Loops nine times removing digits from the string "0123456789" producing the missing number.
  F      # Pushes the string "F" onto the stack.
   Ḷ     # Consumes the string "F" and converts to a base 98 number producing 9 then loops the following that many times.
    ṡ    # Swaps the string "0123456789" with the input on the stack.
     ạ   # Gets the ith element from the input and pushes it onto the top of the stack.
      Ė  # Grabs the string "0123456789" at the bottom of the stack and puts it at the top.
       ⁻ # Removes the element pulled out of the input from the string.
         # Implicit end of the loop.

         # Implicit push to the screen, outputting the missing digit.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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