Cody

Problem 967. Split a string into chunks of specified length

Solution 2983969

Submitted on 23 Sep 2020 at 17:56 by ivan fernandez
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test Status Code Input and Output
1   Pass
s = 'Easy'; b = ones(1,4); y_correct = {'E','a','s','y'}; assert(isequal(break_string(s,b),y_correct))

celle = 0×0 empty cell array indice1 = 1 newS = 1×4 string array "" "" "" "" newS = 1×4 string array "E" "" "" "" indice1 = 2 newS = 1×4 string array "E" "a" "" "" indice1 = 3 newS = 1×4 string array "E" "a" "s" "" indice1 = 4 newS = 1×4 string array "E" "a" "s" "y" indice1 = 5 celle = 1×4 cell array {'E'} {'a'} {'s'} {'y'} y = 1×4 cell array {'E'} {'a'} {'s'} {'y'}

2   Pass
s = 'I seem to be having tremendous difficulty with my lifestyle'; b = [4 1 5 22]; y_correct = {'I se','e','m to ','be having tremendous d'}; assert(isequal(break_string(s,b),y_correct))

celle = 0×0 empty cell array indice1 = 1 newS = 1×4 string array "" "" "" "" newS = 1×4 string array "I se" "" "" "" indice1 = 5 newS = 1×4 string array "I se" "e" "" "" indice1 = 6 newS = 1×4 string array "I se" "e" "m to " "" indice1 = 11 newS = 1×4 string array "I se" "e" "m to " "be having tremendous d" indice1 = 33 celle = 1×4 cell array {'I se'} {'e'} {'m to '} {'be having tremendous d'} y = 1×4 cell array {'I se'} {'e'} {'m to '} {'be having tremendous d'}

3   Pass
s = 'The answer is 42'; b = [4 7 3]; y_correct = {'The ','answer ','is '}; assert(isequal(break_string(s,b),y_correct))

celle = 0×0 empty cell array indice1 = 1 newS = 1×3 string array "" "" "" newS = 1×3 string array "The " "" "" indice1 = 5 newS = 1×3 string array "The " "answer " "" indice1 = 12 newS = 1×3 string array "The " "answer " "is " indice1 = 15 celle = 1×3 cell array {'The '} {'answer '} {'is '} y = 1×3 cell array {'The '} {'answer '} {'is '}

4   Pass
s = 'Did I do that?'; b = [2 3 3]; y_correct = {'Di','d I',' do'}; assert(isequal(break_string(s,b),y_correct))

celle = 0×0 empty cell array indice1 = 1 newS = 1×3 string array "" "" "" newS = 1×3 string array "Di" "" "" indice1 = 3 newS = 1×3 string array "Di" "d I" "" indice1 = 6 newS = 1×3 string array "Di" "d I" " do" indice1 = 9 celle = 1×3 cell array {'Di'} {'d I'} {' do'} y = 1×3 cell array {'Di'} {'d I'} {' do'}

5   Pass
s = 'Yes no when?'; b = [2 6 3]; y_correct = {'Ye','s no w','hen'}; assert(isequal(break_string(s,b),y_correct))

celle = 0×0 empty cell array indice1 = 1 newS = 1×3 string array "" "" "" newS = 1×3 string array "Ye" "" "" indice1 = 3 newS = 1×3 string array "Ye" "s no w" "" indice1 = 9 newS = 1×3 string array "Ye" "s no w" "hen" indice1 = 12 celle = 1×3 cell array {'Ye'} {'s no w'} {'hen'} y = 1×3 cell array {'Ye'} {'s no w'} {'hen'}

6   Pass
s = 'Hello there'; b = [3 4 4]; y_correct = {'Hel','lo t','here'}; assert(isequal(break_string(s,b),y_correct))

celle = 0×0 empty cell array indice1 = 1 newS = 1×3 string array "" "" "" newS = 1×3 string array "Hel" "" "" indice1 = 4 newS = 1×3 string array "Hel" "lo t" "" indice1 = 8 newS = 1×3 string array "Hel" "lo t" "here" indice1 = 12 celle = 1×3 cell array {'Hel'} {'lo t'} {'here'} y = 1×3 cell array {'Hel'} {'lo t'} {'here'}

7   Pass
s = 'One is not enough!'; b = [3 1 1]; y_correct = {'One',' ','i'}; assert(isequal(break_string(s,b),y_correct))

celle = 0×0 empty cell array indice1 = 1 newS = 1×3 string array "" "" "" newS = 1×3 string array "One" "" "" indice1 = 4 newS = 1×3 string array "One" " " "" indice1 = 5 newS = 1×3 string array "One" " " "i" indice1 = 6 celle = 1×3 cell array {'One'} {' '} {'i'} y = 1×3 cell array {'One'} {' '} {'i'}

Suggested Problems

More from this Author1

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!