Lab 11, Part 2 -------------- This was the exercise in which you had to work out whether the user's numbers were all positive, all negative or mixed. It was marked generously and so most of you scored 4 out of 5. The main error was to fail to do EXACTLY what the question said. The question defined positive as greater than zero (which is the normal definition), and it defined negative as less than zero (again, the normal definition). Zero itself is neither positive nor negative. Therefore, the output should be "mixed" in each of the following examples: 4 0 3 -9 -3 0 -2 0 0 0 0 as well as in this more obvious example: 5 -3 2 2 Don't invent your own version of the question: do exactly what the question says. As for the solutions, many of you counted positves and negatives; others of you used booleans to record the presence of at least one positive and the presence of at least one negative. Probably the most elegant solution uses booleans to record whether they are all positive and whether they are all negative: these booleans start out true and get flipped when they see a counter-example: $all_positive = true; $all_negative = true; foreach ( $numbers as $number ) { if ( $number <= 0 ) { $all_positive = false; } if ( $number >= 0 ) { $all_negative = false; } } echo $all_positive ? '
All positive
' : ( $all_negative ? 'All negative
' : 'Mixed
'); It should have been possible for you to find this solution because we did one that was almost exactly the same in the previous day's Monday lecture: see the program for checking whether all numbers are even or not. Lab 11, Part 4 -------------- This was the Huffman encoding. If you got it to work, you scored 4 or 5 out of 5 (generous again!). Many of you thought it needed a foreach inside a foreach. Well done for writing a program with a foreach insude a foreach - especially since I've not yet showed you an example of such a program in the lectures. But you only scored 4 out of 5 because the solution does NOT require a foreach inside a foreach. Here's the solution that scores 5 out of 5: $map = array('e' => '0110', 'g' => '10', 'h' => '0101', 'o' => '11', 'p' => '0100', 'r' => '0111', 's' => '000', ' ' => '001'); $sentence = trim( $_GET['sentence'] ); $chars = str_split( $sentence ); $code = ''; foreach ($chars as $char) { $code .= $map[$char]; } echo "{$code}
"; Ask us in the labs if you don't understand.