Skip to content

Instantly share code, notes, and snippets.

@Andruj
Andruj / pipe.md
Created July 30, 2018 22:43
A conceptual meta-bash language.

Pipe

Language

Operators

final List<int> heap = [];
bubble(int index) {
while(index > 0) {
int parent = index ~/ 2;
if(heap[index] < heap[parent]) {
// Swap values.
@Andruj
Andruj / djikstras.dart
Created August 30, 2016 21:44
Djikstra's algorithm.
class Vertex {
int distance;
int weight;
List<Vertex> edges;
int value;
bool visited;
String toString() => '$value';
}
class PriorityQueue {
@Andruj
Andruj / longest_increasing_sub_length.dart
Created August 27, 2016 23:05
Longest increasing subsequence in length.
import 'dart:math';
longest(List values) {
final LIS = new List(values.length);
LIS[0] = 1;
for(int i = 1; i < values.length; i++) {
var max = 1;
for(int j = i; j > 0; j--) {
if(values[j] < values[i] && LIS[j] > max) {
max = LIS[j];
@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
// 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 / 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);
}
@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 / 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 / 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() =>
'''