-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist_exercise.php
More file actions
188 lines (159 loc) · 3.86 KB
/
list_exercise.php
File metadata and controls
188 lines (159 loc) · 3.86 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
<?php
// Given two sorted lists of numbers:
$a1 =
array(
871,
899,
924,
939,
946,
955,
959,
975,
978,
1013,
1014,
1016,
1035,
1040,
1042,
1066,
1066,
1087,
1100,
1135,
1141,
1154,
1170,
1189,
1199,
1216,
1272,
1307,
1327,
1377,
);
$a2 =
array(
888,
898,
922,
923,
936,
954,
986,
987,
996,
1031,
1060,
1066,
1108,
1137,
1180,
1223,
1226,
1270,
1285,
1314,
1316,
1322);
// Calculates the highest value in an array containing only positive numbers
function my_max(array $a) {
$rv = 0;
for($i = 0; $i < count($a); $i++) { // You might want to use while() or foreach() here. Both are equally valid.
if($rv < $a[$i]) {
$rv = $a[$i];
}
}
return $rv;
}
// Calculates the reverse of an array
function my_reverse(array $a) {
$rv = array();
$i = count($a);
while($i > 0) { // A for-loop might be slightly less clear, but still valid.
$i--;
$rv[] = $a[$i]; // Note that the $rv[] = ... construction adds an element to the array.
// Also note that adding an element to an empty array turns it into an array containing one element.
}
return $rv;
}
// Note that neither function uses the php-library. For this exercise it is not allowed to use it.
// However, it is allowed to use it for displaying results, in this case with echo and print_r:
echo my_max($a1);
print_r(my_reverse($a2));
// Finds the smallest value that is present in two *sorted* arrays
function find_first_match(array $a, array $b) {
$i = 0;
$j = 0;
while($i < count($a) && $j < count($b) && $a[$i] != $b[$j]) { // Note that the check whether $i and $j are in range...
// ...has to come before whe use $a[$i] or $b[$j].
// Also note that when we check something in the condition of a while-statement, it is true in the whole body,
// unless the checked values are changed.
if($a[$i] < $b[$j]) {
$i++; // The sought value has to be bigger than $a[$i], and thus come after $a[$i]
} else { // $a[$i] > $b[$j] since where are in a while loop that demands $a[$i] != $b[$j]
$j++; // The sought value has to be bigger than $b[$j].
}
}
if ($i < count($a) && $j < count($b)) { // This means that the condition on which the while-loop ended was $a[$i] != $b[$j]..
// ..That means that $a[$i] == $b[$i] and we have found our equal value. We don't check whether $a[$i] == $b[$j] because..
// ..if $i >= count($a) or $j >= count($b) we would try to address values that are not part of the array. Resulting in..
// ..unpredictable behaviour.
return $a[$i];
} else { // This means that $i >= count($a) or $j >= count($b), so we reached the end of an array without finding a match.
throw new Exception('Value not found.');
}
}
echo find_first_match($a1, $a2);
// Task: Design a function that takes two array-parameters containing sorted lists and merges them into one array,
// in a way that results into a sorted array containing all elements of the original two lists.
function my_merge(array $a, array $b)
// Solution while destroying the argument arrays.
{
$rv = array();
// Sum the number of elements in both arrays.
while(!empty($b) and !empty($a))
{
if($a[0] <= $b[0]) {
$rv[] = array_shift($a);
} else {
$rv[] = array_shift($b);
}
}
while(!empty($b))
{
$rv[] = array_shift($b);
}
while(!empty($a))
{
$rv[] = array_shift($a);
}
return $rv;
}
function my_merge_ix(array $a, array $b)
// Solution with indexed arrays instead of just taking the first elements.
{
$rv = array();
// Sum the number of elements in both arrays.
$i = 0; $j = 0;
$c = count($a); $d = count($b);
while($i < $c and $j < $d)
{
if($a[$i] <= $b[$j]) {
$rv[] = $a[$i++];
} else {
$rv[] = $b[$j++];
}
}
while($j < $d)
{
$rv[] = $b[$j++];
}
while($i < $c)
{
$rv[] = $a[$i++];
}
return $rv;
}
print_r(my_merge($a1, $a2));