-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsame.php
More file actions
67 lines (61 loc) · 1.96 KB
/
same.php
File metadata and controls
67 lines (61 loc) · 1.96 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
<?php
// 6 kyu - Same Array?
// Given two arrays, the purpose of this Kata is to check if these two arrays are the same. "The same" in this Kata means the two arrays contains arrays of 2 numbers which are same and not necessarily sorted the same way. i.e. [[2,5], [3,6]] is same as [[5,2], [3,6]] or [[6,3], [5,2]] or [[6,3], [2,5]] etc
//
// [[2,5], [3,6]] is NOT the same as [[2,3], [5,6]]
// Two empty arrays [] are the same
// [[2,5], [5,2]] is the same as [[2,5], [2,5]] but NOT the same as [[2,5]]
// [[2,5], [3,5], [6,2]] is the same as [[2,6], [5,3], [2,5]] or [[3,5], [6,2], [5,2]], etc
// An array can be empty or contain a minimun of one array of 2 integers and up to 100 array of 2 integers
// Note:
//
// [[]] is not applicable because if the array of array are to contain anything, there have to be two numbers.
// 100 randomly generated tests that can contains either "same" or "not same" arrays.
function same($aArr, $bArr) {
if (empty($aArr) && empty($bArr)) return true;
$diff = array_diff(array_merge(... $aArr), array_merge(... $bArr));
return count($diff) === 0 ? true : false;
}
// Alternative solutions:
// function same($aArr, $bArr) {
//
// $aArr= array_reduce($aArr, 'array_merge', []);
// $bArr= array_reduce($bArr, 'array_merge', []);
//
// sort($aArr);
// sort($bArr);
//
// return $aArr == $bArr;
// }
// function same($aArr, $bArr) {
// if (empty($aArr) && empty($bArr)) return true;
//
// $sumA = array_map(function(&$x) {
// $x = $x[0] + $x[1];
// return $x;
// }, $aArr);
//
// $sumB = array_map(function(&$x) {
// $x = $x[0] + $x[1];
// return $x;
// }, $bArr);
//
// sort($sumA);
// sort($sumB);
//
// return $sumA === $sumB;
// }
// function same($aArr, $bArr): bool {
// foreach($aArr as $a) {
// $contained = false;
// foreach($bArr as $b) {
// if(!array_diff($a, $b)) {
// $contained = true;
// }
// }
// if(!$contained) return false;
// }
//
// return true;
// }
?>