Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

componentize

  • Loading branch information...
commit 25a90ca1f85e78c242469899923cae9ece8c660d 1 parent 371ed53
@jonathanong jonathanong authored
View
59 .gitignore
@@ -0,0 +1,59 @@
+# Compiled source #
+###################
+*.com
+*.class
+*.dll
+*.exe
+*.o
+*.so
+
+# Packages #
+############
+# it's better to unpack these files and commit the raw source
+# git has its own built in compression methods
+*.7z
+*.dmg
+*.gz
+*.iso
+*.jar
+*.rar
+*.tar
+*.zip
+
+# Logs and databases #
+######################
+*.log
+*.sql
+*.sqlite
+
+# OS generated files #
+######################
+.DS_Store*
+ehthumbs.db
+Icon?
+Thumbs.db
+
+# Node.js #
+###########
+lib-cov
+*.seed
+*.log
+*.csv
+*.dat
+*.out
+*.pid
+*.gz
+
+pids
+logs
+results
+
+node_modules
+npm-debug.log
+
+# Components #
+##############
+
+/build
+/components
+/vendors
View
7 Makefile
@@ -0,0 +1,7 @@
+build:
+ @coffee --compile --bare linear_partition.coffee
+
+test:
+ @node test
+
+.PHONY: build test
View
13 README.md
@@ -0,0 +1,13 @@
+# Linear Partition
+
+This is just a component and vanilla Javascript version of the original repo at https://github.com/crispymtn/linear-partition.
+
+## Changes
+
+- Remove underscore.js dependency
+- Export to `module.exports`
+- Use `->` vs. `=>` for definition to avoid `var _this = this`
+
+## License
+
+MIT, apparently.
View
11 component.json
@@ -0,0 +1,11 @@
+{
+ "name": "linear-partition",
+ "description": "Linear partition algorithm",
+ "version": "0.0.1",
+ "scripts": [
+ "linear_partition.js"
+ ],
+ "main": "linear_partition.js",
+ "license": "MIT",
+ "repo": "jonathanong/linear-partition"
+}
View
25 linear_partition.coffee
@@ -2,25 +2,34 @@
# Partitions a sequence of non-negative integers into k ranges
# Based on Óscar López implementation in Python (http://stackoverflow.com/a/7942946)
# Also see http://www8.cs.umu.se/kurser/TDBAfl/VT06/algorithms/BOOK/BOOK2/NODE45.HTM
-# Dependencies: UnderscoreJS (http://www.underscorejs.org)
# Example: linear_partition([9,2,6,3,8,5,8,1,7,3,4], 3) => [[9,2,6,3],[8,5,8],[1,7,3,4]]
-
-linear_partition = (seq, k) =>
+
+min = (arr) ->
+ for x in arr
+ computed = x[0]
+ if !result || computed < result.computed
+ result =
+ value: x
+ computed: computed
+
+ result.value
+
+module.exports = (seq, k) ->
n = seq.length
-
+
return [] if k <= 0
return seq.map((x) -> [x]) if k > n
-
+
table = (0 for x in [0...k] for y in [0...n])
solution = (0 for x in [0...k-1] for y in [0...n-1])
table[i][0] = seq[i] + (if i then table[i-1][0] else 0) for i in [0...n]
table[0][j] = seq[0] for j in [0...k]
for i in [1...n]
for j in [1...k]
- m = _.min(([_.max([table[x][j-1], table[i][0]-table[x][0]]), x] for x in [0...i]), (o) -> o[0])
+ m = min([Math.max(table[x][j-1], table[i][0]-table[x][0]), x] for x in [0...i])
table[i][j] = m[0]
solution[i-1][j-1] = m[1]
-
+
n = n-1
k = k-2
ans = []
@@ -28,5 +37,5 @@ linear_partition = (seq, k) =>
ans = [seq[i] for i in [(solution[n-1][k]+1)...n+1]].concat ans
n = solution[n-1][k]
k = k-1
-
+
[seq[i] for i in [0...n+1]].concat ans
View
107 linear_partition.js
@@ -0,0 +1,107 @@
+// Generated by CoffeeScript 1.6.3
+var min;
+
+min = function(arr) {
+ var computed, result, x, _i, _len;
+ for (_i = 0, _len = arr.length; _i < _len; _i++) {
+ x = arr[_i];
+ computed = x[0];
+ if (!result || computed < result.computed) {
+ result = {
+ value: x,
+ computed: computed
+ };
+ }
+ }
+ return result.value;
+};
+
+module.exports = function(seq, k) {
+ var ans, i, j, m, n, solution, table, x, y, _i, _j, _k, _l;
+ n = seq.length;
+ if (k <= 0) {
+ return [];
+ }
+ if (k > n) {
+ return seq.map(function(x) {
+ return [x];
+ });
+ }
+ table = (function() {
+ var _i, _results;
+ _results = [];
+ for (y = _i = 0; 0 <= n ? _i < n : _i > n; y = 0 <= n ? ++_i : --_i) {
+ _results.push((function() {
+ var _j, _results1;
+ _results1 = [];
+ for (x = _j = 0; 0 <= k ? _j < k : _j > k; x = 0 <= k ? ++_j : --_j) {
+ _results1.push(0);
+ }
+ return _results1;
+ })());
+ }
+ return _results;
+ })();
+ solution = (function() {
+ var _i, _ref, _results;
+ _results = [];
+ for (y = _i = 0, _ref = n - 1; 0 <= _ref ? _i < _ref : _i > _ref; y = 0 <= _ref ? ++_i : --_i) {
+ _results.push((function() {
+ var _j, _ref1, _results1;
+ _results1 = [];
+ for (x = _j = 0, _ref1 = k - 1; 0 <= _ref1 ? _j < _ref1 : _j > _ref1; x = 0 <= _ref1 ? ++_j : --_j) {
+ _results1.push(0);
+ }
+ return _results1;
+ })());
+ }
+ return _results;
+ })();
+ for (i = _i = 0; 0 <= n ? _i < n : _i > n; i = 0 <= n ? ++_i : --_i) {
+ table[i][0] = seq[i] + (i ? table[i - 1][0] : 0);
+ }
+ for (j = _j = 0; 0 <= k ? _j < k : _j > k; j = 0 <= k ? ++_j : --_j) {
+ table[0][j] = seq[0];
+ }
+ for (i = _k = 1; 1 <= n ? _k < n : _k > n; i = 1 <= n ? ++_k : --_k) {
+ for (j = _l = 1; 1 <= k ? _l < k : _l > k; j = 1 <= k ? ++_l : --_l) {
+ m = min((function() {
+ var _m, _results;
+ _results = [];
+ for (x = _m = 0; 0 <= i ? _m < i : _m > i; x = 0 <= i ? ++_m : --_m) {
+ _results.push([Math.max(table[x][j - 1], table[i][0] - table[x][0]), x]);
+ }
+ return _results;
+ })());
+ table[i][j] = m[0];
+ solution[i - 1][j - 1] = m[1];
+ }
+ }
+ n = n - 1;
+ k = k - 2;
+ ans = [];
+ while (k >= 0) {
+ ans = [
+ (function() {
+ var _m, _ref, _ref1, _results;
+ _results = [];
+ for (i = _m = _ref = solution[n - 1][k] + 1, _ref1 = n + 1; _ref <= _ref1 ? _m < _ref1 : _m > _ref1; i = _ref <= _ref1 ? ++_m : --_m) {
+ _results.push(seq[i]);
+ }
+ return _results;
+ })()
+ ].concat(ans);
+ n = solution[n - 1][k];
+ k = k - 1;
+ }
+ return [
+ (function() {
+ var _m, _ref, _results;
+ _results = [];
+ for (i = _m = 0, _ref = n + 1; 0 <= _ref ? _m < _ref : _m > _ref; i = 0 <= _ref ? ++_m : --_m) {
+ _results.push(seq[i]);
+ }
+ return _results;
+ })()
+ ].concat(ans);
+};
View
24 package.json
@@ -0,0 +1,24 @@
+{
+ "name": "linear-partition",
+ "description": "Linear partition algorithm",
+ "version": "0.0.1",
+ "main": "linear_partition.js",
+ "author": {
+ "name": "Jonathan Ong",
+ "email": "me@jongleberry.com",
+ "url": "http://jongleberry.com",
+ "twitter": "https://twitter.com/jongleberry"
+ },
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/jonathanong/linear-partition.git"
+ },
+ "bugs": {
+ "url": "https://github.com/jonathanong/linear-partition/issues",
+ "email": "me@jongleberry.com"
+ },
+ "scripts": {
+ "test": "make test"
+ },
+ "license": "MIT"
+}
View
11 test.js
@@ -0,0 +1,11 @@
+var assert = require('assert')
+
+var part = require('./linear_partition')
+
+// Not sure how to test equality
+assert.equal(
+ part([9,2,6,3,8,5,8,1,7,3,4], 3).toString(),
+ [[9,2,6,3],[8,5,8],[1,7,3,4]].toString()
+)
+
+process.exit()

0 comments on commit 25a90ca

Please sign in to comment.
Something went wrong with that request. Please try again.