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

Post History

60%
+1 −0
Challenges How many odd digits?

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 expl...

posted 2mo ago by Karl Knechtel‭  ·  edited 2mo ago by Karl Knechtel‭

Answer
#3: Post edited by user avatar Karl Knechtel‭ · 2024-08-29T18:10:23Z (about 2 months ago)
Update because I didn't notice the friendly input specification
  • ## Python, 37 bytes
  • ```
  • 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.
  • ## 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.)
#2: Post edited by user avatar Karl Knechtel‭ · 2024-08-27T20:28:01Z (about 2 months ago)
Bitwise-complementing the mask is wrong; rather the input value would be bitwise-complemented in that hypothetical.
  • ## Python, 37 bytes
  • ```
  • 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 say `&~1` 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.
  • ## Python, 37 bytes
  • ```
  • 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.
#1: Initial revision by user avatar Karl Knechtel‭ · 2024-08-27T00:49:47Z (about 2 months ago)
## Python, 37 bytes
```
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 say `&~1` 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.