This test is about 4 questions related to php and should take
less than 75 minutes to complete these Question 1 0f 4
Fantastic three Sequence
The fantastic three sequences is a series of numbers in which
each subsequent number is the sum of the previous three, minus one. The first
few numbers in fantastic three sequences are: 0, 1, 1, 1, 2, 3, 5, etc Write a
function that prints the Nth number in the fantastic three sequences to
standard output.
Answer:
<?php
function
fantastic3($n) {
$series =
array(0,1,1,1);
for($i=3 ; $i<=$n
; $i++){
$a =
(isset($series[ (int)$i-3 ]))?$series[ (int)$i-3 ]:0;
$b =
(isset($series[ (int)$i-2 ]))?$series[ (int)$i-2 ]:0;
$c =
(isset($series[ (int)$i-1 ]))?$series[ (int)$i-1 ]:0;
$d =
($a+$b+$c)-1;
if($d<0){
$d = 0;
}
$series[$i] = $d;
}
echo $series[
(int)$n-1 ];
}
// Do NOT call the
fantastic3 function in the code
// you write. The
system will call it automatically.
?>
Question 2 of 4
Counts Days
Write a function named count Days which takes a single parameter
named date in string which is string in the form ”MM.DD.YYY” represent a real date value. The function
should print to the console the number of days from the beginning of the year
specified in date in String until the date represented in date in String. If
the value of date in String is invalid, the function should print “Bad
format” to the console.
Answer:
<?php
function
countDays($dateInString) {
$date = explode(‘.’,
$dateInString);
if(count($date) == 3
&& checkdate($date[0], $date[1], $date[2])){
$formatted_date =
$date[2].’-‘.$date[0].’-‘.$date[1].’ 00:00:00′;
$diff =
strtotime($formatted_date) – strtotime($date[2].’-01-01 00:00:00′);
echo
round($diff/86400)+1;
} else {
echo ‘Bad format';
}
}
// Do NOT call the
countDays function in the code
// you write. The
system will call it automatically.
?>
Question 3 of 4
Change Nickname
The following from
enable user to change their nickname on their website. <form> <input
type=”text” name=”oldNickName” value=” /> <input
type=”text” name=”newNickName” value=” /> <input
type=”submit” name=” name=”submit” /> the users are stored in an
array as follows: $users = array( array(
Answer:
<?php
function
changeNickname($oldNickname, $newNickname, $users){
$valid =
preg_match(‘/^(([^0-9])+([A-Za-z0-9\$\#\<\>\-\_]+))$/i’,
$newNickname, $matches);
if($valid == true){
$old_found = false;
$new_exists = false;
foreach($users as $id
=> $user){
if($user[‘nickname’]
== $oldNickname){
$old_found = true;
}
if($user[‘nickname’]
== $newNickname){
$new_exists = true;
}
}
if($old_found == true
&& $new_exists == false){
echo ‘Your nickname
has been changed from ‘.$oldNickname.’ to ‘.$newNickname;
} else {
echo ‘Failed to
update';
}
} else {
echo ‘Failed to
update';
}
}
// Do NOT call the
changeNickname function in the code
// you write. The
system will call it automatically.
?>
Question 4 of 4
Calculate shipping fees
A script is required
to calculate shipping fees for a store.the store has two types of shipping
local shipping and international shipping. they are calculated according to the
following formulas. Local shipping: number of items * distance * .8 international
shipping : number of items * ( local distance * .8 + international
distance * 1.2)
Answer:
<?php
// Do not modify the
Shipping class.
abstract class
Shipping
{
private $_itemsCount;
private $_distance;
public function
__construct($itemsCount, $distance)
{
$this->_itemsCount
= $itemsCount;
$this->_distance
= $distance;
}
abstract public
function getFees();
public function
getDistance()
{
return $this->_distance;
}
public function
getItemsCount()
{
return $this->_itemsCount;
}
}
// You can modify code
below this comment.
class
InternationalShipping extends Shipping
{
private
$_internationalDistance;
public function
__construct($itemsCount, $distance, $internationalDistance){
parent::__construct($itemsCount,
$distance);
$this->_internationalDistance
= $internationalDistance;
}
public function
getFees(){
$no_of_items = $this->getItemsCount();
$local_distance =
$this->getDistance();
$international_distance
= $this->_internationalDistance;
$fees = $no_of_items *
($local_distance * 0.8 + $international_distance * 1.2);
return $fees;
}
}
class LocalShipping
extends Shipping
{
public function
getFees(){
$no_of_items = $this->getItemsCount();
$local_distance =
$this->getDistance();
$fees = $no_of_items *
$local_distance * 0.8;
return $fees;
}
}
function
calculateShippingFees($items) {
// To print results to
the standard output you can use print
// Example:
// print “Hello
world!”;
$total = 0;
foreach ($items as
$key => $object) {
# code…
if($object instanceof
InternationalShipping || $object instanceof LocalShipping){
$total += $object->getFees();
} else {
$total = 0;
break;
}
}
echo $total;
}
// Do NOT call the
calculateShippingFees function in the code
// you write. The
system will call it automatically.
?>


1 comments:
thanks for sharing this i got 4.35 out of 5, yupiee :)
ReplyPost a Comment