-5
\$\begingroup\$

The Challenge:

Find if a number contains a digit. The number and the digit are to be taken as input. Output must be a boolean value. But you are not allowed to use the string representation of the number at any time. Now don't count upon a char* either. You must devise a mathematical way to do it.

Rules:

• You can't use a string/char[] representation of the number. Same goes for the digit.

• It must be a complete program.

• Input can be in any form.

• Output must be a boolean value.

This is code-golf, so shortest code wins!.

Test Cases:

Input: 51, 5
Output : True
------------------------
Input: 123456789, 0
Output : False
------------------------
Input: 1111, 1
Output : True

Disclaimer: I don't understand much Pyth/CJam/Japt and similar languages. Thus I hope everyone will be honest enough not to break the rules. Never mind, the community is always there to help me :)

\$\endgroup\$
  • 5
    \$\begingroup\$ Uh, isn't this just repeatedly mod then divide the number by 10? Unless you mean to include floating point, which isn't really clear from your test cases. \$\endgroup\$ – FryAmTheEggman Feb 26 '16 at 15:59
  • \$\begingroup\$ 05AB1E converts numbers to strings implicitly if needed. Is this also prohibited? \$\endgroup\$ – Adnan Feb 26 '16 at 16:08
  • 3
    \$\begingroup\$ Recommended reading. \$\endgroup\$ – Martin Ender Feb 26 '16 at 16:11
  • 8
    \$\begingroup\$ @FarhanAnam of course it's not interesting without the restriction, but that doesn't mean the restriction fixes that problem. \$\endgroup\$ – Martin Ender Feb 26 '16 at 16:37
  • 10
    \$\begingroup\$ Some languages read all input as strings, regardless of whether the input is numeric. Are such languages then banned outright? \$\endgroup\$ – Alex A. Feb 26 '16 at 16:46
3
\$\begingroup\$

Perl 6, 27 bytes

{$^a.polymod(10 xx*)∋$^b}

Usage:

# make it an infix op, because I can
my &infix:<contains> = {$^a.polymod(10 xx*)∋$^b}

say 51 contains 5; # True
say 123456789 contains 0; # False
say 1111 contains 1; # True

# you can use infix ops as oddly named subs as well
say infix:<contains> 1230, 0; # True

say map {$^a.polymod(10 xx*)∋$^b}, 10,0, 12,3, 12,2;
# (True False True)
# proof it isn't doing anything string related
'hi' contains 'h';
# Method 'polymod' not found for invocant of class 'Str'
# Since it uses a Set operator the second argument has to be an Int
# not a Str
say 123 contains '1'; # False
# not a Rat
say 123 contains 1.0; # False
# not a Num
say 123 contains 1e0; # False
# but an Int
say 123 contains 1; # True

Explanation:

polymod is similar to divmod from other languages, except it takes the number and a list of divisors. Here I give it an endless list of 10s so that it splits it up into a list of digits.

Then I see if the resulting list contains the second argument by using the 'contains as member' (  / (cont) ) Set operator.

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

Haskell, 42 bytes

Called like number!digitToFind:

n!d|n<d=1>2|mod n 10==d=1<2|1<2=div n 10!d
\$\endgroup\$
2
\$\begingroup\$

MATL, 15 14 bytes

t`10H#\t]xN$hm

Try it online!

t                   % take 1st input (number) implicitly. Duplicate
 `      ]           % do...while loop
  10                % push number 10, to be used as divisor
    H#\             % divmod. Pushes remainder and then quotient
       t            % duplicate quotient. Used as loop condition: exit if zero
         x          % delete last quotient (which is zero)
          N$h       % concatenate all partial results in an array
             =      % take 2nd input (digit) implicitly.
              m     % ismember function. True if digit is in array
\$\endgroup\$
0
\$\begingroup\$

Python 3, 59

It uses the same approach as every other approach.

def f(x,s):
 while x:
  x,r=divmod(x,10)
  if r==s:return 1
\$\endgroup\$
0
\$\begingroup\$

Befunge-93, 38 bytes

&&00pv >0.@.1<
%*25:<_^#:/_ ^#\*25-g00

Arguments are taken in the form MainNum Digit. It's the same algorithm that everyone else is doing (check if mod 10 is equal to the digit; if not, div 10 until it equals 0), since that's pretty much the optimal way to do this one.

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

JavaScript (ES6), 30 bytes

d=>g=n=>n%10==d||n>9&&g(n/10|0)

Examples:

f=d=>g=n=>n%10==d||n>9&&g(n/10|0)
f(0)(0) -> true
f(0)(10) -> true
f(0)(12) -> false
f(1)(12) -> true
f(2)(12) -> true
f(3)(12) -> false
\$\endgroup\$
0
\$\begingroup\$

PHP, 66 65 62 47 bytes

in_array($d,array_map('intval',str_split($n)));

$n is the number to be searched
$d is the digit to search for

This code converts the number into array[number], then scans the array for a matching digit. Returns true if any digit matches.

Alternatively, if number->string->array[string]->array[number] is not valid (even though no operations use it as a string), there's this at 62 bytes, for any $n > 0:

function f($n,$d){return$n&&($d==$n%10||f(($n-$n%10)/10,$d));}
\$\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.