Skip to content

Instantly share code, notes, and snippets.

@moff
Last active April 13, 2017 13:51
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 moff/a5263d29a5e436561e01b3afa65a97ce to your computer and use it in GitHub Desktop.
Save moff/a5263d29a5e436561e01b3afa65a97ce to your computer and use it in GitHub Desktop.
<?php
/*
1. Create a function that combines the two arrays.
Example:
[a, b, c], [1, 2, 3] => [a, 1, b, 2, c, 3]
*/
function combiner(array $a = [], array $b = []) {
$a_len = count($a);
$b_len = count($b);
if ($a_len > $b_len) {
$max = $a_len;
} else {
$max = $b_len;
}
$combined = [];
for ($i = 0; $i < $max; $i++) {
// commented out wrong approach - it could combine values wrong in case of mixed indexes (strings and integers)
// if (array_key_exists($i, $a)) $combined[] = $a[$i];
// if (array_key_exists($i, $b)) $combined[] = $b[$i];
if (!empty($a)) $combined[] = array_shift($a);
if (!empty($b)) $combined[] = array_shift($b);
}
return $combined;
}
$a = ['a', 'b', 'c'];
$b = [1, 2, 3];
print_r(combiner($a, $b));
/*
2. Create a function that returns the required number of digits from the Fibonacci series.
*/
function fibonacci(int $n = 0) {
if ($n <= 0) return [];
if ($n == 1) return [1];
if ($n > 1) {
$numbers = [1,1];
$num1 = 1;
$num2 = 1;
}
for ($i = 2; $i < $n; $i++) {
$next = $num1 + $num2;
$numbers[] = $next;
$num1 = $num2;
$num2 = $next;
}
return $numbers;
}
print_r(fibonacci(9));
/*
3. Supplement the function code so that the function calculates dimensions of an image
($imgWidth, $imgHeight), which must fit into the screen ($canvasWidth, $canvasHeight). The size
must be returned by reference to variables $resultWidth and $resultHeight. The function must
maintain the aspect ratio of the original image.
*/
function computeImageSize($imgWidth, $imgHeight, $canvasWidth, $canvasHeight, &$resultWidth, &$resultHeight) {
$imageRatio = $imgWidth / $imgHeight;
$canvasRatio = $canvasWidth / $canvasHeight;
if ($imageRatio > $canvasRatio) {
$resultWidth = $canvasWidth;
$resultHeight = $resultWidth / $imageRatio;
}
if ($imageRatio < $canvasRatio) {
$resultHeight = $canvasHeight;
$resultWidth = $resultHeight * $imageRatio;
}
if ($imageRatio == $canvasRatio) {
$resultHeight = $canvasHeight;
$resultWidth = $canvasWidth;
}
}
$resultWidth = 0;
$resultHeight = 0;
computeImageSize(600, 200, 300, 300, $resultWidth, $resultHeight);
print_r([$resultWidth, $resultHeight]);
/*
4. Imagine a situation when you have to do a code review for your colleague. How would you comment this code:
*/
$folders_temp = [];
foreach ($folders_temp as $fid => $fold) {
// TODO $ symbol missing before level-variable
// code isn't readable, when it has ternary inside another one
// variable names should have meaning in English, e.g. cislo_naslednika isn't a good identifier for a variable
// comments should be in English also
//уровень папки, если не превосходит -999, тогда это подпапка
level = ($fold["parent_id"] != -999) ? (($folders_temp[strval($fold["parent_id"])]["parent_id"] != -999) ? 1 : 1) : 0;
$folders_temp[strval($fid)]["level"] = $level;
if ($level && $last_parent[$level] != $fold['parent_id']) {
$last_parent[$level] = $fold["parent_id"];
$cislo_naslednika[$level] = 1;
} else $cislo_naslednika[$level]++;
// if it's subfolder, the lines before icon must be identificated (||, |L, _L, ...)
if ($level) {
// semicolon missing at the end of a line;
$blah = ($child_num[$level] < $folders_temp[strval($fold["parent_id"])]["kids"]) ? 1 : 2
if ($level === 2) {
$blah += ($child_num[1] < $folders_temp[$folders_temp[strval($fold["parent_id"])]["parent_id"]]["kids"]) ? 4 : 8;
}
} else $blah = 0;
$folders_temp[strval($fid)]["cross"] = $blah;
}
/*
5. What is the content of variables $a?
*/
// 21
// ============================================= //
/*
Answers:
# What is OOP, instance, object, class?
Object is an instance of a class.
Class is a structure that describes objects that should have same properties and methods.
# What is the use of inheritance?
Inheritance helps to reduce code duplication and to make sure that class and its instances have properties and methods of a parent class.
# What is the use of and how are exceptions used in PHP?
Exceptions help to handle "exceptional situations" - situations when something goes wrong and we have to do something with it by catching those.
# What is a framework?
Framework is a platform for building applications. It consists of classes that provide different functionality on top of implemented patterns, e.g. MVC, routing, database access, ORM, etc.
# Give an example of some design patterns and explain their use.
MVC stands for Model-View-Controller. This patterns helps to separate logic, presentation and request handling. Makes apps flexible, implements separation of concerns.
Singleton pattern helps us to have just one instance of a class at a time, e.g. database access object.
# What is the use of version control systems?
It's an important tool for developer teams.
Developers can work on different parts of an application and everyone can roll back to previous version in case something goes wrong.
Helps to track changes that were made by others and so on.
# What does testing mean in programming (e.g. unittesty, ...)?
Testing helps to make sure that every part of an application works as expected. And - more important - it helps to find bugs, when application is being changed.
We allways know that something is broken when tests fail.
Testing is essential for application of any scale.
Unit testing is about testing that every piece of code works as expected.
Integration testing is about testing user experience (that different pieces of app work properly as a group), e.g. when user clicks a button in a form it should redirect him to expected page, etc.
# What is AJAX, REST API?
AJAX is a technology of getting data via async requests to server performed by JavaScript.
REST API is a concept of using proper http-verbs and URLs to request\send data.
Good example is CRUD operations made via AJAX in single-page applications.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment