Permalink
Browse files

componentize

  • Loading branch information...
1 parent 371ed53 commit 25a90ca1f85e78c242469899923cae9ece8c660d @jonathanong jonathanong committed Aug 14, 2013
Showing with 249 additions and 8 deletions.
  1. +59 −0 .gitignore
  2. +7 −0 Makefile
  3. +13 −0 README.md
  4. +11 −0 component.json
  5. +17 −8 linear_partition.coffee
  6. +107 −0 linear_partition.js
  7. +24 −0 package.json
  8. +11 −0 test.js
View
@@ -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
@@ -0,0 +1,7 @@
+build:
+ @coffee --compile --bare linear_partition.coffee
+
+test:
+ @node test
+
+.PHONY: build test
View
@@ -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
@@ -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
@@ -2,31 +2,40 @@
# 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 = []
while k >= 0
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

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -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.