Detect the Nearly Perfect Licence Plates
Jelly, 29 28 30 bytes
+1 byte to fix a bug spotted by ChristianSievers (incorrectly dealing with substrings of only zeros)
+1 byte to fix false positives for "0"
, "00"
, ... found during above fixing (0 is a perfect square).
i@€ØAS;Ʋ$
e€ØAœpV€€LÐfS€P;0⁼Ç
Try it online!, or run tests
How?
i@€ØAS;Ʋ$ - Link 1: [letter-sum, letter-sum is perfect square?]: plate
i@€ - index of €ach char in plate [reversed @rguments] (1-based, 0 otherwise) in:
ØA - uppercase alphabet
S - sum
$ - last two links as a monad:
; - concatenate with:
Ʋ - is square?
e€ØAœpV€€LÐfS€P;0⁼Ç - Main link: plate e.g. "11BB2"
œp - partition plate at truthy values of:
e€ - is in? for €ach char in plate:
ØA - uppercase alphabet [['1','1'],[''],['2']]
V€€ - evaluate for €ach for €ach [[1,1],[],[2]]
Ðf - filter keep:
L - length [[1,1],[2]]
S€ - sum each [2,2]
P - product 4
;0 - concatenate a zero [4,0]
Ç - last link (1) as a monad (taking plate) [4,1]
⁼ - equal? (non-vectorising) 0
MATL, 36 34 33 35 bytes
3Y432YXU"@V!Usvp]GlY2&msy=wtQ:qUm~v
Try it at MATL Online
Explanation
% Implicitly grab input as a string
3Y4 % Push the predefined literal '[A-Za-z]+' to the stack
32 % Push the literal 32 to the stack (ASCII for ' ')
YX % Replace the matched regex with spaces (puts a space in place of all letters)
U % Convert the string to a number. The spaces make it such that each group of
% of consecutive digits is made into a number
" % For each of these numbers
@V!U % Break it into digits
s % Sum the digits
v % Vertically concatenate the entire stack
p % Compute the product of this vector
] % End of for loop
G % Explicitly grab the input again
lY2 % Push the predefined literal 'ABCD....XYZ' to the stack
&m % Check membership of each character in the input in this array and
% return an array that is 0 where it wasn't a letter and the index in 'ABC..XYZ'
% when it was a letter
s % Sum the resulting vector
y % Duplicate the product of the sums of digits result
= % Compare to the sum of letter indices result
w % Flip the top two stack elements
Q % Add one to this value (N)
t: % Duplicate and compute the array [1...N]
q % Subtract 1 from this array to yield [0...N-1]
U % Square all elements to create all perfect squares between 1 and N^2
m~ % Check to ensure that N is not in the array of perfect squares
v % Vertically concatenate the stack.
% Implicitly display the truthy/falsey result
Python 2, 120 118 bytes
s=t=p=0;r=1
for n in input():
h=int(n,36)
if h>9:s+=h-9;r*=t**p
p=h<10;t=(t+h)*p
print(s==r*t**p)&(int(s**.5)**2<s)
Try it online!
Interprets each character as a number in base-36 (h
). Converts to decimal and adds to the sum if h>9
(meaning it's a letter), otherwise adds to a variable which gets multiplied to form the running product later.