37
\$\begingroup\$

Input

A integer n ( ≥ 1) and a digit d (an integer such that 0 ≤ d ≤ 9).

In either order; from stdin or parameters or whatever; to a program or function; etc.

Output

The integers from 1 to n inclusive (in order) whose decimal representations contain an even number of ds. (That is, the integers in the list are the integers that have, respectively, an even number of ds.)

In any standard format, etc. In particular, the output need not be represented in decimal.

If output as a single string, the integers must be separated somehow (spaces, commas, newlines, null bytes, whatever).

Examples

in(d,n) ↦ out
1,12    ↦ 2 3 4 5 6 7 8 9 11
0,111   ↦ 1,2,3,4,5,6,7,8,9,11,12,13,14,15,16,17,18,19,21,22,23,24,25,26,27,28,29,31,32,33,34,35,36,37,38,39,41,42,43,44,45,46,47,48,49,51,52,53,54,55,56,57,58,59,61,62,63,64,65,66,67,68,69,71,72,73,74,75,76,77,78,79,81,82,83,84,85,86,87,88,89,91,92,93,94,95,96,97,98,99,100,111

Thanks

To quintopia for the title.

\$\endgroup\$
2
  • \$\begingroup\$ Does 0 count as an even number? \$\endgroup\$
    – MilkyWay90
    Apr 23 '19 at 23:27
  • \$\begingroup\$ @MilkyWay90, of course. \$\endgroup\$
    – msh210
    Apr 24 '19 at 8:50

32 Answers 32

1
2
1
\$\begingroup\$

C, 175 Bytes

_;main(f,n,d){f?f-1?main(0,n,d),printf(_?"%d ":"",n),(n-1?main(2,n-1,d):0):(scanf("%d %d",&n,&d),main(2,n,d)):(n>9?n%10==d?(main(0,n/10,d),_=!_):main(0,n/10,d):(_=!(n==d)));}

Takes n, then d, from standard input. Prints the correct numbers in reverse order.

Expansion

_; // declare return variable
main(f,n,d) //entry point (function selection [1==real main],n,d)
{
    f?
        f-1?
            //recursive loop from 'n' to 1
            main(0,n,d), //call even_digits?
            printf(_?"%d ":"",n),
            (n-1?main(2,n-1,d):0)
        :
            //actual main, just scan and run the loop
            (scanf("%d %d",&n,&d),main(2,n,d))
    :
        //even_digits? (stores result in _)
        (n>9? // recursive case, many digits
            n%10==d?
                (main(0,n/10,d),_=!_)
            :
                main(0,n/10,d)
         :
            (_=!(n==d)) //base case: 1 digit, return !(n==d)
        );
}

I feel like it might be possible to remove the duplicated main(0,n/10,d) near the end.

\$\endgroup\$
1
  • \$\begingroup\$ This doesn't (you say) print them in order. \$\endgroup\$
    – msh210
    Mar 10 '16 at 13:43
1
\$\begingroup\$

Python 2, 68 chars

f=lambda d,n:filter(lambda x:x.count(`d`)%2^1,map(str,range(1,n+1)))

This function takes two ints and returns a list of the numbers with an even number of ds.

\$\endgroup\$
3
  • \$\begingroup\$ Just curious, why the downvote? This is my first post on code golf. \$\endgroup\$ Mar 17 '16 at 21:52
  • \$\begingroup\$ If this isn't recursive, you don't have to name it. lambda d,n:... is fine. \$\endgroup\$ Mar 25 '16 at 18:59
  • \$\begingroup\$ And good job with your first post, keep going! \$\endgroup\$ Mar 25 '16 at 18:59
1
2

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.