APL(NARS), chars 46, bytes 92
{(⊂,⍺)∊(⊂''),↑¨,/¨{⍵⊂w}¨{⍵⊤⍨k⍴2}¨⍳¯1+2*k←≢w←⍵}
test:
h←{(⊂,⍺)∊(⊂''),↑¨,/¨{⍵⊂w}¨{⍵⊤⍨k⍴2}¨⍳¯1+2*k←≢w←⍵}
'' h 'z00'
1
'z00' h 'z00'
1
'z00' h '00z0'
0
'aa' h 'anna'
1
'anna' h 'banana'
1
'Anna' h 'banana'
0
comment:
{(⊂,⍺)∊(⊂''),↑¨,/¨{⍵⊂w}¨{⍵⊤⍨k⍴2}¨⍳¯1+2*k←≢w←⍵}
⍳¯1+2*k←≢w←⍵ this assign to w the argument and k argument lenght, it return 1..(2^k)-1 range
{⍵⊤⍨k⍴2}¨ for each element of 1..(2^k)-1 convert in base 2 with lenght k (as the arg lenght)
{⍵⊂w}¨ use the binary array above calculation for all partition argument
,/¨ concatenate each element of partition
↑¨ get the firs element of each element because they are all enclosed
(⊂''), add to the array the element (⊂'')
(⊂,⍺)∊ see if (⊂,⍺) is one element of the array, and return 1 true o 0 false
How all you can see the comments are +- superfluous all is easy...
I have to say not understand why the last instruction is not "(,⍺)∊" in the place of "(⊂,⍺)∊"
because for example in code
q←{↑¨,/¨{⍵⊂w}¨{⍵⊤⍨k⍴2}¨⍳¯1+2*k←≢w←⍵}
o q '123'
┌7──────────────────────────────────────┐
│┌1─┐ ┌1─┐ ┌2──┐ ┌1─┐ ┌2──┐ ┌2──┐ ┌3───┐│
││ 3│ │ 2│ │ 23│ │ 1│ │ 13│ │ 12│ │ 123││
│└──┘ └──┘ └───┘ └──┘ └───┘ └───┘ └────┘2
└∊──────────────────────────────────────┘
o (,'3')
┌1─┐
│ 3│
└──┘
all you see array of 1 element (,'3') is the element of the set of instruction q '123', but
o (,'3')∊q '123'
┌1─┐
│ 0│
└~─┘
return one array of unclosed 0 instead of number 1... for workaround that one has to write:
o (⊂,'3')∊q '123'
1
~
that is the right result even if the element seems different because :
o (⊂,'3')
┌────┐
│┌1─┐│
││ 3││
│└──┘2
└∊───┘
It is sure i make some error because i am not so smart... where is my error?
1Why is 'anna' substr of 'banana'? – kaoD – 2012-04-26T05:04:43.933
4@kaoD -
anna
is a subsequence (but *not* a substring) ofbanana
. String X is a subsequence of string Y just if X can be obtained from Y by deleting zero or more of the elements of Y; e.g., deleting theb
and the seconda
frombanana
givesanna
. – r.e.s. – 2012-04-26T13:33:16.1802This has about a single solution in every scripting language offering regex that's both trivial to see and impossible to golf further. – Joey – 2012-04-27T14:50:36.693