Skip to content

Instantly share code, notes, and snippets.

@bklein01
Last active February 18, 2019 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bklein01/8d1add163b6bfe157cbc88c6e2e85839 to your computer and use it in GitHub Desktop.
Save bklein01/8d1add163b6bfe157cbc88c6e2e85839 to your computer and use it in GitHub Desktop.
PHP Skills Test
<?php
/*
Second Assignment
Write a script, module, application or program in PHP, which upon execution accepts
two arguments. One of those arguments should be a "total cost" (in dollars and/or cents)
and the other an "amount
provided" (also in dollars and/or cents). Return as output the change that should be
provided, by returning the count of each denomination of bills and/or coins. Extra
points for object oriented and/or advanced concepts.
*/
function makeChange($amount) {
if ($amount >= 100) {
$numBills = floor($amount / 100);
echo "You get {$numBills} $100 bills.\n";
$amountLeft = round($amount - ($numBills * 100), 2);
if ($amountLeft > 0) {
makeChange($amountLeft);
}
} elseif ($amount >= 50) {
$numBills = floor($amount / 50);
echo "You get {$numBills} $50 bills.\n";
$amountLeft = round($amount - ($numBills * 50), 2);
if ($amountLeft > 0) {
makeChange($amountLeft);
}
} elseif ($amount >= 20) {
$numBills = floor($amount / 20);
echo "You get {$numBills} $20 bills.\n";
$amountLeft = round($amount - ($numBills * 20), 2);
if ($amountLeft > 0) {
makeChange($amountLeft);
}
} elseif ($amount >= 10) {
$numBills = floor($amount / 10);
echo "You get {$numBills} $10 bills.\n";
$amountLeft = round($amount - ($numBills * 10), 2);
if ($amountLeft > 0) {
makeChange($amountLeft);
}
} elseif ($amount >= 5) {
$numBills = floor($amount / 5);
echo "You get {$numBills} $5 bills.\n";
$amountLeft = round($amount - ($numBills * 5), 2);
if ($amountLeft > 0) {
makeChange($amountLeft);
}
} elseif ($amount >= 1) {
$numBills = floor($amount / 1);
echo "You get {$numBills} $1 bills.\n";
$amountLeft = round($amount - ($numBills * 1), 2);
if ($amountLeft > 0) {
makeChange($amountLeft);
}
} elseif ($amount >= .25) {
$numBills = floor($amount / .25);
echo "You get {$numBills} quarters.\n";
$amountLeft = round($amount - ($numBills * .25), 2);
if ($amountLeft > 0) {
makeChange($amountLeft);
}
} elseif ($amount >= .10) {
$numBills = floor($amount / .10);
echo "You get {$numBills} dimes.\n";
$amountLeft = round($amount - ($numBills * .10), 2);
if ($amountLeft > 0) {
makeChange($amountLeft);
}
} elseif ($amount >= .05) {
$numBills = floor($amount / .05);
echo "You get {$numBills} nickels.\n";
$amountLeft = round($amount - ($numBills * .05), 2);
if ($amountLeft > 0) {
makeChange($amountLeft);
}
} elseif ($amount >= .01) {
$numBills = floor($amount / .01);
echo "You get {$numBills} pennies.\n";
$amountLeft = round($amount - ($numBills * .01), 2);
if ($amountLeft > 0) {
makeChange($amountLeft);
}
}
}
$options = getopt("u:d:");
if (!isset($options['u']) || (isset($options['u']) && $options['u'] == '')) {
die("Invalid Parameters: expecting '-u [total cost] -d [amount provided]'\n");
} else {
$total = (float)$options['u'];
if (!isset($options['d']) || (isset($options['d']) && $options['d'] == '')) {
die("Invalid Parameters: expecting '-u [total cost] -d [amount provided]'\n");
} else {
$amountProvided = (float)$options['d'];
}
}
if ($amountProvided < $total) {
$change = $total - $amountProvided;
echo "Not Enough Provided. You Still Need To Give \${$change}.\n";
} else if ($amountProvided == $total) {
$change = 0;
echo "No Change Is Required.\n";
} else {
$change = round($amountProvided - $total, 2);
echo "You Get \${$change} in change.\n";
makeChange($change);
}
<?php
/*
Performance Test: Simple PHP Crawler
Create a simple web crawler.
User Input
Takes an URL (like http://cnn.com) as an input from the user;
Takes depth (integer number >=1) as an input from the user;
PHP Processing
The application goes to the specified URL and creates a list of all hyperlinks there.
If the specified depth is greater than 1, the app follows each link from the above list,
and visits each page to gather all of the links there. When it is done, with the whole
list, depth 2 is complete.
Repeat steps 1 and 2 until the depth given by the user is reached. If depth 1 is given,
step 2 is skipped.
The application generates simple HTML report in the browser for each URL visited
containing the Depth number, the URL of the page and a list of all links found at that
URL.
*/
$options = getopt("u:d:");
if (!isset($options['u']) || (isset($options['u']) && $options['u'] == '')) {
die("Invalid Parameters: expecting '-u [url to crawl] -d [depth >= 1] >crawl.txt'\n");
} else {
$url = $options['u'];
if (!isset($options['d']) || (isset($options['d']) && $options['d'] == '')) {
die("Invalid Parameters: expecting '-u [url to crawl] -d [depth >= 1] >crawl.txt'\n");
} else {
$depth = (int)$options['d'];
if ($depth <= 0) {
die("Invalid Parameters: expecting '-u [url to crawl] -d [depth >= 1] >crawl.txt'\n");
}
}
}
function crawlPage($url, $iteration, $depth) {
$page = file_get_contents($url);
$regex = '/href="(.*?)"/s';
$searchResults = [];
if ( preg_match_all($regex, $page, $list) ) {
$links = $list[0];
foreach ($links as $link) {
$link = str_replace('href=', '', $link);
$link = str_replace('"', '', $link);
$searchResults[] = array(
'link' => $link,
'iteration' => $iteration
);
var_dump($searchResults);
if ($iteration < $depth) {
$searchResults[] = crawlPage($url, $iteration + 1, $depth);
}
}
return $searchResults;
} else {
return $searchResults;
}
}
$searchResults = crawlPage($url, 1, $depth);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment