我需要将用户的所有课程输入组合起来,例如用户添加5个课程,它将作为此模板: 课程1 课程2 课程3 课程4 课程5
之后,我需要为所有课程创建一个组合级别:
generateVideoUrl(videoId) {
return this.sanitizer.bypassSecurityTrustResourceUrl("https://www.youtube.com/embed/" + videoId + "?rel=0&showinfo=0");
}
实际上,我在这个主题中找到了我需要的东西:Create every possible combination in PHP但是有些东西我不明白我该怎么做。
直到现在这段代码编辑后:
array('A+', 'A', 'B+', 'B', 'C+', 'C','D+','D','F')
输出
$dict = [
'1' => ['A+', 'A', 'B+', 'B', 'C+', 'C','D+','D','F'],
'2' => ['Course 1', 'Course 2', 'Course 3', 'Course 4', 'Course 5'],
];
$str = '2 : 1';
class SentenceTemplate implements IteratorAggregate
{
private $template;
private $thesaurus;
public function __construct($str, $dict)
{
$this->thesaurus = [];
$this->template = preg_replace_callback('/\w+/', function($matches) use ($dict) {
$word = $matches[0];
if (isset($dict[$word])) {
$this->thesaurus[] = $dict[$word];
return '%s';
} else {
return $word;
}
}, $str);
}
public function getIterator()
{
return new ArrayIterator(array_map(function($args) {
return vsprintf($this->template, $args);
}, $this->combinations($this->thesaurus)));
}
private function combinations($arrays, $i = 0) {
if (!isset($arrays[$i])) {
return array();
}
if ($i == count($arrays) - 1) {
return $arrays[$i];
}
// get combinations from subsequent arrays
$tmp = $this->combinations($arrays, $i + 1);
$result = array();
// concat each array from tmp with each element from $arrays[$i]
foreach ($arrays[$i] as $v) {
foreach ($tmp as $t) {
$result[] = is_array($t) ? array_merge(array($v), $t) : array($v, $t);
}
}
return $result;
}
}
$sentences = new SentenceTemplate(1, $dict);
$i = 1;
foreach ($sentences as $sentence) {
echo "COMBINATION $i : \n";
$sentences2 = new SentenceTemplate(2, $dict);
foreach ($sentences2 as $sentence2) {
echo "$sentence2 : "."$sentence\n";
}
echo "----------\n";
$i++;
}
现在我如何构建更多组合,例如:
COMBINATION 1 :
Course 1 : A+
Course 2 : A+
Course 3 : A+
Course 4 : A+
Course 5 : A+
----------
COMBINATION 2 :
Course 1 : A
Course 2 : A
Course 3 : A
Course 4 : A
Course 5 : A
----------
COMBINATION 3 :
Course 1 : B+
Course 2 : B+
Course 3 : B+
Course 4 : B+
Course 5 : B+
----------
COMBINATION 4 :
Course 1 : B
Course 2 : B
Course 3 : B
Course 4 : B
Course 5 : B
----------
COMBINATION 5 :
Course 1 : C+
Course 2 : C+
Course 3 : C+
Course 4 : C+
Course 5 : C+
----------
COMBINATION 6 :
Course 1 : C
Course 2 : C
Course 3 : C
Course 4 : C
Course 5 : C
----------
COMBINATION 7 :
Course 1 : D+
Course 2 : D+
Course 3 : D+
Course 4 : D+
Course 5 : D+
----------
COMBINATION 8 :
Course 1 : D
Course 2 : D
Course 3 : D
Course 4 : D
Course 5 : D
----------
COMBINATION 9 :
Course 1 : F
Course 2 : F
Course 3 : F
Course 4 : F
Course 5 : F
----------
该程序是针对每门课程与其他课程有不同的成绩,其他课程有相同的程序,有人有想法建立这个吗?
答案 0 :(得分:0)
此代码可能有所帮助。它是一个PHP程序,用于打印长度为k的所有可能的字符串。
<?php
// PHP program to print all possible strings of length k
// Driver method to test below methods
function main() {
$set = ['A+', 'A', 'B+', 'B', 'C+', 'C','D+','D','F'];
$k = 5;
$n = sizeof($set);
printAllKLengthRec($set, "", $n, $k);
}
// The main recursive method to print all possible strings of length k
function printAllKLengthRec($set, $prefix, $n, $k) {
// Base case: k is 0, print prefix
if ($k == 0) {
echo $prefix."\n";
return;
}
// One by one add all characters from set and recursively
// call for k equals to k-1
for ($i = 0; $i < $n; ++$i) {
// Next character of input added
$newPrefix = $prefix . $set[$i];
// k is decreased, because we have added a new character
printAllKLengthRec($set, $newPrefix, $n, $k - 1);
}
}
main();
?>
您只需要修改打印部分。 下面的代码可能会实现您的目的。
<?php
// PHP program to print all possible strings of length k
// Driver method to test below methods
function main() {
$set = ['A+', 'A', 'B+', 'B', 'C+', 'C','D+','D','F'];
$k = 5;
$n = sizeof($set);
printAllKLengthRec($set, "", $n, $k);
}
// The main recursive method to print all possible strings of length k
function printAllKLengthRec($set, $prefix, $n, $k) {
// Base case: k is 0, print prefix
if ($k == 0) {
$c = 1;
for($p=0; $p<strlen($prefix); $p++) {
echo "Course $c: ". $prefix[$p];
if(($p+1) < strlen($prefix) && ($prefix[$p+1] == '+' || $prefix[$p+1] == '-')) {
echo $prefix[$p+1];
$p++;
}
echo "\n";
$c++;
}
return;
}
// One by one add all characters from set and recursively
// call for k equals to k-1
for ($i = 0; $i < $n; ++$i) {
// Next character of input added
$newPrefix = $prefix . $set[$i];
// k is decreased, because we have added a new character
printAllKLengthRec($set, $newPrefix, $n, $k - 1);
}
}
main();
?>