Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Write a code that takes a string as input, and outputs a truthy or falsey value depending on whether or not the string follows these rules:

If you stack each character on top of each other, convert to binary and sums each column, then all the sum should be identical. You may assume the input string contain only printable ASCII-characters (code points 32 - 126).

As an example:

The input O5vy_+~ should return a truthy value, since its binary representation is:

1001111  | O
0110101  | 5
1110110  | v
1111001  | y
1011111  | _
0101011  | +
1111110  | ~
-------
5555555  <- Sum of bits in each column. Should give a truthy value.

The input PPCG should return a falsey value, since its binary representation is:

1010000  | P
1010000  | P
1000011  | C
1000111  | G
-------
4020122  <- Should give a falsey value

The twist is: Your code should return a truthy value if it's used as input to your function / program. I.e. the code must adhere to the same rules as above (your code can contain characters that are not ASCII 32-126).

Your program/function only needs to handle printable ASCII as input. If your code contains something else, 8 bit, 16 bit encoding, Unicode, a custom made character set (or something else), then the binary representation of it should adhere to the same rules, but your code doesn't need to handle it as input.

This is , so standard rules apply.

share|improve this question
    
How long will the input string be? Can we assume the sum will always be 7 digits long? – Okx 16 hours ago
    
Also, if our program uses characters other than ASCII characters, what happens? – Okx 16 hours ago
    
I guess that "then the binary representation of it should adhere to the same rules" should explicitly exclude the clause "only needs to handle printable ASCII as input" (otherwise one could write code with just one byte that maps to non-printable ASCII). – Jonathan Allan 16 hours ago
    
@Okx you may assume the input string is less than 1kB. The input will only be printable ASCII that can be represented using 7 bits, so yes: There will always be 7 integer (not necessarily digits) sums. – Stewie Griffin 15 hours ago
1  
@StewieGriffin That's not a very good clarification. If I have a non-ASCII answer, and you try and input the program into the program, and it doesn't work because it only supports ASCII, what happens? – Okx 15 hours ago

MATL, 10 9 bytes

BXs&=?I&]

Input is a string enclosed with single qoutes (if the input contains single qoutes, escape them by duplicating).

Output is 3 as truthy and nothing (empty output) as falsy.

Try it online!

The code in binary is as follows:

B     1 0 0 0 0 1 0
X     1 0 1 1 0 0 0
s     1 1 1 0 0 1 1
&     0 1 0 0 1 1 0
=     0 1 1 1 1 0 1
?     0 1 1 1 1 1 1
I     1 0 0 1 0 0 1
&     0 1 0 0 1 1 0
]     1 0 1 1 1 0 1

Sum   5 5 5 5 5 5 5

Explanation

B      % Implicit input. Convert each char to its ASCII code, and then to
       % binary. This gives a binary matrix, with each char of the input
       % corresponding to a row
Xs     % Sum of each column. Gives a row vector
&=     % All pairwise equality comparisons
?      % If all are true
  I    %    Push 3
  &    %    Specify that the next function, namely implicit display, will 
       %    take one input, instead of the whole stack which is the default
]      % End
       % Display (implicit)
share|improve this answer

JavaScript (ES6), 123 122 120 110 bytes

S=>[...S].map(S=>R.map((_GSSSSSSVWWW,V)=>R[V]-=S.charCodeAt()>>V&1),R=[_=3^3,_,_,_,_,_,_])&&!R.some(S=>S^R[_])

Below is a hexdump with bit sums.

Addr. | Dump                                            | #6 #5 #4 #3 #2 #1 #0
------+-------------------------------------------------+---------------------
00-0F | 53 3D 3E 5B 2E 2E 2E 53 5D 2E 6D 61 70 28 53 3D |  8 11  9 11  9  9  9
10-1F | 3E 52 2E 6D 61 70 28 28 5F 47 53 53 53 53 53 53 | 20 18 19 17 14 20 19
20-2F | 56 57 57 57 2C 56 29 3D 3E 52 5B 56 5D 2D 3D 53 | 30 24 32 25 26 30 29
30-3F | 2E 63 68 61 72 43 6F 64 65 41 74 28 29 3E 3E 56 | 41 37 37 32 34 38 36
40-4F | 26 31 29 2C 52 3D 5B 5F 3D 33 5E 33 2C 5F 2C 5F | 47 47 48 43 44 47 46
50-5F | 2C 5F 2C 5F 2C 5F 2C 5F 5D 29 26 26 21 52 2E 73 | 54 57 55 54 56 56 54
60-6D | 6F 6D 65 28 53 3D 3E 53 5E 52 5B 5F 5D 29       | 64 64 64 64 64 64 64

