Skip to content

Instantly share code, notes, and snippets.

View claytonjwong's full-sized avatar
🚂
🚂 I think I can... 🚂 I think I can... 🚂 I think I can...

Clayton Wong claytonjwong

🚂
🚂 I think I can... 🚂 I think I can... 🚂 I think I can...
View GitHub Profile
@claytonjwong
claytonjwong / rev_linked_list.js
Created December 22, 2021 15:26
reverse linked list
//
// Recursively change each node of the linked list such that next
// is the last node seen during a linear scan of the linked list.
//
let reverseList = head => {
let go = (node = head, last = null) => {
let next = node.next;
node.next = last;
if (!next)
return node;
@claytonjwong
claytonjwong / rev_linked_list.kt
Created December 22, 2021 15:26
reverse linked list
//
// Recursively change each node of the linked list such that next
// is the last node seen during a linear scan of the linked list.
//
class Solution {
fun reverseList(head: ListNode?): ListNode? {
fun go(node: ListNode? = head, last: ListNode? = null): ListNode? {
var next = node?.next
node?.next = last
if (next == null)
@claytonjwong
claytonjwong / transpose_matrix.md
Created April 8, 2022 19:19
transposes a matrix, ie. transform rows into columns

Kotlin

var transpose = { A: Array<IntArray> -> A[0].mapIndexed{ j, _ -> A.mapIndexed{ i, _ -> A[i][j] }.toIntArray() }.toTypedArray() }

Javascript

let transpose = A => A[0].map((_, j) => A.map((_, i) => A[i][j]));
@claytonjwong
claytonjwong / count.md
Last active September 12, 2023 13:33
Create a map `m` to count the frequency of each character `c` of the string `s`.

Create a map m to count the frequency of each implicit character c of the string s.

  • Note: The character c is explicit in Rust and C++

Kotlin

var m = s.groupingBy{ it }.eachCount()
@claytonjwong
claytonjwong / monty_hall.py
Last active November 14, 2023 01:51
Monty Hall Problem
# https://en.wikipedia.org/wiki/Monty_Hall_problem
import random
N = int(1e5)
door = [1, 2, 3]
same = 0
diff = 0
#!/bin/bash
git config --global user.name "claytonjwong"
git config --global user.email claytonjwong@gmail.com
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.au 'add . -u'
git config --global alias.di diff