Skip to main content
Bounty Awarded with 100 reputation awarded by Adám
deleted 276 characters in body
Source Link
voidhawk
  • 1.9k
  • 9
  • 5

APL (Dyalog Unicode), 1513 bytesSBCS

-2 bytes:

(∧/1=+/)∘≢=∘≢∘⍸∘.(⊃⍷)⍨ 

Try it online!Try it online!

Explanation:

(∧/1=+/)∘≢=∘≢∘⍸∘.(⊃⍷)⍨ ⍝ Monadic function train   ⍷ ⍝ "Find": Convert the right argument into a boolean vector,   ⍝ where ones correspond to instances of the left argument   ⊃ ⍝ Take the first item of the above vector (i.e., only prefixes)   ∘.( )⍨ ⍝ Commutative outer product: take the above function and apply   ⍝ it for each possible pair of elements in the input   ⍝ If the input is a prefix code, the above should behave equivalent a number of ones  equal to the identitylength matrixof the input (i.e., each elementitem is a prefix of only itself)   ⍝ To test that it's an identity matrixthis... ( +/) SumFind alongthe rowslocation of theall matrix ones in the 1=above  ≢∘  Test thatTake the sums are equal to 1, resultslength inof booleanthe vectorabove  ∧/ ≢=∘ And-reduce: testCompare thatto abovethe vectorlength onlyof containsthe onesinput 

APL (Dyalog Unicode), 15 bytesSBCS

(∧/1=+/)∘.(⊃⍷)⍨ 

Try it online!

Explanation:

(∧/1=+/)∘.(⊃⍷)⍨ ⍝ Monadic function train   ⍷ ⍝ "Find": Convert the right argument into a boolean vector,   ⍝ where ones correspond to instances of the left argument   ⊃ ⍝ Take the first item of the above vector (i.e., only prefixes)   ∘.( )⍨ ⍝ Commutative outer product: take the above function and apply   ⍝ it for each possible pair of elements in the input   ⍝ If the input is a prefix code, the above should be equivalent  ⍝ to the identity matrix (i.e., each element is a prefix of only itself)   ⍝ To test that it's an identity matrix... ( +/)Sum along rows of the matrix  1=  Test that the sums are equal to 1, results in boolean vector  ∧/  And-reduce: test that above vector only contains ones 

APL (Dyalog Unicode), 13 bytesSBCS

-2 bytes:

≢=∘≢∘⍸∘.(⊃⍷)⍨ 

Try it online!

Explanation:

≢=∘≢∘⍸∘.(⊃⍷)⍨ ⍝ Monadic function train ⍷ ⍝ "Find": Convert the right argument into a boolean vector, ⍝ where ones correspond to instances of the left argument ⊃ ⍝ Take the first item of the above vector (i.e., only prefixes) ∘.( )⍨ ⍝ Commutative outer product: take the above function and apply ⍝ it for each possible pair of elements in the input ⍝ If the input is a prefix code, the above should have a number of ones  equal to the length of the input (i.e., each item is a prefix of only itself) ⍝ To test this... Find the location of all ones in the above  ≢∘  Take the length of the above ≢=∘Compare to the length of the input 
Source Link
voidhawk
  • 1.9k
  • 9
  • 5

APL (Dyalog Unicode), 15 bytesSBCS

(∧/1=+/)∘.(⊃⍷)⍨ 

Try it online!

Explanation:

(∧/1=+/)∘.(⊃⍷)⍨ ⍝ Monadic function train ⍷ ⍝ "Find": Convert the right argument into a boolean vector, ⍝ where ones correspond to instances of the left argument ⊃ ⍝ Take the first item of the above vector (i.e., only prefixes) ∘.( )⍨ ⍝ Commutative outer product: take the above function and apply ⍝ it for each possible pair of elements in the input ⍝ If the input is a prefix code, the above should be equivalent ⍝ to the identity matrix (i.e., each element is a prefix of only itself) ⍝ To test that it's an identity matrix... ( +/) ⍝ Sum along rows of the matrix 1= ⍝ Test that the sums are equal to 1, results in boolean vector ∧/ ⍝ And-reduce: test that above vector only contains ones