Demo

let f =

S=>[...S].map(S=>R.map((_GSSSSSSVWWW,V)=>R[V]-=S.charCodeAt()>>V&1),R=[_=3^3,_,_,_,_,_,_])&&!R.some(S=>S^R[_])

console.log(f("O5vy_+~"))
console.log(f("Hello World!"))
console.log(f(`S=>[...S].map(S=>R.map((_GSSSSSSVWWW,V)=>R[V]-=S.charCodeAt()>>V&1),R=[_=3^3,_,_,_,_,_,_])&&!R.some(S=>S^R[_])`))

share|improve this answer

Mathematica, 88 bytes

Total@IntegerDigits[ToCharacterCode@#,2,7]~MatchQ~{"?";a_ ..}&

Contains many unprintable characters between the quotes. Has 49 of each bit.

Here's the hexdump:

0000-0010:  54 6f 74 61-6c 40 49 6e-74 65 67 65-72 44 69 67  Total@In tegerDig
0000-0020:  69 74 73 5b-54 6f 43 68-61 72 61 63-74 65 72 43  its[ToCh aracterC
0000-0030:  6f 64 65 40-23 2c 32 2c-37 5d 7e 4d-61 74 63 68  ode@#,2, 7]~Match
0000-0040:  51 7e 7b 22-3f 1f 1f 1f-1f 1f 1f 1f-1f 1f 1f 1f  Q~{"?... ........
0000-0050:  1f 1f 1f 1f-1f 1a 1a 1a-1a 18 18 18-18 18 10 22  ........ ......."
0000-0058:  3b 61 5f 20-2e 2e 7d 26                          ;a_...}&
share|improve this answer

Jelly, 11 10 bytes

OBUSE&889.

Try It Online! Or see tests and self-input (the code is all printable ASCII, which have the same values in Jelly's code page, as seen below).

Char -> Hex -> Decimal -> Binary
O       0x4F   79         0b1001111
B       0x42   66         0b1000010
U       0x55   85         0b1010101
S       0x53   83         0b1010011
E       0x45   69         0b1000101
&       0x26   38         0b0100110
8       0x38   56         0b0111000
8       0x38   56         0b0111000
9       0x39   57         0b0111001
.       0x2E   46         0b0101110
                            -------
                            5555555

How?

OBUSE&889. - Main link: string
O          - cast to ordinals
 B         - convert to binary
  U        - upend (reverses each to prepare for vectorised sum)
   S       - sum (vectorises)
    E      - all equal? (yields 1 if all bit-sums are equal and 0 if not)
      889. - 889.0
     &     - bitwise and (1 & 889.0 is 1; and 0 & 889.0 is 0)
share|improve this answer
    
Your code looks much like Dennis's. – Erik the Outgolfer 3 hours ago
    
Yeah, I saw after. – Jonathan Allan 3 hours ago

Jelly, 11 10 bytes

OBUSE$*8?8

Uses no no-ops or comments.

Try it online!

Binary breakdown

O  1 0 0 1 1 1 1
B  1 0 0 0 0 1 0
U  1 0 1 0 1 0 1
S  1 0 1 0 0 1 1
E  1 0 0 0 1 0 1
$  0 1 0 0 1 0 0
*  0 1 0 1 0 1 0
8  0 1 1 1 0 0 0
?  0 1 1 1 1 1 1
8  0 1 1 1 0 0 0
————————————————
∑  5 5 5 5 5 5 5

How it works

OBUSE$*8?8  Main link. Argument: s (string)

O           Ordinal; map all characters in s to their code points.
 B          Binary; convert each code point to base 2.
  U         Upend; reverse each binary array to right-align the digits.
       8?   If 8 is non-zero (it is):
   SE$          Sum the corresponding digits and test the the sums for equality.
            Else (never happens):
      *  8      Raise all binary digits to the eighth power.
share|improve this answer
    
Is Jelly TC using only printable ASCII? – Pavel 13 hours ago
    
I don't think so. – Dennis 12 hours ago

JavaScript (ES6), 139 111 bytes

f=
S=>![...""+1E6].some((_______________BBBPPWWWWWWWWW,P)=>P*=R-(R=0,[...S].map(Q=>R+=Q.charCodeAt()>>P&1),R),R=0)
<textarea oninput=o.textContent=f(this.value) style=width:100% rows=10>S=>![...""+1E6].some((_______________BBBPPWWWWWWWWW,P)=>P*=R-(R=0,[...S].map(Q=>R+=Q.charCodeAt()>>P&1),R),R=0)</textarea><div id=o>true

Contains 81 63 of each bit.

share|improve this answer

Octave, 52 bytes

Making a complete rewrite helped me golf the code 5 bytes, but I had to add more no-ops, making it a net-save of only 1 byte.

@(_)~diff(sum(de2bi(+_)))%RRPPPVVVW?????????________

I can't add a TIO-link, since none of the online interpreters have implemented the communication toolbox necessary for de2bi. Changing it to dec2bin instead would cost 4 bytes (2 for working code, and two no-ops).

I found no way to avoid any of the 27 no-ops. All function names and parentheses are between either below 64, or higher than 96, meaning all "necessary" characters have a 1 in the 6th position (from the right, 2^5). I had a solution with only 23 no-ops, but the code itself was longer. The actual code is 25 bytes, and has the following column sum when counting the bits of the binary equivalent:

15   22    6   15   10    9   13

There are 22 bits in the 6th position from the right (2^5), and only 6 bits in the 4th position from the right (2^3). That means, we have to add at least 16 bytes, to get the 6 up to 22. Now, the comment character % adds a bit to the 6th position, increasing it to 23. All printable ASCII-characters needs at least one of the two top bits to be 1. Therefore, adding 17 bytes will give us at least 27 bits in each of the two "top spots" (2^6 and 2^5). Now, we have 27 bits in the top two spots, and 22 in the rest. In order to get to an equilibrium, we have to add 10 bytes, to get to an even 32 bits in each position.

An explanation of the new code (52 bytes):

@(_)~diff(sum(de2bi(+_)))
@(_)      % An anonymous function that take a variable _ as input
          % We use underscore, instead of a character, since it has the
          % most suitable binary represetation
              de2bi(+_)    % Convert the input string to a binary matrix
          sum(de2bi(+_))   % Take the sum of each column
     diff(sum(de2bi(+_)))  % And calculate the difference between each sum
    ~diff(sum(de2bi(+_)))  % Negate the result, meaning 0 becomes true, 
                           % and everything else becomes false

A vector containing only 1s (true) is evaluated to true in Octave, and a vector containing at least one zero is evaluated to false in Octave.

An explanation of the old code (53 bytes):

@(_)!((_=sum(de2bi(+_)))-_(1))%RRRFVVVVVVVVV_____????

@(_)      % An anonymous function that take a variable _ as input
          % We use underscore, instead of a character, since it has the
          % most suitable binary represetation
    !     % Negate the result, meaning 0 becomes true, and everything else becomes false
        de2bi(+_)         % Convert the input string to a binary matrix
    sum(de2bi(+_))        % Take the sum of each column
 (_=sum(de2bi(+_)))       % Assign the result to a new variable, also called _
                          % It's not a problem that we use the same variable name, due
                          % to the order of evaluation
((_=sum(de2bi(+_)))-_(1)) % Subtract the first element of the new variable _
                          % If all elements of the new variable _ are identical, then this
                          % should give us a vector containing only zeros,
                          % otherwise, at least one element should be non-zero
!((_=sum(de2bi(+_)))-_(1))  % And finally, we negate this.

A vector containing only 1s (true) is evaluated to true in Octave, and a vector containing at least one zero is evaluated to false in Octave.

share|improve this answer

Python 2, 117 bytes

All "spaces" are tabs to reduce number of 0x20 bits.

def Y(S):
    O=map(sorted,zip(*['{:07b}'.format(ord(W))for   W   in  S]))
    return  O[1:]==O[:-1]#V_____________

Contains 66 of each bit. (Due to a bug in Python I can't use '%07b'.)

Hex dump:

00000000: 64 65 66 09 59 28 53 29 3a 0a 09 4f 3d 6d 61 70  def.Y(S):..O=map
00000010: 28 73 6f 72 74 65 64 2c 7a 69 70 28 2a 5b 27 7b  (sorted,zip(*['{
00000020: 3a 30 37 62 7d 27 2e 66 6f 72 6d 61 74 28 6f 72  :07b}'.format(or
00000030: 64 28 57 29 29 66 6f 72 09 57 09 69 6e 09 53 5d  d(W))for.W.in.S]
00000040: 29 29 0a 09 72 65 74 75 72 6e 09 4f 5b 31 3a 5d  ))..return.O[1:]
00000050: 3d 3d 4f 5b 3a 2d 31 5d 23 56 5f 5f 5f 5f 5f 5f  ==O[:-1]#V______
00000060: 5f 5f 5f 5f 5f 5f 5f 16 16 16 16 16 16 16 16 16  _______.........
00000070: 16 16 14 14 10                                   .....
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.