Without much ado, today’s problem - Put it in Reverse
The find function X⍷Y identifies the beginnings of occurrences of array X in array Y.
In this problem, you’re asked to return a result that identifies the endings of occurrences of array X in array Y. To keep things simple, X and Y will be at most rank 1, meaning they’ll either be vectors or scalars.
Write a function that:
- takes a scalar or vector left argument
- takes a scalar or vector right argument
- returns a Boolean result that is the same shape as the right argument where 1’s mark the ends of occurrences of the left argument in the right argument
Examples:
'abra' findEnd 'abracadabra'
0 0 0 1 0 0 0 0 0 0 1
'issi' findEnd 'Mississippi'
0 0 0 0 1 0 0 1 0 0 0
'bb' findEnd 'bbb bbb'
0 1 1 0 0 1 1
(,42) findEnd 42
0
42 findEnd 42
1
(,42) findEnd ,42
1
'are' 'aquatic' findEnd 'ducks' 'are' 'aquatic' 'avians'
0 0 1 0
Solution
See the solution
{⌽(⌽⍺)⍷⌽⍵}
- We use ⍷ to find beginning of reversed array
⌽⍺
in⌽⍵
- We reverse this to get the ending of ⍺ in ⍵
Taking example 'abra' {⌽(⌽⍺)⍷⌽⍵} 'abracadabra'
- For
'abra' ⍷ 'abracadabra'
, we get1 0 0 0 0 0 0 1 0 0 0
as'abra
matches twice at index 1 and 8 - When we reverse ‘abra’ and ‘abracadabra’ and do a find, we get start index of ‘arba’ in ‘arbadacarba’ i.e
1 0 0 0 0 0 0 1 0 0 0
- Finally, when we reverse this array, we get end index of ‘abra’ in ‘abracadabra’ i.e
0 0 0 1 0 0 0 0 0 0 1