Post History
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...
Answer
#3: Post edited
## 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
- ## 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
## 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.