Cody

Problem 65. Word Counting and Indexing

Solution 2674979

Submitted on 10 Jul 2020 by 春樹 内糸
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
sl = {'one two three','two one four zero'}; wtc = {'four','one','three','two','zero'}; silc = {[2 4 3],[4 2 1 5]}; [wt,sil] = word_index(sl); assert(isequal(wt,wtc))

lstr = 1×1 cell array {'one two three'} sp = 1×3 cell array {'one'} {'two'} {'three'} word_table = 1×3 cell array {'one'} {'two'} {'three'} lstr = 1×1 cell array {'two one four zero'} sp = 1×4 cell array {'two'} {'one'} {'four'} {'zero'} word_table = 1×7 cell array {'one'} {'two'} {'three'} {'two'} {'one'} {'four'} {'zero'} word_table = 1×5 cell array {'four'} {'one'} {'three'} {'two'} {'zero'} lstr = 1×1 cell array {'one two three'} sp = 1×3 cell array {'one'} {'two'} {'three'} idxs = [] str = 'one' idc = 1×5 cell array {0×0 double} {[1]} {0×0 double} {0×0 double} {0×0 double} idx = 1×5 logical array 0 1 0 0 0 idxs = 0 1 0 0 0 str = 'two' idc = 1×5 cell array {0×0 double} {0×0 double} {0×0 double} {[1]} {0×0 double} idx = 1×5 logical array 0 0 0 1 0 idxs = 0 1 0 0 0 0 0 0 1 0 str = 'three' idc = 1×5 cell array {0×0 double} {0×0 double} {[1]} {0×0 double} {0×0 double} idx = 1×5 logical array 0 0 1 0 0 idxs = 0 1 0 0 0 0 0 0 1 0 0 0 1 0 0 str_index_list = 1×1 cell array {1×15 double} lstr = 1×1 cell array {'two one four zero'} sp = 1×4 cell array {'two'} {'one'} {'four'} {'zero'} idxs = [] str = 'two' idc = 1×5 cell array {0×0 double} {0×0 double} {0×0 double} {[1]} {0×0 double} idx = 1×5 logical array 0 0 0 1 0 idxs = 0 0 0 1 0 str = 'one' idc = 1×5 cell array {0×0 double} {[1]} {0×0 double} {0×0 double} {0×0 double} idx = 1×5 logical array 0 1 0 0 0 idxs = 0 0 0 1 0 0 1 0 0 0 str = 'four' idc = 1×5 cell array {[1]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} idx = 1×5 logical array 1 0 0 0 0 idxs = 0 0 0 1 0 0 1 0 0 0 1 0 0 0 0 str = 'zero' idc = 1×5 cell array {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[1]} idx = 1×5 logical array 0 0 0 0 1 idxs = 0 0 0 1 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 1 str_index_list = 1×2 cell array {1×15 double} {1×20 double}

2   Pass
sl = {'one two three'}; wtc = {'one','three','two'}; silc = {[1 3 2]}; [wt,sil] = word_index(sl); assert(isequal(wt,wtc))

lstr = 1×1 cell array {'one two three'} sp = 1×3 cell array {'one'} {'two'} {'three'} word_table = 1×3 cell array {'one'} {'two'} {'three'} word_table = 1×3 cell array {'one'} {'three'} {'two'} lstr = 1×1 cell array {'one two three'} sp = 1×3 cell array {'one'} {'two'} {'three'} idxs = [] str = 'one' idc = 1×3 cell array {[1]} {0×0 double} {0×0 double} idx = 1×3 logical array 1 0 0 idxs = 1 0 0 str = 'two' idc = 1×3 cell array {0×0 double} {0×0 double} {[1]} idx = 1×3 logical array 0 0 1 idxs = 1 0 0 0 0 1 str = 'three' idc = 1×3 cell array {0×0 double} {[1]} {0×0 double} idx = 1×3 logical array 0 1 0 idxs = 1 0 0 0 0 1 0 1 0 str_index_list = 1×1 cell array {1×9 double}

3   Pass
sl = {'this little piggy went to market', ... 'and this little piggy stayed home', ... 'this little piggy had roast beef', ... 'and this one studied computer science at piggy university'}; wtc = { ... 'and','at','beef','computer','had','home', ... 'little','market','one','piggy','roast','science', ... 'stayed','studied','this','to','university','went'}; silc = { ... [15 7 10 18 16 8], ... [1 15 7 10 13 6], ... [15 7 10 5 11 3], ... [ 1 15 9 14 4 12 2 10 17]}; [wt,sil] = word_index(sl); assert(isequal(wt,wtc))

