Skip to content

Instantly share code, notes, and snippets.

@ben-ng
ben-ng / roman.py
Created September 21, 2016 15:51
Sample solution for converting roman numerals to integers
def convert(char):
return {
'M': 1000,
'D': 500,
'C': 100,
'L': 50,
'X': 10,
'V': 5,
'I': 1
}.get(char)
@ben-ng
ben-ng / mergesort.swift
Created December 1, 2016 07:00
Benchmarking improvements to the ArrayElementValuePropagation pass
import Cocoa
func mergeSort(_ array: [Int]) -> [Int] {
guard array.count > 1 else { return array }
let middleIndex = array.count / 2
let leftArray = mergeSort(Array(array[0..<middleIndex]))
let rightArray = mergeSort(Array(array[middleIndex..<array.count]))
return merge(leftPile: leftArray, rightPile: rightArray)
}
@ben-ng
ben-ng / try-1.txt
Last active December 5, 2016 01:10
Swift build failure logs
Welcome to Ubuntu 16.04.1 LTS (GNU/Linux 4.4.0-31-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
100 packages can be updated.
47 updates are security updates.
@ben-ng
ben-ng / a-repro.js
Last active March 24, 2017 18:20
Salesforce Marketing Cloud closure scope bug
function foo() {
function boom() {
Write('Boom');
};
function bar () {
function baz() {
boom(); // Fails here, "boom" can't be found
};
@ben-ng
ben-ng / a-repro.js
Created March 24, 2017 18:32
Salesforce Marketing Cloud constructor-from-closure bug
function newEnv () {
function Foo () {
Write('Hello World');
};
var f = {
Foo: Foo
}
return f;
@ben-ng
ben-ng / a-repro.js
Last active March 29, 2017 18:27
Salesforce Marketing Cloud Function.length bug
Platform.Load('Core', '1.0');
function moo (foo, bar) {};
Write(moo.length); // You'll get a .NET error if you do this, weirdly enough.
@ben-ng
ben-ng / a-repro.js
Last active March 28, 2017 21:01
AMD silently misbehaving on Salesforce Marketing Cloud
define('foo', function () {
function Foo () {
Write('Hello World');
};
return Foo;
})
require(['foo'], function (Foo) {
// Foo is undefined on Salesforce
@ben-ng
ben-ng / substr.js
Last active March 29, 2017 18:28
substr shim
// There is no String.substr on Salesforce. This uses String.substring instead.
// substring's arguments are different, so this just converts substr args to
// their substring equivalents.
function substrShim (str, start, len) {
if (len <= 0)
return '';
if (start < 0)
start = str.length + start;
@ben-ng
ben-ng / fizzbuzz.sh
Created March 4, 2019 05:39
FizzBuzz solved with data-flow programming in Bash.
#!/usr/bin/env bash
seq 1 100 | \
xargs -Ix -n1 echo "x + ((x % 3 == 0) + ((x % 5) == 0) * 2) * 1000" | \
bc | \
xargs -n1 printf "%04d\n" | \
sed 's/^1.*/Fizz/' | \
sed 's/^2.*/Buzz/' | \
sed 's/^3.*/FizzBuzz/' | \
sed 's/^0*//'
@ben-ng
ben-ng / reverse-linked-list.sh
Created March 4, 2019 06:02
Solution for reversing a linked list in bash
#!/usr/bin/env bash
# Where input.txt is:
# a>b
# b>c
# c>d
# d>e
paste -d '>' <(< input.txt | cut -d'>' -f2 | tac) <(< input.txt | cut -d'>' -f1 | tac)