Created
October 27, 2019 17:30
-
-
Save ayal/bc8bb1eff415aa379322ba1869afb28d to your computer and use it in GitHub Desktop.
offer class preferences code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //var prefs = [[2,3],[1,3],[1,2],[5,6],[4,6],[4,5],[8,9],[7,9],[7,8],[11,12],[10,12],[10,11],[14,15],[13,15],[13,14],[17,18],[16,18],[16,17]]; | |
| var perfstr = `1 13 16 | |
| 2 13 18 | |
| 3 7 10 | |
| 4 11 16 | |
| 5 6 9 | |
| 6 13 5 | |
| 7 5 6 | |
| 8 -1 -1 | |
| 9 11 12 | |
| 10 11 12 | |
| 11 4 10 | |
| 12 18 17 | |
| 13 1 6 | |
| 14 5 9 | |
| 15 17 18 | |
| 16 1 13 | |
| 17 18 10 | |
| 18 15 17`; | |
| var prefs = perfstr.split('\n').map(x=>x.split('\t')).map(x=>[parseInt(x[1]),parseInt(x[2])]) | |
| var getAllSubsets = | |
| theArray => theArray.reduce( | |
| (subsets, value) => subsets.concat( | |
| subsets.map(set => [value,...set]) | |
| ), | |
| [[]] | |
| ); | |
| var all = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18]; | |
| var nines = getAllSubsets(all).filter(subset=>subset.length === 9) | |
| var groups = nines.map(subset=>{ | |
| return [subset, all.map(x => subset.indexOf(x) === -1 ? x : null).filter(x=>!!x)] | |
| }); | |
| var scores = groups.map(([arr1,arr2])=>{ | |
| return [{arr:arr1, score:arr1.map((x)=>{ | |
| var p1 = arr1.indexOf(prefs[x-1][0]) !== -1 ? 1 : 0; | |
| var p2 = arr1.indexOf(prefs[x-1][1]) !== -1 ? 1 : 0; | |
| return p1 + p2; | |
| }).reduce((a, b) => a + b, 0)}, | |
| {arr: arr2, score: arr2.map((x)=>{ | |
| var p1 = arr2.indexOf(prefs[x-1][0]) !== -1 ? 1 : 0; // pref 1 score, for now 1 | |
| var p2 = arr2.indexOf(prefs[x-1][1]) !== -1 ? 1 : 0; // pref 2 score, for now 2 | |
| return p1 + p2; | |
| }).reduce((a, b) => a + b, 0)}] | |
| }) | |
| scores.sort((a,b)=>((b[0].score + b[1].score) - (a[0].score + a[1].score) )) | |
| console.log(scores) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment