Answer:
<?php
function mostVowels($sentence) {
$sentence = preg_replace("/[^A-Za-z0-9 ]/", '', $sentence);
$words = explode(' ', $sentence);
$maxWord;
$maxCount = 0;
foreach($words as $word) {
preg_match_all('/[aeiou]/i',$word, $matches);
$curCount = count($matches[0]);
if ($curCount > $maxCount) {
$maxCount = $curCount;
$maxWord = $word;
}
}
return $maxWord;
}<?php
function changeName(&$newName) {
$newName = 'blah';
}
function changeAgain($newName) {
$newName = 'Keith';
}
$myName = 'Keith';
changeName($myName);
changeAgain($myName);
echo $myName;Answer: It echos 'blah', changeName takes a reference and changeAgain passes by value.
<?php
class SomeImportantClass {
public function someFunction($someParameter, $otherParameter) {
$someData = SomeSingleton->getInstance()->calculateStuff($otherParameter);
$result = ... // operate on $someData and $someParameter
return $result;
}
}Answer:
<?php
class SomeImportantClass {
protected $singleton;
public function __construct($singleton=SomeSingleton) {
$this->singleton = $singleton;
}
public function someFunction($someParameter, $otherParameter) {
$someData = $this->singleton->getInstance()->calculateStuff($otherParameter);
$result = ... // operate on $someData and $someParameter
return $result;
}
}As a deeper answer, since a singleton is being used in this class, when writing tests either the singleton should implement a reset function for the test setup and teardown or reflection api can be used to reinstance the singleton. In general though, singleton pattern makes for difficult testing since you are spreading what is effectively a global scope all over the code.
Answer:
<?php
function isPrime($number) {
for ($i=2; $i < $number; $i++) {
if($number % $i == 0) {
return false;
}
}
return true;
}| items | orders | delivery_address | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
|
Write a query that retrieves order IDs that contained a Hat, and include the delivery address if provided
Answer:
SELECT id, address FROM orders
JOIN items ON orders.id = items.order_id
LEFT JOIN delivery_address ON orders.id = delivery_address.order_id
WHERE item_name = 'Hat';
Write a query that retrieves each order and the total count of all items sold for each order. Sort the results per orders having sold the highest counts of “distinct” items
Answer:
SELECT i.order_id, i.sum AS total_count
FROM (
SELECT order_id, count(order_id), sum(quantity) FROM items
GROUP BY order_id
ORDER BY count DESC
) i
Consider the two arrays, $carMakeCount and $carMakeType. Write a function that maps both datasets and returns an array having a structure identical to $exampleMapped:
<?php
$carMakeCount = ['lexus' => 4, 'bmw' => 2, 'mercedes' => 5];
$carMakeType = ['suv'=>'lexus', 'coupe'=>'mercedes', 'sedan'=>'bmw'];
$exampleMapped = [
'lexus has 4 suv',
'bmw has 2 sedan',
'mercedes has 5 coupe'
];Answer:
<?php
function exampleMap($carMakeCount, $carMakeType) {
return array_map(function ($make, $type) {
return "$make has $carMakeCount[$make] $type";
}, $carMakeType, array_keys($carMakeType));
}Design a set of PHP classes in an object oriented manner to model various shapes. Start with a Shape class where a shape is any two dimensional figure that has the following characteristics: name, perimeter, area.
Answer:
<?php
interface IShape {
public function area();
public function perimeter();
}
interface IStringable {
public function toString();
}
class Shape {
protected $name;
public function __construct($name) {
$this->name = $name;
}
public function name() {
return $this->name;
}
}<?php
class Rectangle extends Shape implements IShape, IStringable {
protected $width;
protected $height;
public function __construct($width, $height, $name="Rectangle") {
parent::__construct($name);
$this->width = $width;
$this->height = $height;
}
public function area() {
return $this->width * $this->height;
}
public function perimeter() {
return 2 * ($this->width + $this->height);
}
public function toString() {
return "[$this->name] width: $this->width, height: $this->height";
}
}
class Circle extends Shape implements IShape, IStringable {
protected $radius;
public function __construct($radius, $name="Circle") {
parent::__construct($name);
$this->radius = $radius;
}
public function area() {
return $this->radius * $this->radius * pi();
}
public function perimeter() {
return 2 * pi() * $this->radius;
}
public function toString() {
return "[$this->name] radius: $this->radius";
}
}