Skip to main content
added explanation
Source Link
Neil
  • 184.3k
  • 12
  • 76
  • 290

JavaScript (ES2017), 346 bytes

f=(p,q,a=p?q.split(p[0]):[q])=>p[1]?f(p.slice(1),a.join(p[1])):(s=a.filter(e=>a.indexOf(e)==a.lastIndexOf(e)).map(e=>e.length).sort((a,b)=>a-b),t=l=0,r=[],s.map(e=>r[t+=e,l++,e]=-~r[e]),[l&&t/l,l&&(s[l>>1]+s[l-1>>1])/2,[...m=new Set(s.filter(e=>r[e]==Math.max(...Object.values(r))))],[a.filter(e=>!m.has(e.length)),a.filter(e=>m.has(e.length))]])

For ES6 replace Object.values(r) with Object.keys(r).map(k=>r[k]) at a cost of 11 bytes. Explanation:

f=(p,q,a=p?q.split(p[0]):[q])=>         split on first seprator
 p[1]?f(p.slice(1),a.join(p[1])):       recurse for each separator
  (s=a.filter(e=>                       search through list for
     a.indexOf(e)==a.lastIndexOf(e))    unique strings only
    .map(e=>e.length)                   get lengths of each string
    .sort((a,b)=>a-b),                  sort lengths into order
   t=l=0,                               total and length
   r=[],                                frequency table
   s.map(e=>r[t+=e,l++,e]=-~r[e]),      build frequency table and total
   [l&&t/l                              mean = total / length
   ,l&&(s[l>>1]+s[l-1>>1])/2            median = mean of middle elements
   ,[...m=new Set(s.filter(e=>r[e]==    unique elements matching
    Math.max(...Object.values(r))))]    maximum length frequency
   ,[a.filter(e=>!m.has(e.length))      filter on non-modal length
    ,a.filter(e=>m.has(e.length))]])    filter on modal length

JavaScript (ES2017), 346 bytes

f=(p,q,a=p?q.split(p[0]):[q])=>p[1]?f(p.slice(1),a.join(p[1])):(s=a.filter(e=>a.indexOf(e)==a.lastIndexOf(e)).map(e=>e.length).sort((a,b)=>a-b),t=l=0,r=[],s.map(e=>r[t+=e,l++,e]=-~r[e]),[l&&t/l,l&&(s[l>>1]+s[l-1>>1])/2,[...m=new Set(s.filter(e=>r[e]==Math.max(...Object.values(r))))],[a.filter(e=>!m.has(e.length)),a.filter(e=>m.has(e.length))]])

For ES6 replace Object.values(r) with Object.keys(r).map(k=>r[k]) at a cost of 11 bytes.

JavaScript (ES2017), 346 bytes

f=(p,q,a=p?q.split(p[0]):[q])=>p[1]?f(p.slice(1),a.join(p[1])):(s=a.filter(e=>a.indexOf(e)==a.lastIndexOf(e)).map(e=>e.length).sort((a,b)=>a-b),t=l=0,r=[],s.map(e=>r[t+=e,l++,e]=-~r[e]),[l&&t/l,l&&(s[l>>1]+s[l-1>>1])/2,[...m=new Set(s.filter(e=>r[e]==Math.max(...Object.values(r))))],[a.filter(e=>!m.has(e.length)),a.filter(e=>m.has(e.length))]])

For ES6 replace Object.values(r) with Object.keys(r).map(k=>r[k]) at a cost of 11 bytes. Explanation:

f=(p,q,a=p?q.split(p[0]):[q])=>         split on first seprator
 p[1]?f(p.slice(1),a.join(p[1])):       recurse for each separator
  (s=a.filter(e=>                       search through list for
     a.indexOf(e)==a.lastIndexOf(e))    unique strings only
    .map(e=>e.length)                   get lengths of each string
    .sort((a,b)=>a-b),                  sort lengths into order
   t=l=0,                               total and length
   r=[],                                frequency table
   s.map(e=>r[t+=e,l++,e]=-~r[e]),      build frequency table and total
   [l&&t/l                              mean = total / length
   ,l&&(s[l>>1]+s[l-1>>1])/2            median = mean of middle elements
   ,[...m=new Set(s.filter(e=>r[e]==    unique elements matching
    Math.max(...Object.values(r))))]    maximum length frequency
   ,[a.filter(e=>!m.has(e.length))      filter on non-modal length
    ,a.filter(e=>m.has(e.length))]])    filter on modal length
Source Link
Neil
  • 184.3k
  • 12
  • 76
  • 290

JavaScript (ES2017), 346 bytes

f=(p,q,a=p?q.split(p[0]):[q])=>p[1]?f(p.slice(1),a.join(p[1])):(s=a.filter(e=>a.indexOf(e)==a.lastIndexOf(e)).map(e=>e.length).sort((a,b)=>a-b),t=l=0,r=[],s.map(e=>r[t+=e,l++,e]=-~r[e]),[l&&t/l,l&&(s[l>>1]+s[l-1>>1])/2,[...m=new Set(s.filter(e=>r[e]==Math.max(...Object.values(r))))],[a.filter(e=>!m.has(e.length)),a.filter(e=>m.has(e.length))]])

For ES6 replace Object.values(r) with Object.keys(r).map(k=>r[k]) at a cost of 11 bytes.