Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

How many odd digits?

+1
−0

Given a positive integer, count its odd digits.

Input

  • An integer from 1 to 999,999,999, inclusive, in any of the following formats:
    • A number (such as an integer or floating point number), like 123.
    • A string, like "123".
    • A sequence of characters (such as an array or list), like ['1', '2', '3'].
    • A sequence of single digit numbers, like [1, 2, 3].

Output

  • The number of odd digits in the input.

Test cases

Test cases are in the format input : output.

1 : 1
2 : 0
3 : 1
4 : 0
5 : 1
6 : 0
7 : 1
8 : 0
9 : 1
10 : 1
11 : 2
12 : 1
13 : 2
14 : 1
15 : 2
16 : 1
17 : 2
18 : 1
19 : 2
20 : 0
111111111 : 9
222222222 : 0
123456789 : 5
999999999 : 9

Scoring

This is a code golf challenge. Your score is the number of bytes in your code. Lowest score for each language wins.

Explanations are optional, but I'm more likely to upvote answers that have one.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Input format (5 comments)

6 answers

You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.

+1
−0

C (gcc), 37 bytes

This takes an integer as input. It uses the fact that the last digit is odd if and only if the number is odd, and that integer division by 10 removes the last digit.

Testing if the number is odd is by doing bitwise and with 1, which conveniently gives 1 for odd and 0 for even numbers, so the number of odd digits is obtained by just adding those up.

r;f(i){for(;i;i/=10)r+=i&1;return r;}

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−0

Python, 37 27 bytes

First, with input as an integer:

lambda i:sum(ord(x)&1for x in str(i))

This converts the number to string (in the base-10 default) and processes each character. It exploits the fact that digit symbols 0..9 are consecutive in Unicode and 0 corresponds to an even code point (if it were odd, the code would have to use ord(~x) instead).

ord is used to process each character individually. The alternative would be to convert the string to bytes and use the numeric values that come (assuming Python 3.x) from iterating over the bytes directly. However, any such conversion results in longer code. Also, while Python offers direct int->bytes conversions, they do the wrong thing: bytes(i) creates an object of that many bytes with all zero values, and i.to_bytes() gives a binary representation.

As a list of digit values, of course we can just process them directly:

lambda i:sum(x&1for x in i)

This can be done in pure FP primitives instead of using a comprehension. But it's longer, despite the eta reduction:

lambda i:sum(map((1).__and__,i))

(The parentheses around 1 are syntactically necessary.)

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

More flexible input formats (2 comments)
+1
−0

Vyxal, 4 bytes

Expects a string.

O2%∑

Try it here!

Explanation

O2%∑
O    ord
 2%  modulo 2
   ∑ sum
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−0

C (gcc), 41 bytes

r;o(char*s){for(;*s;r+=*s++&1);return r;}

Try it online!

This is under the assumption that in a function solution, input has to be passed as parameter and output through the return value. The result variable is allocated outside the function to get gcc implicit int declaration and zero initialization for free.

It's very straight-forward: input as string, iterate until null terminator found. Mask each character's ASCII value with 1 to see if odd or even, under the assumption that '0' == 48, an even value. From there, ASCII character are guaranteed to be adjacent in the symbol table.

I also tried to write it with recursion but got exactly the same amount of characters (41):

r;o(char*s){r+=*s&1;*s&&o(s+1);return r;}

Printing the result inside the function also works but is longer:

r=48;o(char*s){for(;*s;r+=*s++&1);puts(&r);}
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

Alternative function IO (3 comments)
+1
−0

Japt -mx, 2 1 byte

u

Try it

u     :Implicit map of input array
u     :Modulo 2
      :Implicit output of sum of resulting array
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−0

JavaScript, 25 bytes

Input as an array of digits

a=>a.map(x=>t+=x%2,t=0)|t

Try it online!

26 bytes

With input as a string (or an array of digit strings).

f=([d,...a])=>d?d%2+f(a):0

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

Same length and perhaps easier to understand: `a=>a.reduce((s,a)=>s+a%2)` (1 comment)

Sign up to answer this question »