Możliwe grupowanie słów
To nie jest praca domowa: przyszedł do tego scenariusza podczas pracyRóżnice w łańcuchach PHP i dynamiczne ograniczenia
Podano ciągn
słowa, jak je rozpowszechniaćm
grupy bez zmiany kolejności słów?
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')
tym samym łańcuchem
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')
Moja funkcja PHP () bez kierunku (przypuszcza się, że zwraca wiele wymiarów grup):
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);
dla powyższego przykładu funkcja 2 ma powrócić:
$groupings = array (
array ('My', 'name', 'is SparKot'),
array ('My', 'name is', 'SparKot'),
array ('My name', 'is', 'SparKot'));
Wszelkie wskazówki, jak podejść do tego problemu, zostaną bardzo docenione.
Postęp:
sprawa: kiedywordCount = groupCount
[rozwiązany]