CS1109 Lab 11

Put the work for this lab into a new folder public_html\cs1109\lab11.

In your solutions to Parts 1-4, do not use any built-in functions other than those explicitly mentioned below.

Part 1: Something easy

In Part 1 of Lab 10, you wrote a program to compute the sample mean and sample standard deviation. A limitation was that the form for data entry required the user to enter exactly five numbers. Today we lift that restriction.

Take a copy of this new version of statistics.html; do not edit it. It has just one textfield into which the user enters as many numbers as s/he wishes, separated by spaces, e.g. 3 6 2 121. You can assume that s/he only enters digits and spaces, and no other characters.

You must write a new version of statistics.php, which again outputs the sample mean and sample standard deviation of the numbers that the user has entered. Most of it can be obtained by copy-and-paste from last week's solution.

You will need the trim and explode built-in functions from the lecture. You will need sqrt. And you may use count and even array_sum if you judge that they improve the program.

Part 2: Are you being negative?

Take a copy of numbers.html; do not edit it. The user enters integers separated by spaces, e.g. 3 -7 -1. You can assume that s/he only enters digits, minus signs and spaces, and no other characters.

You must write numbers.php, which outputs 'All positive' if all the user's numbers are positive numbers (greater than 0), or 'All negative' if all the user's numbers are negative numbers (less than 0), or 'Mixed' otherwise.

You will need trim and explode but should use no other built-in functions.

Part 3: igPay atinLay

According to urban legend, schoolkids who want to communicate without being understand by eavesdropping adults do so by speaking in Pig Latin. In the simplified version that we will use in this exercise, to convert a word into Pig Latin you move its initial letter to the end of the word and add "ay". For example, "world wide web" becomes "orldway ideway ebway".

Take a copy of english.html; do not edit it. The user enters words separated by spaces.

Write latin.php, which takes the sentence that the user enters into the text field and outputs its Pig Latin equivalent.

You should use no built-in functions other than trim, explode, substr and (optionally) strlen. You haven't encountered substr before. It's your job to work out how to use it from its entries in the PHP manual (although you can, of course, ask for help): substr.

Part 4: A Huffman Code

Take a copy of huffman.html; do not edit it. The user enters words separated by spaces. However, the user only uses the letters 'e', 'g', 'h', 'o', 'p', 'r', and 's' and spaces. Don't worry. Even with just these letters the user can make many sentences, including she goes, he gropes sheep and herpes progresses. The only reason for confining ourselves to a handful of letters is to reduce the amount of typing later.

Write huffman.php, which takes the sentence that the user enters into the text field and outputs a coded version, based on the following correspondences:

e0110
g10
h0101
o11
p0100
r0111
s000
(space)001

So, for example, if the user enters go here, you program outputs 10110010101011001110110. In other words, 10 for 'g', then 11 for 'o', then 001 for space, and so on.

Assume that the user only enters the allowed characters (e, g, h, o, p, r, s and space). Your script does not need to handle the situation where the user enters some other character.

Hint: associative array

You should use no built-in functions other than trim and str_split. str_split is like explode except that each string in the resulting array is one character in length. For example, str_split("gross spheres") returns the following array: array( "g", "r", "o", "s", "s", " ", "s", "p", "h", "e", "r", "e", "s" ).

Deadline for Lab 11: 1pm, Tuesday 17th January.

If you have named your files and folders correctly, your work will be collected automatically at that time by my software.

Challenge exercise

Remember that challenge exercises are always optional. They do not form part of your year's work and they are not worth any marks. They are designed for those students who finish the main exercise quickly and easily, and wish to explore further.

Warning! Writing an efficient solution is exceptionally challenging! (I suspect no one will manage it. But I'd be delighted to be proved wrong!)

Take a copy of this form: huffman_challenge.html. The user enters a string of 0s and 1s.

Write huffman_challenge.php, which converts the user's 0s and 1s back into English using the same correspondences as above. For example, if the user enters 01001111011100101011110000, your program outputs poor hogs (because 0100 is "p", 11 is "o" and so on).

Note how you are not given any indication of where the code for one letter ends and the code for the next letter begins. However, this example Huffman code is what is known as a prefix code, and this means that it is possible for your program to overcome this difficulty!

Use whatever built-in functions you wish.

Leave both files in your lab11 folder, and I will take a look at them.