String and ASCII
Jelly, 4 bytes
OSBS
TryItOnline
How?
OSBS - Main link: s e.g. "abcDeFAaB"
O - ordinal (vectorises) [97, 98, 99, 68, 101, 70, 65, 97, 66]
S - sum 761
B - binary [1, 0, 1, 1, 1, 1, 1, 0, 0, 1]
S - sum 7
Python3, 46 40 bytes
lambda s:bin(sum(map(ord,s))).count("1")
R, 65 53 43 42 bytes
Counting the number of 1s in a binary representation is calculating the Hamming weight of a number, a common procedure in cryptanalysis. There's an R package boolfun
, described in this journal article, which handily has a function weight
which computes the Hamming weight of any (base 10) integer. That makes our job a lot easier! The rest of the code just sums over the integer representation of STDIN, before sending that to weight
.
boolfun::weight(sum(utf8ToInt(scan(,'')))
As an added bonus, this code works for UTF-8 as well as ASCII. There are 7 ones in the binary representation of the sum of 高尔夫 (that's golf in Mandarin).
A note for those playing along at home: unfortunately, boolfun
is no longer hosted on CRAN, so if you don't already have it installed you'll have to do it manually with install.packages("https://cran.r-project.org/src/contrib/Archive/boolfun/boolfun_0.2.8.tar.gz", repos = NULL, type = "source")
, rather than install.packages(boolfun)
.
If you really don't want to use boolfun
, you can achieve the same result with only two more bytes:
sum(intToBits(sum(utf8ToInt(scan(,''))))==1)