In this test there are 4 questions they are following.
Question no 1:- Fantastic three sequence
Solution :-<?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 no 2 :-Counts Days
Solution :-
<?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); $diff2 = strtotime($date[2]."-01-01 00:00:00"); $diff3 = $diff -$diff2; echo round($diff3/86400)+1; } else { echo "Bad format"; } } // Do NOT call the countDays function in the code // you write. The system will call it automatically. ?>
Question no 3 :- Change Nickname.
Solution :-<?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 no 4 :- Calculate shipping fees.
Solution :-<?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; private $_percent_d; private $_percent_i_d; public function __construct($itemsCount, $distance, $internationalDistance){ parent::__construct($itemsCount, $distance); $this->_internationalDistance = $internationalDistance; $this->_percent_d = 0.8; $this->_percent_i_d = 1.2; } public function getInternationalDistance(){ return $this->_internationalDistance; } #local shipping: number of items * distance * .8 #international shipping: number of items * #( local distance * .8 + international distance * 1.2) public function getFees(){ $number_of_items = $this->getItemsCount(); $local_distence = $this->getDistance(); $internalDistance= $this->getInternationalDistance(); $_percent_d = $this->_percent_d; $_percent_i_d = $this->_percent_i_d; return $number_of_items * ( $local_distence * $_percent_d + $internalDistance * $_percent_i_d ); } } class LocalShipping extends Shipping { private $_percent_d; public function __construct($itemsCount, $distance){ parent::__construct($itemsCount, $distance); $this->_percent_d = 0.8; } #number of items * distance * .8 public function getFees(){ $number_of_items = $this->getItemsCount(); $local_distence = $this->getDistance(); $_percent_d = $this->_percent_d; return $number_of_items * ( $local_distence * $_percent_d ); } } function calculateShippingFees($items, $out = 'print') { // To print results to the standard output you can use print // Example: // print "Hello world!"; $sum = 0; foreach ($items as $item){ if(! is_a($item, "Shipping")) $sum += 0; else $sum += $item->getFees(); } if($out == 'print') print( $sum ); return $sum; } // Do NOT call the calculateShippingFees function in the code // you write. The system will call it automatically. ?>
Tips:-
Do not make any white spaces between codes and test you code before submit question. Enjoy....!
No comments:
Post a Comment