2018-12-30 19:05:56 +05:30
---
id: 5a23c84252665b21eecc801d
title: Split a character string based on change of character
challengeType: 5
2019-08-05 09:17:33 -07:00
forumTopicId: 302322
2021-01-13 03:31:00 +01:00
dashedName: split-a-character-string-based-on-change-of-character
2018-12-30 19:05:56 +05:30
---
2020-11-27 19:02:05 +01:00
# --description--
2020-03-30 11:23:18 -05:00
2020-11-27 19:02:05 +01:00
Split a (character) string into comma (plus a blank) delimited strings based on a change of character (left to right). Blanks should be treated as any other character (except they are problematic to display clearly). The same applies to commas. For instance, the string:
2020-03-30 11:23:18 -05:00
2019-03-08 18:00:54 +09:00
< pre >
2018-12-30 19:05:56 +05:30
"gHHH5YY++///\"
2019-03-08 18:00:54 +09:00
< / pre >
2020-03-30 11:23:18 -05:00
2018-12-30 19:05:56 +05:30
should be split as:
2020-03-30 11:23:18 -05:00
2019-03-08 18:00:54 +09:00
< pre >
["g", "HHH", "5", "YY", "++", "///", "\" ];
< / pre >
2020-11-27 19:02:05 +01:00
# --hints--
`split` should be a function.
```js
assert(typeof split == 'function');
2018-12-30 19:05:56 +05:30
```
2020-11-27 19:02:05 +01:00
`split("hello")` should return an array.
2018-12-30 19:05:56 +05:30
2020-11-27 19:02:05 +01:00
```js
assert(Array.isArray(split('hello')));
```
2020-03-30 11:23:18 -05:00
2020-11-27 19:02:05 +01:00
`split("hello")` should return `["h", "e", "ll", "o"]` .
2019-07-18 17:32:12 +02:00
2020-11-27 19:02:05 +01:00
```js
assert.deepEqual(split('hello'), ['h', 'e', 'll', 'o']);
```
`split("commission")` should return `["c", "o", "mm", "i", "ss", "i", "o", "n"]` .
2018-12-30 19:05:56 +05:30
```js
2020-11-27 19:02:05 +01:00
assert.deepEqual(split('commission'), [
'c',
'o',
'mm',
'i',
'ss',
'i',
'o',
'n'
]);
```
2020-09-15 09:57:40 -07:00
2020-11-27 19:02:05 +01:00
`split("ssss----====llloooo")` should return `["ssss", "----", "====", "lll", "oooo"]` .
```js
assert.deepEqual(split('ssss----====llloooo'), [
'ssss',
'----',
'====',
'lll',
'oooo'
]);
2018-12-30 19:05:56 +05:30
```
2020-11-27 19:02:05 +01:00
`split("sssmmmaaammmaaat")` should return `["sss", "mmm", "aaa", "mmm", "aaa", "t"]` .
2018-12-30 19:05:56 +05:30
2020-11-27 19:02:05 +01:00
```js
assert.deepEqual(split('sssmmmaaammmaaat'), [
'sss',
'mmm',
'aaa',
'mmm',
'aaa',
't'
]);
```
`split("gHHH5YY++///\")` should return `["g", "HHH", "5", "YY", "++", "///", "\\"]` .
```js
assert.deepEqual(split('gHHH5YY++///\\'), [
'g',
'HHH',
'5',
'YY',
'++',
'///',
'\\'
]);
```
2020-03-30 11:23:18 -05:00
2020-11-27 19:02:05 +01:00
# --seed--
## --seed-contents--
```js
function split(str) {
}
```
# --solutions--
2018-12-30 19:05:56 +05:30
```js
2019-03-08 18:00:54 +09:00
function split(str) {
2018-12-30 19:05:56 +05:30
const concat = xs =>
2020-03-30 11:23:18 -05:00
xs.length > 0
? (() => {
const unit = typeof xs[0] === 'string' ? '' : [];
return unit.concat.apply(unit, xs);
})()
: [];
2018-12-30 19:05:56 +05:30
const group = xs => groupBy((a, b) => a === b, xs);
const groupBy = (f, xs) => {
2020-03-30 11:23:18 -05:00
const dct = xs.slice(1).reduce(
(a, x) => {
const h = a.active.length > 0 ? a.active[0] : undefined,
2018-12-30 19:05:56 +05:30
blnGroup = h !== undefined & & f(h, x);
return {
active: blnGroup ? a.active.concat([x]) : [x],
sofar: blnGroup ? a.sofar : a.sofar.concat([a.active])
};
2020-03-30 11:23:18 -05:00
},
{
2018-12-30 19:05:56 +05:30
active: xs.length > 0 ? [xs[0]] : [],
sofar: []
2020-03-30 11:23:18 -05:00
}
);
2018-12-30 19:05:56 +05:30
return dct.sofar.concat(dct.active.length > 0 ? [dct.active] : []);
};
const map = (f, xs) => xs.map(f);
const stringChars = s => s.split('');
2020-03-30 11:23:18 -05:00
return map(concat, group(stringChars(str)));
2018-12-30 19:05:56 +05:30
}
```