/
lesson3_homework.php
222 lines (184 loc) · 5.63 KB
/
lesson3_homework.php
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
<p> Using arrays, create a data structure which can store a list of students (ID, name, surname) together with the personal details (address & tel. no.) of
each individual student, including an average mark based on a preassessment test.
</p>
<p> Create a function which shows a list of students showing ID, Name, Surname & average mark. Default sorting by Surname (asc), but should allow for
sorting by ID (asc) and average mark (desc).
</p>
<p> Create a function which can display all the information belonging to a particular student.
</p>
<p> Create a function which resets the average mark for all students to 0.
</p>
<?php
function dump($data){
echo "<pre>";
var_dump($data);
echo "</pre>";
}
$students = array(
array(
"id" => "STUD_123",
"name" => "Alex",
"surname" => "Spiteri",
"address" => "194, Autumn Leaves, Triq il-Kampanella, Fgura, Malta",
"number" => "0035679281426",
"mark" => 80
),
array(
"id" => "STUD_789",
"name" => "John",
"surname" => "Cena",
"address" => "123, London Bulevard, California, USA",
"number" => "684423234434",
"mark" => 50
),
array(
"id" => "STUD_456",
"name" => "Jerry",
"surname" => "Cheng",
"address" => "1223, Flat1 BlockB, China City, China",
"number" => "34434343434",
"mark" => 100
)
);
function getStudents($orderBy = "surname"){
global $students;
$order = strtolower($orderBy);
switch ($order){
case "surname":
usort($students, function($a, $b) {
return strcasecmp($a['surname'], $b['surname']);
});
break;
case "id":
usort($students, function($a, $b) {
return strnatcasecmp($a['id'], $b['id']);
});
break;
case "mark":
usort($students, function($a, $b) {
return $b['mark'] - $a['mark'];
});
break;
default:
throw new Exception("Invalid orderby parameter supplied");
break;
}
foreach($students as $student){
echo "<p>".$student['id'].": ".$student['name']." ".$student['surname']." Mark: ".$student['mark']."</p>";
}
}
function getStudent($id){
global $students;
foreach($students as $student){
if(in_array($id, $student)){
return "<p>ID: ".$student['id']."</p><p>Name: ".$student['name']." ".$student['surname']."</p><p>Address: ".$student['address']."</p><p>Number: ".$student['number']."</p><p> Mark: ".$student['mark']."</p>";
}
}
return "Student with ID: $id not found.";
}
function resetMarks(){
global $students;
foreach($students as &$student){
$student['mark'] = 0;
}
}
getStudents();
$student = getStudent("STUD_123");
dump($student);
resetMarks();
dump($students);
?>
<p> Create an array containing 20 numbers. Using loops, display the
maximum, minimum, average and sum of the numbers.</p>
<p> Create an associative array having ID Card as key and Name as
value. Then iterate through the array and display values.</p>
<?php
$numbers = array();
function minNum($array){
$count = count($array);
if($count == 0){
throw new Exception("Array is empty");
}
else if($count == 1){
return $array[0];
}
else{
$min = $array[0];
foreach($array as $number){
if($number <= $min){
$min = $number;
}
}
return $min;
}
}
function maxNum($array){
$count = count($array);
if($count == 0){
throw new Exception("Array is empty");
}
else if($count == 1){
return $array[0];
}
else{
$min = $array[0];
foreach($array as $number){
if($number >= $min){
$min = $number;
}
}
return $min;
}
}
function sumNum($array){
$sum = 0;
foreach($array as $a){
if(is_numeric($a)){
$sum += $a;
}
}
return $sum;
}
function avgNum($array){
return sumNum($array)/count($array);
}
for($i = 0; $i < 20; $i++){
$numbers[] = rand(1,10000);
}
dump($numbers);
echo "Min: ".minNum($numbers)."<br>";
echo "Max: ".maxNum($numbers)."<br>";
echo "Sum: ".sumNum($numbers)."<br>";
echo "Average: ".avgNum($numbers)."<br>";
$assoc = array("584291M" => "Alex Spiteri", "323423M" => "John Doe", "2323342G" => "Wenzu Mallia");
dump($assoc);
foreach($assoc as $key => $value){
echo "<p>$key: $value</p>";
}
$nums = array();
for($i = 0; $i < 10; $i++){
$nums[] = rand(1,10000);
}
dump($nums);
rsort($nums);
dump($nums);
$count = count($nums);
$nums2 = array();
for($i=0;$i<$count;$i++){
$nums2[] = array_pop($nums);
}
$nums2;
dump($nums2);
function fibonacci($pos) {
static $cache; //declaring $cache as static to persist it throughout all the calls.
if ($pos == 0) {
return 0;
} else if ($pos == 1 || $pos == 2) {
return 1;
} else {
if (!(!is_null($cache) && array_key_exists($pos, $cache))) { //is $cache is not null and the current position is not already cached
$cache[$pos] = fibonacci($pos - 1) + fibonacci($pos - 2);
}
return $cache[$pos];
}
}