Skip to content

Instantly share code, notes, and snippets.

@Andruj
Andruj / ScriptingLanguageSpec.md
Last active November 14, 2015 04:00
A idea I had for a compile-to-js scripting language, primarily dealing with streams and file processing.

A Node Scripting Language (TB Named)

Variables

// Traditional JavaScript Primitives
$1 = [1,2,3,4]
$2 = 'String'
$3 = true
@Andruj
Andruj / merge_sort.dart
Created August 23, 2016 03:08
An implementation of merge sort in Dart.
List merge(List first, List second) {
if(first.isEmpty) {
return second;
}
if(second.isEmpty) {
return first;
}
if(first.first <= second.first) {
@Andruj
Andruj / peaks.dart
Created August 23, 2016 03:16
An implementation of the peaks problem in Dart.
// Must have be non-empty.
peaks(list) {
if(list.length == 1) {
return list.first;
}
if(list.length == 2) {
return list.reduce((prev, curr) => curr >= prev ? curr : prev);
}
@Andruj
Andruj / binary_search.dart
Created August 23, 2016 03:38
An implementation of binary search in Dart.
search(List list, int value, int min, int max) {
if(min > max) {
return null;
}
final int mid = (max + min) ~/ 2;
if(value < list[mid]) {
return search(list, value, min, mid - 1);
}
@Andruj
Andruj / graph.dart
Created August 23, 2016 04:24
An implementation of graphs in Dart.
class Node {
dynamic value;
bool discovered = false;
List<Node> links = [];
int distance = 0;
int weight;
@override
String toString() =>
'''
@Andruj
Andruj / huffman_coding.dart
Created August 23, 2016 04:41
An implementation of Huffman coding in Dart.
String encode(String message) {
final Map frequencies = count(message);
final sorted = frequencies.values.toList()
..sort();
print(sorted);
return '';
}
Map<String, int> count(String message) {
Map freq = {};
@Andruj
Andruj / activity_selection.dart
Created August 23, 2016 19:11
An implementation of greedy activity selection in Dart.
class Activity {
String name;
int start;
int end;
@override
String toString() => '$name: $start, $end';
}
@Andruj
Andruj / dynamic_fibonacci.dart
Created August 27, 2016 19:44
A memoized fibonacci sequence calculator.
var results = {
0: 0,
1: 1
};
fib(n) {
if(!results.containsKey(n)) {
results[n] = fib(n - 1) + fib(n - 2);
}
// This method takes O(n) time but only O(1) space.
fib(int n) {
if(n == 0) {
return 0;
}
else {
var previousFib = 0;
var currentFib = 1;
while(--n != 0) { // loop is skipped if n = 1
@Andruj
Andruj / shortest_weighted_path.dart
Created August 27, 2016 22:09
An implementation of the DP shortest weighted path problem.
import 'dart:math';
// The cost function c(i, j) = ...
final costs = [
[1,2,3,4,6], // 0
[5,6,2,3,1], // 1
[6,2,0,2,6], // 2
[5,0,2,8,6], // 3
[1,2,3,4,5], // 4
// 0 1 2 3 4