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

Comments on How many odd digits?

Post

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)
Input format
Shaggy‭ wrote about 2 months ago

Any restrictions on the input format? Can we take an array of digits, for example?

trichoplax‭ wrote about 2 months ago

Oh good point. I hadn't thought of that. Usually I like input formats to be flexible as long as the input format doesn't constitute preprocessing. In this case I feel like separating the input into digits is the bulk of the challenge.

I've currently specified input as an integer, so the existing python answer has converted to string before processing. Forbidding input as a string seems difficult (particularly as some answers may take input from STDIN which is always a string). So I'm leaning towards allowing input as an integer or string, but not an array, but some languages have little or no distinction between a string and an array, so I'm not sure if that's best.

I'll avoid relaxing the input format until I've thought about it, as it's easier to make it more flexible than less flexible once answers are posted. Any suggestions of where to draw the line and why?

trichoplax‭ wrote about 2 months ago

Actually allowing input as a string would only take the python answer from 37 bytes to 32, so I was probably overstating "bulk of the challenge".

trichoplax‭ wrote about 2 months ago · edited about 2 months ago

Allowing an array of digits (as integers rather than characters) would bring the python answer down to 27 bytes.

I did intend this as a very quick challenge, so I'm now considering allowing input as any of:

  • a number (such as integer or floating point number)
  • a string
  • an array of characters
  • an array of numbers
trichoplax‭ wrote about 2 months ago

I've now edited to make the input format more flexible.

Specifically, yes you can take input as an array of digits.