Skip to content

Instantly share code, notes, and snippets.

View ralphmoran's full-sized avatar
🎯
Working on something interesting

Ralph Moran ralphmoran

🎯
Working on something interesting
View GitHub Profile
@ralphmoran
ralphmoran / DiagonalDifference.php
Last active September 29, 2019 21:00
Diagonal difference. Given a square matrix, calculate the absolute difference between the sums of its diagonals. #hackerrank #test #code
<?php
/**
* Given a square matrix, calculate the absolute difference between the sums of its diagonals.
*
* @param array $matrix
* @return integer
*/
function diagonal_difference(array $matrix)
{
@ralphmoran
ralphmoran / Pipeline.php
Created September 24, 2019 05:39
Pipeline of functions. #testdome #pipeline
<?php
class Pipeline
{
/**
* Executes a pipeline of functions.
*
* @return void
*/
public static function makePipeline()
@ralphmoran
ralphmoran / ParseCsv.php
Created September 24, 2019 05:29
Parse CSV function. #testdome #parse #csv
<?php
function parseCsv( string $csv, string $delimiter)
{
return str_getcsv($csv, $delimiter);
}
$csv = 'a,b,c,"d,e",f,g';
$parsedArray = parseCsv($csv, ',');
@ralphmoran
ralphmoran / FindUnorderedNeedles.php
Created September 24, 2019 05:20
Find unordered needles in haystack. #testdome #haystack #needles
<?php
$haystack = 'abcdefghijklmnopqrstuvwxy';
$needles = array('z', 'g', 'a', 'r');
array_walk($needles, function($needle) use ($haystack) {
print strpos($haystack, $needle) !== FALSE ? "'{$needle}' exist in haystack" : "'{$needle}' does not exist";
});
@ralphmoran
ralphmoran / Palindrome.php
Created September 24, 2019 05:16
Palindrome words. #testdome #palindrome
<?php
class Palindrome
{
/**
* Validates is a word is a palindrome.
*
* @param [type] $word
* @return boolean
*/
@ralphmoran
ralphmoran / sortByPriceAscending.php
Created September 24, 2019 05:03
Sort by price (ASC), if there are two or more items with the same price, it orders the list by name (ASC). #testdome #sortByPriceAscending
<?php
function sortByPriceAscending(string $jsonString) : string
{
$items = json_decode($jsonString);
usort($items, function($a, $b)
{
$cmp = $a->price - $b->price;
@ralphmoran
ralphmoran / CategoryTree.php
Created September 24, 2019 04:58
Category tree. #testdome #categories
<?php
class CategoryTree
{
/** @var array $categoryTree */
private $categoryTree = [];
/**
* Adds new root category or creates a parent-child relationship.
*
@ralphmoran
ralphmoran / ReturnFileExtension.php
Created September 24, 2019 03:10
Returns only the file extension. #extension #file
<?php
$files = [
'blah.gif',
'my.file.gif',
'my.file.jpg',
'this/is/a/path',
'my.file.xlsx',
'this/is/a/path',
'my.file.php',
@ralphmoran
ralphmoran / ShowUserClasses.php
Created September 22, 2019 07:24
It shows user classes only - #reflection #user #class
<?php
class First {}
class Second {}
class Third extends Second {}
class Fourth {}
$userClasses = array_filter(
get_declared_classes(),
function($className) {
@ralphmoran
ralphmoran / ShowUserClasses.php
Created September 22, 2019 07:24
It shows user classes only - #reflection #user #class
<?php
class First {}
class Second {}
class Third extends Second {}
class Fourth {}
$userClasses = array_filter(
get_declared_classes(),
function($className) {