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 / Git-Worktree.md
Created March 30, 2026 13:37
Git Worktree Guide

Frequently Used Git Worktree Commands

Basic Operations

git worktree add <path> <branch>         # Create worktree for a branch that already exists in the repo
git worktree add -b <new-branch> <path>  # Create worktree with new branch
git worktree list                        # List all worktrees
git worktree remove <path>               # Remove a worktree
@davidon
davidon / NullObjectPattern.md
Last active March 30, 2026 13:40
Null Object Pattern in PHP

Null Object Pattern in PHP

Overview

The Null Object pattern replaces null return values with a special "do-nothing" object that shares the same interface as the real object. This eliminates the need for null checks at every call site and prevents "Call to a member function on null" fatal errors.

When to use it

@davidon
davidon / FrontEndKnowledgeBase.md
Last active March 10, 2026 12:55
Front End Knowledge Base — Dev Notes & Q&A (Web, JS, Git, Tools)
@davidon
davidon / reducer-localstorage-tasks-react.markdown
Created February 25, 2026 04:16
Reducer-LocalStorage-Tasks-React
@davidon
davidon / app-localstorage-tasks-react.markdown
Created February 25, 2026 04:14
App-LocalStorage-Tasks-React
@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);