Skip to content

Instantly share code, notes, and snippets.

View nickangtc's full-sized avatar

Nick Ang nickangtc

View GitHub Profile
@nickangtc
nickangtc / Contains Duplicate.js
Created May 20, 2023 12:47
LeetCode Contains Duplicates (solution)
// https://leetcode.com/problems/contains-duplicate/
// this solution beats 85% of other submissions in terms of runtime, 57% in terms of memory
/**
* @param {number[]} nums
* @return {boolean}
*/
var containsDuplicate = function(nums) {
const memo = {};
@nickangtc
nickangtc / Bear and Steady Gene.js
Created May 19, 2023 15:04
Hackerrank medium: Bear and Steady Gene (n^2 timeout solution - works but too slow)
// https://www.hackerrank.com/challenges/bear-and-steady-gene/problem
function steadyGene(gene) {
const geneArray = gene.split("");
const geneDict = geneArray.reduce(
// O(n) time
(dict, char) => {
return {
...dict,
[char]: dict[char] + 1,
@nickangtc
nickangtc / Sherlock and the Valid String.js
Last active May 18, 2023 16:03
Hackerrank medium: Sherlock and the Valid String
// https://www.hackerrank.com/challenges/sherlock-and-valid-string/problem
function isValid(s) {
const occurrences = {};
for (let i = 0; i < s.length; i++) {
const alphabet = s.charAt(i);
occurrences[alphabet] = occurrences[alphabet]
? occurrences[alphabet] + 1
: 1;
@nickangtc
nickangtc / modifying-ruby-builtin-classes.rb
Last active March 9, 2022 15:48
How Rails extends Ruby builtin classes is actually really simple...
# scenario
# ========
# suppose we want functionality like so on any string:
# "racecar".palindrome? #=> true
#
# where should we could we add that functionality?
# in ruby, this is surprisingly possible:
class String
def palindrome?
@nickangtc
nickangtc / triangle-koan-ruby.rb
Last active March 21, 2022 08:23
an example of beautiful ruby code used to validate a triangle as part of ruby koans exercise
# exercise is from http://rubykoans.com/ - a great way to learn ruby!
#! my not so succinct but works solution
def triangle(a, b, c)
validate_triangle(a, b, c)
if a == b && b == c
:equilateral
elsif a != b && b != c && c != a
:scalene
else
@nickangtc
nickangtc / ruby-array-slicing-weirdness.rb
Last active February 25, 2022 13:24
special case for accessing the first out-of-range element in an array
array = ['foo']
# slicing
p array[0,1] #=> ["foo"]
p array[1,1] #=> [] - SURPRISE!?
p array[2,1] #=> nil
# accessing
# no surprises
p array[0] #=> "foo"
# this example shows that in ruby you can
# define how an object is being compared
# to another object with a custom `==` method
class ProductItem
attr_reader :name, :quantity
def initialize(name, quantity)
@name = name
@quantity = quantity
@nickangtc
nickangtc / insert yyyymmddhhmm timestamp Apple Automator service.js
Last active February 17, 2020 16:36
Apple Automator script using JavaScript to generate a timestamp string in format YYYYMMDDHHMM
// To use JavaScript in Apple Automator, select 'Run JavaScript' action
// copy-paste this code into that action
// no need to have any other action -> output is printed automatically
function run(input, parameters) {
var now = new Date()
var year = now.getFullYear()
var month = now.getMonth() + 1
var day = now.getDate()
var hours = now.getHours()
@nickangtc
nickangtc / multi-git-win.md
Created December 2, 2018 08:33 — forked from rosswd/multi-git-win.md
Setting up a Github and Bitbucket account on the same computer on Mac OS. Now with a guide for Windows 10.

Setting up github and bitbucket on the same computer (Windows)

Guide for Windows

mix3d asked for some help using this guide with windows so here we go. This was tested with Windows 10. Run all commands in Git Bash once it's installed.

Github will be the main account and bitbucket the secondary.

Git for Windows

  • Download and install Git for Windows
    • In the installer, select everything but decide if you want a desktop icon (2nd step)
@nickangtc
nickangtc / iterm2-solarized.md
Created May 11, 2018 02:02 — forked from kevin-smets/iterm2-solarized.md
iTerm2 + Oh My Zsh + Solarized color scheme + Meslo powerline font + [Powerlevel9k] - (macOS)

Default

Default

Powerlevel9k

Powerlevel9k