Это не домашнее задание: Наткнулся этого сценария во время работы на PHP String Differences and Dynamic RestrictionsВозможная группировка слов
Учитывая строку n
слов, как распределить их в m
групп, не изменяя последовательности слов?
Example 1:
String: "My name is SparKot"
Groups: 2 (string is split in to two strings)
Possible groups will be:
('My', 'name is SparKot'),
('My name', 'is SparKot'),
('My name is', 'SparKot')
с одной и той же строки
Example 2:
String: "My name is SparKot"
Groups: 3 (string will be split in to three strings)
Possible groups will be:
('My', 'name', 'is SparKot'),
('My', 'name is', 'SparKot'),
('My name', 'is', 'SparKot')
Мой PHP функция() без направления (это, предполагают, чтобы вернуть Многомерные групп):
function get_possible_groups ($orgWords, $groupCount, &$status) {
$words = explode (' ', $orgWords);
$wordCount = count($words);
if ($wordCount < $groupCount) {
$status = -1;
return;
} else if ($wordCount === $groupCount) {
$status = 0;
return (array_chunk($words, 1));
}
for ($idx =0; $idx < $wordCount; $idx) {
for ($jdx =0; $jdx < $groupCount; $jdx++) {
}
}
// append all arrays to form multidimension array
// return groupings[][] array
}
$status =0;
$groupings = get_possible_groups('My name is SparKot', 4, $status);
var_dump($groupings);
для выше ПРИМЕР- 2 должна быть возвращена:
$groupings = array (
array ('My', 'name', 'is SparKot'),
array ('My', 'name is', 'SparKot'),
array ('My name', 'is', 'SparKot'));
Любые подсказки для решения этой проблемы будут высоко оценены.
Прогресс:
- случай: когда
wordCount = groupCount
[решено]
Вы хотите, чтобы все возможные значения возвращались? –
Вы вообще что-то пробовали ??? – Shef
В примере 1 не должно быть трех групп не три? –