lstr = 1×1 cell array {'this little piggy went to market'} sp = 1×6 cell array {'this'} {'little'} {'piggy'} {'went'} {'to'} {'market'} word_table = 1×6 cell array {'this'} {'little'} {'piggy'} {'went'} {'to'} {'market'} lstr = 1×1 cell array {'this little piggy had roast beef'} sp = 1×6 cell array {'this'} {'little'} {'piggy'} {'had'} {'roast'} {'beef'} word_table = 1×12 cell array {'this'} {'little'} {'piggy'} {'went'} {'to'} {'market'} {'this'} {'little'} {'piggy'} {'had'} {'roast'} {'beef'} lstr = 1×1 cell array {'and this little piggy stayed home'} sp = 1×6 cell array {'and'} {'this'} {'little'} {'piggy'} {'stayed'} {'home'} word_table = 1×18 cell array Columns 1 through 14 {'this'} {'little'} {'piggy'} {'went'} {'to'} {'market'} {'this'} {'little'} {'piggy'} {'had'} {'roast'} {'beef'} {'and'} {'this'} Columns 15 through 18 {'little'} {'piggy'} {'stayed'} {'home'} lstr = 1×1 cell array {'and this one studied computer science at piggy university'} sp = 1×9 cell array {'and'} {'this'} {'one'} {'studied'} {'computer'} {'science'} {'at'} {'piggy'} {'university'} word_table = 1×27 cell array Columns 1 through 14 {'this'} {'little'} {'piggy'} {'went'} {'to'} {'market'} {'this'} {'little'} {'piggy'} {'had'} {'roast'} {'beef'} {'and'} {'this'} Columns 15 through 27 {'little'} {'piggy'} {'stayed'} {'home'} {'and'} {'this'} {'one'} {'studied'} {'computer'} {'science'} {'at'} {'piggy'} {'university'} word_table = 1×18 cell array Columns 1 through 14 {'and'} {'at'} {'beef'} {'computer'} {'had'} {'home'} {'little'} {'market'} {'one'} {'piggy'} {'roast'} {'science'} {'stayed'} {'studied'} Columns 15 through 18 {'this'} {'to'} {'university'} {'went'} lstr = 1×1 cell array {'this little piggy went to market'} sp = 1×6 cell array {'this'} {'little'} {'piggy'} {'went'} {'to'} {'market'} idxs = [] str = 'this' idc = 1×18 cell array Columns 1 through 11 {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} Columns 12 through 18 {0×0 double} {0×0 double} {0×0 double} {[1]} {0×0 double} {0×0 double} {0×0 double} idx = 1×18 logical array 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 idxs = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 str = 'little' idc = 1×18 cell array Columns 1 through 11 {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[1]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} Columns 12 through 18 {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} idx = 1×18 logical array 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 idxs = Columns 1 through 30 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 Columns 31 through 36 0 0 0 0 0 0 str = 'piggy' idc = 1×18 cell array Columns 1 through 11 {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[1]} {0×0 double} Columns 12 through 18 {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} idx = 1×18 logical array 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 idxs = Columns 1 through 30 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 Columns 31 through 54 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 str = 'went' idc = 1×18 cell array Columns 1 through 11 {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} Columns 12 through 18 {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[1]} idx = 1×18 logical array 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 idxs = Columns 1 through 30 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 Columns 31 through 60 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 61 through 72 0 0 0 0 0 0 0 0 0 0 0 1 str = 'to' idc = 1×18 cell array Columns 1 through 11 {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} Columns 12 through 18 {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[1]} {0×0 double} {0×0 double} idx = 1×18 logical array 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 idxs = Columns 1 through 30 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 Columns 31 through 60 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 61 through 90 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 str = 'market' idc = 1×18 cell array Columns 1 through 11 {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[1]} {0×0 double} {0×0 double} {0×0 double} Columns 12 through 18 {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} idx = 1×18 logical array 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 idxs = Columns 1 through 30 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 Columns 31 through 60 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 61 through 90 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 Columns 91 through 108 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 str_index_list = 1×1 cell array {1×108 double} lstr = 1×1 cell array {'this little piggy had roast beef'} sp = 1×6 cell array {'this'} {'little'} {'piggy'} {'had'} {'roast'} {'beef'} idxs = [] str = 'this' idc = 1×18 cell array Columns 1 through 11 {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} Columns 12 through 18 {0×0 double} {0×0 double} {0×0 double} {[1]} {0×0 double} {0×0 double} {0×0 double} idx = 1×18 logical array 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 idxs = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 str = 'little' idc = 1×18 cell array Columns 1 through 11 {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[1]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} Columns 12 through 18 {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} idx = 1×18 logical array 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 idxs = Columns 1 through 30 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 Columns 31 through 36 0 0 0 0 0 0 str = 'piggy' idc = 1×18 cell array Columns 1 through 11 {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[1]} {0×0 double} Columns 12 through 18 {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} idx = 1×18 logical array 0 0 0 0 0 0 0 0 0 1 0 0 0 ...

Suggested Problems

More from this Author95

Community Treasure Hunt

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

Start Hunting!