Put the work for this lab into a new folder
public_html\cs1109\lab13
.
Create a new file called array_library.php
, which should
contain just the PHP start and end tags (no HTML at all). Between these tags define
each of the ten functions below. To make the exercise worthwhile, the only
built-in functions I am going to allow you to use are
array
(for creating arrays),
in_array
and
isset
(or
array_key_exists
).
Create another file called array_tester.php
. The first statement of PHP in this file
will use the require_once
command to load in
array_library.php
.
After you add each function to array_library.php
,
add some statements to array_tester.php
to invoke your function
in order to test it.
get_distinct
, which
takes in an indexed array $a
and returns a new
array that contains the contents of $a
but
with no duplicate values. E.g. on
array(2, 3, 1, 2, 1, 4)
, it returns
array(2, 3, 1, 4)
.
get_intersection
,
which takes in two indexed arrays $a
and $b
, each of which you can assume contains no
duplicate values, and returns a new array that contains
just elements that $a
and $b
have in common. E.g. if $a
is
array(2, 5, 1, 7)
and $b
is
array(1, 6, 2, 4)
, it returns
array(2, 1)
.
get_union
,
which takes in the same as get_intersection
but it returns a new array that contains all the elements
in $a
and all the elements of $b
but with no duplicates. E.g. if $a
is
array(2, 5, 1, 7)
and $b
is
array(1, 6, 2, 4)
, it returns
array(2, 5, 1, 7, 6, 4)
.
get_difference
,
which takes in the same as get_intersection
but it returns a new array that contains all the elements
in $a
that are not in $b
.
E.g. if $a
is
array(2, 5, 1, 7)
and $b
is
array(1, 6, 2, 4)
, it returns
array(5, 7)
.
get_symmetric_difference
,
which takes in the same as get_intersection
but it returns a new array that contains all the elements
in $a
that are not in $b
and
all the elements in $b
that are not in
$a
. E.g. if $a
is
array(2, 5, 1, 7)
and $b
is
array(1, 6, 2, 4)
, it returns
array(5, 7, 6, 4)
.
is_subset
,
which takes in the same as get_intersection
but it returns true
if $a
is a subset of $b
and false
otherwise. In other words, it returns
true
if and only if all elements of $a
are in $b
.
E.g. if $a
is
array(2, 5, 1, 7)
and $b
is
array(5, 2, 3, 7)
, it returns
false
; but if $b
is
array(5, 7, 1, 4, 2)
or even array(5, 7, 1, 2)
,
it returns true
.
are_equal
,
which takes in the same as get_intersection
but it returns true
if $a
and $b
contain exactly the same elements, and false
otherwise.
E.g. if $a
is
array(2, 5, 1, 7)
and $b
is
array(5, 2, 1, 7)
, it returns
true
; but if $b
is
array(5, 7, 6, 4)
, it returns false
.
is_proper_subset
,
which takes in the same as get_intersection
but it returns true
if $a
is a proper subset of $b
and false
otherwise. In other words, it returns
true
if and only if all elements of $a
are in
$b
but their contents are
not equal.
E.g. if $a
is
array(2, 5, 1, 7)
and $b
is
array(5, 2, 3, 7)
, it returns
false
; if $b
is
array(5, 7, 1, 4, 2)
, it returns true
; but if $b
contains array(5, 7, 1, 2)
,
it returns false
.
get_flip
that takes in
an associative array $a
and returns a new
associative array whose keys are $a
's values
and vice versa. There is a problem if $a
's
values are not unique, because the new array's keys
must be unique. In this case, the new array contains the
first encountered value and key from $a
.
E.g. if $a
is
array("a" => "x", "c" => "y", "b" => "z", "d" => "y")
,
it returns
array("x" => "a", "y" => "c", "z" => "b")
.
get_frequencies
that takes in
an associative array $a
and returns a new
associative array whose keys are $a
's values
and whose values are the frequency with which the value
occurs in $a
.
E.g. if $a
is
array("a" => "x", "c" => "y", "b" => "z", "d" => "y")
,
it returns
array("x" => 1, "y" => 2, "z" => 1)
.
Deadline: 1pm, Tuesday 5th February.
If you have named your files and folders correctly, your work will be collected automatically at that time by my software.
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 specificity.html
; do not modify it.
It has two text fields. The user enters two CSS selectors.
Write specificity.php
, which compares the two selectors and outputs either
First one, Second one or Neither, depending on which is the more specific selector.
For example, if the user enters p em and h1, then your program outputs First one;
or if the user enters header h1 hgroup and #education p, it outputs
Second one.
Keep things simple by only handling what I called find selectors; ignore what I called filter selectors.
Browsers work this out by scoring each selector. Here is a slide show that contains details of the scoring algorithm (see slides 48-71). You can find other presentations of this material on the Web.
Leave both files in your lab13
folder, and I will take a look at them.