-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpermutations.html
124 lines (111 loc) · 3.13 KB
/
permutations.html
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<!DOCTYPE html>
<html>
<head>
<title>Permutation Generator</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
#container {
max-width: 800px;
margin: 20px auto;
padding: 20px;
background-color: #f2f2f2;
border: 1px solid #ccc;
}
h1 {
text-align: center;
}
#input-textarea {
width: 100%;
height: 200px;
resize: none;
}
#length-input {
width: 100%;
margin-top: 10px;
}
#generate-btn {
margin-top: 10px;
}
#loading-indicator {
display: none;
text-align: center;
margin-top: 20px;
}
#result-link {
display: none;
text-align: center;
margin-top: 20px;
}
</style>
</head>
<body>
<div id="container">
<h1>Permutation Generator</h1>
<textarea
id="input-textarea"
placeholder="Enter items, each on a separate line"
></textarea>
<input
type="number"
id="length-input"
placeholder="Permutation Length"
min="1"
/>
<button id="generate-btn" onclick="generatePermutations()">
Generate Permutations
</button>
<div id="loading-indicator">Generating permutations...</div>
<a id="result-link" href="#" download="permutations.txt"
>Download Permutations</a
>
</div>
<script>
function generatePermutations() {
var inputTextarea = document.getElementById("input-textarea");
var items = inputTextarea.value.split("\n").filter(Boolean);
if (items.length === 0) {
alert("Please enter at least one item.");
return;
}
var lengthInput = document.getElementById("length-input");
var length = parseInt(lengthInput.value);
if (isNaN(length) || length <= 0) {
alert("Please enter a valid permutation length.");
return;
}
var loadingIndicator = document.getElementById("loading-indicator");
var resultLink = document.getElementById("result-link");
loadingIndicator.style.display = "block";
resultLink.style.display = "none";
var permutations = getPermutations(items, length);
setTimeout(function () {
loadingIndicator.style.display = "none";
resultLink.style.display = "block";
var textContent = permutations.join("\n");
var blob = new Blob([textContent], { type: "text/plain" });
resultLink.href = URL.createObjectURL(blob);
}, 1000);
}
function getPermutations(items, length) {
var results = [];
function permute(arr, memo = []) {
if (memo.length === length) {
results.push(memo.join(""));
return;
}
for (var i = 0; i < arr.length; i++) {
var curr = arr.slice();
var next = curr.splice(i, 1);
permute(curr.slice(), memo.concat(next));
}
}
permute(items);
return results;
}
</script>
</body>
</html>