-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtele.php
More file actions
62 lines (56 loc) · 1.25 KB
/
tele.php
File metadata and controls
62 lines (56 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php if (!(php_sapi_name() == "cli")) : ?>
<!DOCTYPE html>
<html>
<head>
<title>Telelearn assignment</title>
</head>
<body>
<form name="f" method="GET" action="tele.php">
Type string to permute:
<input type="text" name="myStr">
<button type="submit" accesskey="s"><u>S</u>ubmit</button>
</form>
<?php endif; ?>
<?php
function swap(&$datum,$i,$j) {
$temp = $datum[$i];
$datum[$i] = $datum[$j];
$datum[$j] = $temp;
}
function solve($datum,$i,$n) {
if ($i == $n) {
global $arr;
if (!in_array($datum, $arr)) {
array_push($arr, $datum);
}
}
else {
for ($j = $i; $j < $n; $j++) {
swap($datum,$i,$j);
solve($datum, $i+1, $n);
swap($datum,$i,$j);
}
}
}
$arr = [];
if (php_sapi_name() == "cli") {
if (isset($argv[1])) {
$datum = $argv[1];
solve($datum, 0,strlen($datum));
echo 'Permutations: '. count($arr)."\n";
print_r($arr);
}
}
else {
$datum = $_REQUEST['myStr'];
solve($datum, 0,strlen($datum));
echo 'Permutations: '. count($arr);
echo '<pre>';
print_r($arr);
echo '</pre>';
}
?>
<?php if (!(php_sapi_name() == "cli")) : ?>
</body>
</html>
<?php endif; ?>