-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheck.php
More file actions
291 lines (290 loc) · 7.48 KB
/
Check.php
File metadata and controls
291 lines (290 loc) · 7.48 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
/**
*
* @Name : TDesign-Math/Check.php
* @Version : 1.0
* @Programmer : Max
* @Date : 2019-05-25
* @Released under : https://github.com/BaseMax/TDesign-Math/blob/master/LICENSE
* @Repository : https://github.com/BaseMax/TDesign-Math
*
**/
for ($v = 7;$v <= 100 / 10;$v++) {
$b = $v;
$x = [];
for ($i = 1;$i <= $v;$i++) {
$x[] = $i;
}
for ($k = 3;$k <= ((floor($v - 1) / 2) + 1);$k++) {
$r = $k;
solve($x, $v, $b, $k, $r);
}
}
// function getAllCombos($arr) {
// $combinations = array();
// $words = sizeof($arr);
// $combos = 1;
// for ($i = $words;$i > 0;$i--) {
// $combos*= $i;
// }
// $res = [];
// while (sizeof($combinations) < $combos) {
// shuffle($arr);
// // print_r($arr);
// // $combo = implode(" ", $arr);
// if (!in_array($arr, $combinations)) {
// $combinations[] = $arr;
// sort($arr);
// if (!in_array($arr, $combinations)) {
// print_r($arr);
// $res[] = $arr;
// }
// }
// }
// return $res;
// }
function getAllCombos($arr, $words) {
$combinations = array();
// $words = sizeof($arr);
$combos = 1;
for ($i = $words;$i > 0;$i--) {
$combos*= $i;
}
$res = [];
while (sizeof($combinations) < $combos) {
shuffle($arr);
// print_r($arr);
// $combo = implode(" ", $arr);
if (!in_array($arr, $combinations)) {
$combinations[] = $arr;
sort($arr);
if (!in_array($arr, $combinations)) {
print_r($arr);
$res[] = $arr;
}
}
}
return $res;
}
function check($chars, $size, $combinations = array()) {
if(empty($combinations)) {
$combinations = $chars;
}
if($size == 1) {
return $combinations;
}
$new_combinations = array();
foreach ($combinations as $combination) {
foreach ($chars as $char) {
// print $combination;
// print "\n";
// print $char;
// print "\n===\n";
// $new_combinations[] = [$combination, $char];
// $new_combinations[] = $combination . " " . $char;
$new_combinations[] = $combination . $char;
}
}
return check($chars, $size - 1, $new_combinations);
}
// $chars = array('a', 'b', 'c');
// $output = check($chars, 2);
// print_r($output);
function sortString($string) {
$stringParts = str_split($string);
sort($stringParts);
return implode('', $stringParts);
}
function filter($list, $rules) {
foreach ($list as $index => $item) {
if (substr_count($item, "0") > 0) {
unset($list[$index]);
continue;
}
foreach ($rules as $rule) {
if (substr_count($item, $rule) > 1) {
unset($list[$index]);
break;
}
}
}
return $list;
}
function solve($x, $v, $b, $k, $r) {
// $combinatorics = new Math_Combinatorics;
// global $combinatorics;
// print_r($x);
print "V = " . $v . "\n";
print "K = " . $k . "\n";
// $output = $combinatorics->combinations($x, $v);
$output = check($x, $k);
$output_size = count($output);
for ($i = 0;$i < $output_size;$i++) {
$output[$i] = sortString($output[$i]);
// if(is_array($output[$i])) { }
// sort($output[$i]);
}
asort($output);
$output = array_unique($output);
$output = filter($output, $x);
$output = array_values($output);
print_r($output);
print "---------------------------\n";
}
/**
* Math_Combinatorics
* @link http://pyrus.sourceforge.net/Math_Combinatorics.html
*/
class Math_Combinatorics {
/**
* List of pointers that record the current combination.
*
* @var array
* @access private
*/
private $_pointers = array();
/**
* Find all combinations given a set and a subset size.
*
* @access public
* @param array $set Parent set
* @param int $subset_size Subset size
* @return array An array of combinations
*/
public function combinations(array $set, $subset_size = null) {
$set_size = count($set);
if (is_null($subset_size)) {
$subset_size = $set_size;
}
if ($subset_size >= $set_size) {
return array($set);
} else if ($subset_size == 1) {
return array_chunk($set, 1);
} else if ($subset_size == 0) {
return array();
}
$combinations = array();
$set_keys = array_keys($set);
$this->_pointers = array_slice(array_keys($set_keys), 0, $subset_size);
$combinations[] = $this->_getCombination($set);
while ($this->_advancePointers($subset_size - 1, $set_size - 1)) {
$combinations[] = $this->_getCombination($set);
}
return $combinations;
}
/**
* Recursive function used to advance the list of 'pointers' that record the
* current combination.
*
* @access private
* @param int $pointer_number The ID of the pointer that is being advanced
* @param int $limit Pointer limit
* @return bool True if a pointer was advanced, false otherwise
*/
private function _advancePointers($pointer_number, $limit) {
if ($pointer_number < 0) {
return false;
}
if ($this->_pointers[$pointer_number] < $limit) {
$this->_pointers[$pointer_number]++;
return true;
} else {
if ($this->_advancePointers($pointer_number - 1, $limit - 1)) {
$this->_pointers[$pointer_number] = $this->_pointers[$pointer_number - 1] + 1;
return true;
} else {
return false;
}
}
}
/**
* Get the current combination.
*
* @access private
* @param array $set The parent set
* @return array The current combination
*/
private function _getCombination($set) {
$set_keys = array_keys($set);
$combination = array();
foreach ($this->_pointers as $pointer) {
$combination[$set_keys[$pointer]] = $set[$set_keys[$pointer]];
}
return $combination;
}
/**
* Find all permutations given a set and a subset size.
*
* @access public
* @param array $set Parent set
* @param int $subset_size Subset size
* @return array An array of permutations
*/
public function permutations(array $set, $subset_size = null) {
$combinations = $this->combinations($set, $subset_size);
$permutations = array();
foreach ($combinations as $combination) {
$permutations = array_merge($permutations, $this->_findPermutations($combination));
}
return $permutations;
}
/**
* Recursive function to find the permutations of the current combination.
*
* @access private
* @param array $set Current combination set
* @return array Permutations of the current combination
*/
private function _findPermutations($set) {
if (count($set) <= 1) {
return array($set);
}
$permutations = array();
list($key, $val) = $this->array_shift_assoc($set);
$sub_permutations = $this->_findPermutations($set);
foreach ($sub_permutations as $permutation) {
$permutations[] = array_merge(array($key => $val), $permutation);
}
$set[$key] = $val;
$start_key = $key;
$key = $this->_firstKey($set);
while ($key != $start_key) {
list($key, $val) = $this->array_shift_assoc($set);
$sub_permutations = $this->_findPermutations($set);
foreach ($sub_permutations as $permutation) {
$permutations[] = array_merge(array($key => $val), $permutation);
}
$set[$key] = $val;
$key = $this->_firstKey($set);
}
return $permutations;
}
/**
* Associative version of array_shift()
*
* @access public
* @param array $array Reference to the array to shift
* @return array Array with 1st element as the shifted key and the 2nd
* element as the shifted value
*/
public function array_shift_assoc(array & $array) {
foreach ($array as $key => $val) {
unset($array[$key]);
break;
}
return array($key, $val);
}
/**
* Get the first key of an associative array
*
* @param array $array Array to find the first key
* @access private
* @return mixed The first key of the given array
*/
private function _firstKey($array) {
foreach ($array as $key => $val) {
break;
}
return $key;
}
}