Skip to content

Instantly share code, notes, and snippets.

View elishaukpong's full-sized avatar
🇳🇬
Learning something new somewhere!

Elisha Ukpong elishaukpong

🇳🇬
Learning something new somewhere!
View GitHub Profile
@elishaukpong
elishaukpong / diagonalDifference.php
Last active August 11, 2021 01:03
Problem: Given a square matrix, calculate the absolute difference between the sums of its diagonals. Link: https://www.hackerrank.com/challenges/diagonal-difference/problem
function diagonalDifference($arr) {
//initialize a new array to hold the diagonals, left and right
$rightD = [];
$leftD = [];
//iterate through the parent array, taking note of it's key
foreach($arr as $key => $lineArray)
{
//iterate through the diagonal lines, take note of the keys
foreach ($lineArray as $innerKey => $value){
@elishaukpong
elishaukpong / Mini-MaxSum.php
Last active August 10, 2021 23:46
Problem Statement: Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers. Link: https://www.hackerrank.com/challenges/mini-max-sum/problem
function miniMaxSum($arr) {
$sumValue = [];
foreach($arr as $key => $integer)
{
$sumValue[] = array_sum(array_filter($arr, function($innerKey) use ($key){
return $innerKey !== $key;
},ARRAY_FILTER_USE_KEY));
}
@elishaukpong
elishaukpong / socksMerchantProblem.php
Last active August 9, 2021 12:43
There is a large pile of socks that must be paired by color. Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are.
//PROBLEM
//There is a large pile of socks that must be paired by color.
//Given an array of integers representing the color of each sock,
//determine how many pairs of socks with matching colors there are.
//More details about problem can be found here
//https://www.hackerrank.com/challenges/sock-merchant/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=warmup
function sockMerchant($n, $ar) {
// Get the unique version of the array and the count of value appearance.
@elishaukpong
elishaukpong / staircase.php
Last active August 8, 2021 12:34
Solution to the Staircase problem
<!-- Staircase detail
This is a staircase of size 4:
#
##
###
####
Its base and height are both equal to . It is drawn using # symbols and spaces. The last line is not preceded by any spaces.
@elishaukpong
elishaukpong / covarianceAndContraVariance.php
Last active August 7, 2021 08:38
I have been thinking of where/how to utilise this concept in a day to day development and i realised this can be utilised in our abstract factory methods design pattern and it's composition. Check on the abstract factory on my other gists to get understanding
<?php
class Food
{
}
class AnimalFood extends Food
{
}
@elishaukpong
elishaukpong / simpleFactoryExample.php
Last active August 4, 2021 21:02
After learning about the simple factory pattern, i made up a problem scope and used the pattern to solve it.
<?php
//a boy has two shows and can decide to wear anyone of it at any time,
//the below class shows the implementation without applying any design patterns
class ShoeWithoutFactory
{
const CHURCH = 1;
const SCHOOL = 2;
@elishaukpong
elishaukpong / ReflectionAPIExample.php
Created August 2, 2021 23:00
An example to drive home the utilisation of the reflection api
<?php
class Browser
{
public static function browse($callable)
{
$params = (new ReflectionFunction($callable))->getParameters();
$args = [];
@elishaukpong
elishaukpong / ReflectionAPI
Created July 27, 2021 08:04
This makes a demonstration of the PHP Reflection API
<?php
class Person{
public $name;
public function __construct(string $name){
$this->name = $name;
}
}
<?php
//NULLSAFE OPERATOR
//class User {
// public function profile(){
// return new Profile;
// }
//}
//
<!--
Copyright 2018 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software