Skip to content

Instantly share code, notes, and snippets.

@howardpanton
Last active August 29, 2015 14:18
Show Gist options
  • Save howardpanton/75a5067215fd9945d758 to your computer and use it in GitHub Desktop.
Save howardpanton/75a5067215fd9945d758 to your computer and use it in GitHub Desktop.
<?php
//Q1. PHP - Complete remove_nonAtoZ() below
function remove_nonAtoZ($input) {
// example data: $input = "BA'3Ndf$%^A&*(nN)A";
// $output = "BANANA";
// Create RegExpr to remove non capital letters
$pattern = "/[A-Z]+/";
// Filter using Preg Match
preg_match_all($pattern, $input, $match);
// Convert Array to String
$output = implode($match[0]);
return $output;
}
echo "Question 1: " . remove_nonAtoZ("BA'3Ndf$%^A&*(nN)A");
//Q2. PHP - Complete table_columns() below
function table_columns($input, $cols) {
// problem: array of values are ordered to fill an html table by *column* (ie second value should appear beneath the first etc)
// task: reorder array so that the values are in the order they will be output in html, filling each *row* of table at a time
// example inputs: $input = array('apple', 'orange', 'monkey', 'potato', 'cheese', 'badger', 'turnip'), $cols = 2
// example output: array(array('apple', 'cheese'), array('orange','badger'),...)
// Get the count of items in array and divide by column number.
$num = round(count($input) / $cols);
// Split the input into columns using the tptal count divived by 2
// First column should be values 0 to 4
$col_1 = array_slice($input, 0, $num);
// Second column should be value 4 upwards
$col_2 = array_slice($input, $num);
// Create an araray to hold mutlidimensional arrays
$array = array();
$i = 0;
// Loop through the array and create the pairs [keys and values] for the new arrays
while ($i < $num) {
$array[] = array($col_1[$i], $col_2[$i]);
$i++;
}
echo "\nQuestion 2:" ; print_r($array); }
$input = array('apple', 'orange', 'monkey', 'potato', 'cheese', 'badger', 'turnip');
$cols = 2;
table_columns($input, $cols);
<?php
//Q3. PHP - Complete first_day_of_next_month() below
function first_day_of_next_month($date) {
// Expect and validate $date in in the format "dd/mm/yyyy"
// Note that dd and mm of the provided date may be either one or two digits in length
// Return false if provided date is not in the expected format or is not a valid date
// Convert to the first day of the next month
// return the string of the same date in the format "yyyy-mm-dd 00:00:00" (month and day should be two digits always)
// Get the date and convert to array
$get_date = explode("/", $date);
// Check if we have a valid date
// We need to convert to integers
$day = (int) $get_date[0];
$month = (int) $get_date[1];
$year = (int) $get_date[2];
// Use PHP checkdate()
if (checkdate($day, $month, $year )) {
// Get next months date
$next_month = mktime(0, 0, 0, date($month)+1 , date("1"), date($year));
//retrurn the 1st day of next month in the correct format
return date('Y-m-d g:i:s', $next_month);
} else {
// return false if date is not in the right format
return false;
}
}
echo "Question 3: " . first_day_of_next_month("10/05/2015");
?>
// Q4. MySQL Write a query to summarise tasks for a team of develpers
// CREATE TABLE developers (id int auto_increment, name varchar(255), PRIMARY KEY (id));
// CREATE TABLE tasks (id int auto_increment, developer_id int, subject varchar(255), priority enum ('1','2','3'), status enum('queued','completed', 'approved'), PRIMARY KEY (id));
//
// Write a query to join the two tables together to get a listing:
// - Where each row represents a developer/status combination
// - Excludes those that are 'approved'
// - Shows a count of tasks
// - All 'queued' are listed before all 'completed'
// - The list is ordered by developer name within a status
// (ie The entire answer to this question is a single query that satisfys all of the above)
Q.4
SELECT developers.name, tasks.status, (SELECT count(*) FROM tasks WHERE tasks.status != "approved" AND dev.id = tasks.developer_id) as "Task count" FROM developers INNER JOIN tasks on developers.id = tasks.developer_id WHERE tasks.status != "approved" ORDER BY tasks.status ASC, developers.name;
// Q5. Javascript (JQuery optional)
// <form class="add_option_form">
// <input type="text" class="option_name" name="option_name" />
// <input type="submit" class="add_button" value="Add" />
// </form>
// <div class="checklist">
// <label><input type="checkbox" /><span class="option_name">Option 1</span></label>
// <label><input type="checkbox" checked="checked" /><span class="option_name">Option 2</span></label>
// </div>
// <div class="selected_options"></div>
//
// Write javascript that will:
// (a) Allow adding new checkboxes to div.checklist by using the form at the top
// (b) Make sure that div.selected_options always contains text consisting of the names of all selected checkboxes (including those added dynamically)
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment