Mediocre pop count
Given a sequence of letters, omit those with the highest or lowest pop count.
Terminology
The pop count or population count of a binary string is the number of 1
s in it.
For this challenge, the pop count of a letter is the number of 1
s in its ASCII character code in binary.
For example, the letter "Z" has ASCII character code 90, which is 1011010
in binary. There are 4 1
s in this binary string, so the pop count of "Z" is 4.
Here is the pop count of every letter, in the format letter : pop count
:
A : 2
B : 2
C : 3
D : 2
E : 3
F : 3
G : 4
H : 2
I : 3
J : 3
K : 4
L : 3
M : 4
N : 4
O : 5
P : 2
Q : 3
R : 3
S : 4
T : 3
U : 4
V : 4
W : 5
X : 3
Y : 4
Z : 4
a : 3
b : 3
c : 4
d : 3
e : 4
f : 4
g : 5
h : 3
i : 4
j : 4
k : 5
l : 4
m : 5
n : 5
o : 6
p : 3
q : 4
r : 4
s : 5
t : 4
u : 5
v : 5
w : 6
x : 4
y : 5
z : 5
Input
- A sequence of letters
- This may be a string or any ordered data structure of characters
- The letters may contain a mixture of upper and lower case
- The characters that count as letters for this challenge are A-Z and a-z, specifically
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
- The input may be empty (it may have length zero)
Output
- A sequence of letters
- This may be a string or any ordered data structure of characters. It does not need to match the input format (provided it is consistent between inputs)
- For example, you may take input as an array of characters, and output as a string, provided this does not change for different inputs
- All of the letters from the input are included in the output in the same order, except for those with the highest or lowest pop count, which are omitted
- If all of the letters have either the highest or lowest pop count, the output is empty (a sequence of length zero)
- Case must be maintained
- a letter that is lower case in the input must be lower case in the output (if present)
- a letter that is upper case in the input must be upper case in the output (if present)
Test cases
Test cases are in the format "input" : "output"
"" : ""
"a" : ""
"ab" : ""
"abc" : ""
"abcd" : ""
"abcde" : ""
"abcdef" : ""
"abcdefg" : "cef"
"A" : ""
"AB" : ""
"ABC" : ""
"ABCD" : ""
"ABCDE" : ""
"ABCDEF" : ""
"ABCDEFG" : "CEF"
"oAPwSoeHcoDretMBBoesoBsagHDoew" : "SecretMessage"
"SecretMessage" : "SecretMee"
Explanations are optional, but I'm more likely to upvote answers that have one.
Vyxal, 12 bytes ``` bṠ:₍gGvc …
2y ago
J, 46 char Solution: ``` …
2y ago
APL (Dyalog APL), 61 bytes …
2y ago
Japt, 13 bytes ÆüÈc¤ñÃé …
2y ago
JavaScript, 104 102 99 bytes …
2y ago
Python, 89 bytes ```python …
2y ago
6 answers
APL (Dyalog APL), 61 bytes
{⍵/⍨~(⊢∊⌊/,⌈/)+⌿0 1↓2⊥⍣¯1⊢0,⎕UCS⍵}
APL's style of filter works very well here, since we can check for the max and min elements here:
(⊢∊⌊/,⌈/)
and filter using the boolean mask later:
⍵/⍨~
0 comment threads
Vyxal, 12 bytes
bṠ:₍gGvc†Tİ∑
İ # Index into input
T # Indices where
v # For each
Ṡ # Sum of
b # Binary digits
₍gG # Does [min, max]
† # Not
c # contain
: # That value?
∑ # Concatenate into a single string
0 comment threads
J, 46 char
Solution:
(]#~[:([:*./(<./,>./)~:"0 1])([:+/"1@#:a.&i.))
Test example:
(]#~[:([:*./(<./,>./)~:"0 1])([:+/"1@#:a.&i.)) 'oAPwSoeHcoDretMBBoesoBsagHDoew'
SecretMessage
How it works:
a.&i. converts from text to ascii number
#: converts from ascii number to binary
+/"1 adds the 1s in the binary numbers
(<./,>./) finds greatest and lowest values in lists
*./(<./,>./)~:"0 1 produces boolean indicating locations of non-greatest and non-lowest values
#~ selects from original text locations indicated by boolean
0 comment threads
Japt, 13 bytes
ÆüÈc¤ñÃé ¤¬øX
ÆüÈc¤ñÃé ¤¬øX :Implicit input of string U
Æ :Filter each character, X by
ü : Group & sort U by
È : Passing each character through the following function
c : Charcode
¤ : To binary string
ñ : Sort
à : End grouping
é : Rotate right
¤ : Slice off the first 2 elements
¬ : Join
øX : Contains X?
0 comment threads
JavaScript, 104 102 99 bytes
I/O as a character array.
a=>a.filter(x=>a<g(x)&g(x)<a[~-a.length],a=a.map(g=x=>[...Buffer(x)[0].toString(2)].sort()).sort())
0 comment threads
Python, 89 bytes
lambda l,g=lambda c:ord(c).bit_count():[i for i in l if min(map(g,l))<g(i)<max(map(g,l))]
0 comment threads