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: Are you a little bit odd?

Take a copy of numbers.html; do not edit it. The user enters integers separated by spaces, e.g. 3 243 0 -4 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 'Empty' if the array is empty, or 'All even' if all the user's numbers are even numbers (i.e. divisible by 2), or 'All odd' if all the user's numbers are odd numbers (i.e. not divisible by 2), or 'Mixed' otherwise.

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

Part 2: Yes, we have no bananas

It's time to do something with associative arrays — something simple first.

Take a copy of fruit.html; do not edit it. The user chooses a fruit using the radio buttons and enters a quantity into the textfield, e.g. 10kg of Apples.

You must write fruit.php, which:

  1. creates the following associative array:
    $fruit_prices = array('Apples' => 1.59, 'Pears' => 2.34,
        'Kumquats' => 4.05, 'Jujubes' => 2.34);
  2. takes in the user's input;
  3. outputs the cost of the user's selection, e.g. 10kg of Apples costs 10 × 1.59, which is 15.9.

Note: This does not require a foreach-loop.

Part 3: Oh no, not this again

In Part 2 of lab 10, you had to write a program that converted number words to an integer, e.g. from two four one to the integer 241. Today you're going to rewrite this program, making it shorter by using an associative array

Copy your own solution to Part 2 of the lab 10 exercise from your lab10 folder into your lab11 folder. Remove bugs if you have any.

You should now have a form called words_to_int.html and a PHP script called words_to_int.php in your lab11 folder.

Now edit words_to_int.php: by using an associative array, make it shorter. Note again: a second foreach-loop is not needed.

Part 4: We're all on acid

A DNA molecule is a long sequence of nucleotides: A = adenine, G = guanine, C = cytosine and T = thymine. Codons (triplets of nucleotides) encode amino acids; for example, a sequence of three thymine nucelotides (TTT) specifies the amino acid known as phenylalanine (abbreviated Phe).

Here's an associative array. The keys are codons (triplets of nucleotides); the values are the corresponding amino acids.

$genetic_code = array(
      'TTT' => 'Phe',  'TCT' => 'Ser',  'TAT' => 'Tyr',  'TGT' => 'Cys',
      'TTC' => 'Phe',  'TCC' => 'Ser',  'TAC' => 'Tyr',  'TGC' => 'Cys',
      'TTA' => 'Leu',  'TCA' => 'Ser',  'TAA' => 'Stop', 'TGA' => 'Stop',
      'TTG' => 'Leu',  'TCG' => 'Ser',  'TAG' => 'Stop', 'TGG' => 'Trp',
      'CTT' => 'Leu',  'CCT' => 'Pro',  'CAT' => 'His',  'CGT' => 'Arg',
      'CTC' => 'Leu',  'CCC' => 'Pro',  'CAC' => 'His',  'CGC' => 'Arg',
      'CTA' => 'Leu',  'CCA' => 'Pro',  'CAA' => 'Gln',  'CGA' => 'Arg',
      'CTG' => 'Leu',  'CCG' => 'Pro',  'CAG' => 'Gln',  'CGG' => 'Arg',
      'ATT' => 'Ile',  'ACT' => 'Thr',  'AAT' => 'Asn',  'AGT' => 'Ser',
      'ATC' => 'Ile',  'ACC' => 'Thr',  'AAC' => 'Asn',  'AGC' => 'Ser',
      'ATA' => 'Ile',  'ACA' => 'Thr',  'AAA' => 'Lys',  'AGA' => 'Arg',
      'ATG' => 'Met',  'ACG' => 'Thr',  'AAG' => 'Lys',  'AGG' => 'Arg',
      'GTT' => 'Val',  'GCT' => 'Ala',  'GAT' => 'Asp',  'GGT' => 'Gly',
      'GTC' => 'Val',  'GCC' => 'Ala',  'GAC' => 'Asp',  'GGC' => 'Gly',
      'GTA' => 'Val',  'GCA' => 'Ala',  'GAA' => 'Glu',  'GGA' => 'Gly',
      'GTG' => 'Val',  'GCG' => 'Ala',  'GAG' => 'Glu',  'GGG' => 'Gly'

Take a copy of dna_to_acids.html; do not edit it. The user enters a string of nucleotides, e.g. "TTTTCTGAT".

Write dna_to_acids.php, which takes the string that the user enters into the text field and outputs the corresponding sequence of acids. For example, for "TTTTCTGAT", the output is "PheSerAsp".

If the user enters a character other than uppercase A, G, C or T, or if the length of the user's input is not a multiple of three (e.g. "TTTG"), then do not output any amino acids — instead, simply output a message of complaint to the user (just one message, not one per mistake).

Apart from trim and array_key_exists, the only other built-in function you can use is str_split. It's your job to work out how to use it from its entry in the PHP manual (although you can, of course, ask for help).

Note to biologically sophisticated students: In this exercise, we do not take any special account of start and stop codons.

Deadline for Lab 11: 1pm, Tuesday 22nd 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.

Take a copy of from_roman.html; do not edit it. The user enters a number written in Roman numerals.

Write from_roman.php, which outputs the decimal integer that corresponds to the user's input, e.g. if the user enters MXIV, your program outputs 1014.

Try to avoid looking for algorithms on the Web!

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