Skip to content

Instantly share code, notes, and snippets.

View davidon's full-sized avatar
💯

William [...W->D] Duan davidon

💯
  • W.W.D - Whizz Web Development
  • Melbourne
View GitHub Profile
@davidon
davidon / hourglass.cs
Last active December 13, 2018 11:56
https://www.hackerrank.com/challenges/2d-array/problem ; Given a 2D Array, find the maximum hourglass sum of all 16 hourglasses
using System.IO;
using System;
class Solution {
// Complete the hourglassSum function below.
static int hourglassSum(int[][] arr) {
int[] sum = new int[16];
int k = 0;
for (int i = 0; i <= 3; i++)
@davidon
davidon / format_phone_number.php
Created November 24, 2018 00:54
Format phone number which contains spaces and dashes into every three numbers separated by dashes
public function format_phone_number($S) {
$S = str_replace(' ', '', $S);
$S = str_replace('-', '', $S);
$len = strlen($S);
$num_groups = floor($len / 3);
$remain = $len % 3;
if ($num_groups >= 1 && $remain == 1) {
$num_groups -= 1;
$remain += 3; //4
@davidon
davidon / max_profit.cpp
Last active November 17, 2018 08:53
C++ - To get the max gap in an array supposing gap can only be a later number minus a former number
#include "pch.h"
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <time.h>
using namespace std;
int get_max_profit(vector<int> prices);
<?php
//this is to get the max gap in an array supposing gap can only be a later number minus a former number
$stock_prices_yesterday = [25, 7, 5, 8, 11, 9];
$m_profit = GetMaxProfit($stock_prices_yesterday);
# returns 6 (buying for $5 and selling for $11)
print "1st:the most profit should be 6: {$m_profit}";
$stock_prices_yesterday = [10, 7, 11, 5, 8, 9];
$m_profit = GetMaxProfit($stock_prices_yesterday);
@davidon
davidon / find_first_positive_number_not_in_array.php
Created November 15, 2018 05:45
find first positive number which is not in an numerical array
<?php
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP7.0
$arr_checker = range(1, 100000);
$A = array_unique($A, SORT_NUMERIC );
sort($A);
@davidon
davidon / str2pascal.php
Last active October 1, 2020 10:23
PHP: Conver a string into StudlyCaps,AKA PascalCase
/**
* Convert a value to studly caps case.
*
* @param string $value
*
* @return string
*/
public function studly(string $value): string
{
return \str_replace(' ', '', \ucwords(\str_replace(['-', '_'], ' ', $value)));
@davidon
davidon / str2snake.php
Created November 3, 2018 22:24
PHP: Convert a string to snake case
/**
* Convert a string to snake case.
*
* @param string $value
* @param string $delimiter Default to underscore
*
* @return string
*/
public function snake(string $value, ?string $delimiter = null): string
{
@davidon
davidon / MyLogger.php
Last active January 15, 2019 23:09
my good logger class with backtrace
<?php
namespace App\Debugger;
/**
* usage example: $log = new MyLogger(); $logger->log($logger::ARGTRACE_NONE . $logger::BACKTRACE_NONE . $logger::SELF_ARGUMENTS_LOW, .......)
*/
//for Symfony project, this is put under src/Debugger/ folder
//for Lumen and Laravel, this is put under app/Debugger/ folder
//create logs directory under PROJECT_HOME