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

Mediocre pop count

+3
−0

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 1s in it.

For this challenge, the pop count of a letter is the number of 1s 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 1s 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.

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?

0 comment threads

6 answers

You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.

+0
−0

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())

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−0

APL (Dyalog APL), 61 bytes

{⍵/⍨~(⊢∊⌊/,⌈/)+⌿0 1↓2⊥⍣¯1⊢0,⎕UCS⍵}

Attempt This Online!

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:

⍵/⍨~
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−0

Japt, 13 bytes

ÆüÈc¤ñÃé ¤¬øX

Try it

ÆüÈ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?
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−0

Vyxal, 12 bytes

bṠ:₍gGvc†Tİ∑

Try it Online!

          İ  # 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
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−0

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
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+0
−0

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))]

Attempt This Online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »