string - Display number of spaces -
how should display number of words contained in string(vector)? there may multiple spaces between 2 words.
ex: input: ' hello world, how ' should return 5.
to count number of spaces in vector v, write: n←+/v∊' '
but, won't display number of words. anyone?
s←'hello world, how you?' a←s∊'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz' +/ (a,0)∧~0,a
a
vector contains 1-s @ positions letters occur , 0-s elsewhere.
s: h e l l o w o r l d , h o w r e y o u ? a: 1 1 1 1 1 0 1 1 1 1 1 0 0 1 1 1 0 1 1 1 0 1 1 1 0
every initial letter of word correspond 1 preceded 0 (or in initial position). well, if shift sequence right, negate it, , "and" original value of a
, can identify word-initial letters:
s: h e l l o w o r l d , h o w r e y o u ? a,0: 1 1 1 1 1 0 1 1 1 1 1 0 0 1 1 1 0 1 1 1 0 1 1 1 0 0 ~0,a: 1 0 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 1 0 0 0 1 0 0 0 1 (a,0)∧~0,a: 1 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0
now, counting words easy summation: +/
you can try code interpreter @ http://ngn.github.io/apl/web/index.html#code=s%u2190%27hello%20world%2c%20how%20are%20you%3f%27%0aa%u2190s%u220a%27abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz%27%0a+/%28a%2c0%29%u2227%7e0%2ca or http://tryapl.org
Comments
Post a Comment