All posts in odesk tests

php-frontend-developer-test-v2-answer 2014

Fantastic three sequence

 

The fantastic three sequence 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 sequence are:

0, 1, 1, 1, 2, 3, 5, etc

Write a function that prints the Nth number in the fantastic three sequence to standard output.

 

<?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 countDays which takes a single parameter named dateinstring 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 dateInString until the date represented in dateInString. If the value of dateInString is invalid, the function should print ”Bad format”  to the console.

 

 

  1. function countDays($dateInString)
  2. {
  3.     $data = explode(‘.’, $dateInString);
  4.     $begining = date_create($data[2] . ‘-01-01′);
  5.     $data = date_create($data[2] . ‘-’ . $data[0] . ‘-’ . $data[1]);
  6.     $interval = $data->diff($begining);
  7.     echo $interval->format(‘%R%a days’);
  8. }

Change Nickname

 

The following from enable user to change their nickname on their website.

 

 

 

  1. <?php

     

    function changeNickname($oldNickname, $newNickname, $users) {

    <?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.

    ?>

     

     


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)

 

 

<?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.

 

?>