├── .gitignore ├── LICENSE ├── bower.json ├── index.html ├── random.js ├── readme.md └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | *.iml 3 | out 4 | gen 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 饿了么前端 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "random", 3 | "main": "random.js", 4 | "homepage": "https://github.com/ElemeFE/random", 5 | "authors": [ 6 | "sofish " 7 | ], 8 | "description": "a better way to get carved up", 9 | "moduleType": [], 10 | "keywords": [ 11 | "random", 12 | "didive" 13 | ], 14 | "license": "MIT", 15 | "ignore": [ 16 | "**/.*", 17 | "node_modules", 18 | "bower_components", 19 | "test", 20 | "tests" 21 | ] 22 | } 23 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Random.js 6 | 7 | 8 | 19 | 20 | 21 | 22 |

1、分组参数

23 | 注:默认 N = 分成 N 组;如果选择区分性别,则 N = 每组多少人 24 | 25 | 26 | 27 |

2、编辑团队数据 / JSON

28 |

打开 index.html,编辑 JS 中 team 这个变量

29 | 30 |

3、计算结果

31 |
32 | 33 | 34 | 35 | 91 | 92 | -------------------------------------------------------------------------------- /random.js: -------------------------------------------------------------------------------- 1 | var random = function() { 2 | 3 | return function random(team, size, isSexSensitive, isForceSizeAsMemberCountInGroup) { 4 | return didive(split(team), size, isSexSensitive, isForceSizeAsMemberCountInGroup); 5 | }; 6 | 7 | function split(team) { 8 | return team.reduce(function(result, current) { 9 | result[current.sex].push(current); 10 | return result; 11 | }, { female: [], male: [], length: team.length }) 12 | } 13 | 14 | function didive(team, size, isSexSensitive, isForceSizeAsMemberCountInGroup) { 15 | var female = team.female; 16 | var male = team.male; 17 | 18 | // 当选择 sex sensitive 的时候,我们大多数情况希望以每组多少人来算 19 | // 比如分房,每组是 2 人,所以 size 在这里的设定不是「多少组」 20 | if(isSexSensitive) { 21 | return loop(female, Math.ceil(female.length / size)) 22 | .concat(loop(male, Math.ceil(male.length / size))); 23 | } 24 | 25 | // 可以强制按每组多少人来计算,而不区分性别 26 | if(isForceSizeAsMemberCountInGroup) size = Math.ceil(team.length / size); 27 | 28 | var females = loop(female, size); 29 | var males = loop(male, size).reverse(); 30 | 31 | if(females.length - males.length < 0) females = [males, males = females][0]; 32 | return females.map(function(group, i) { 33 | return group.concat(males[i] || []); 34 | }); 35 | } 36 | 37 | function loop(arr, size){ 38 | var i = 0; 39 | var result = []; 40 | 41 | while(arr.length) { 42 | var index = Math.floor(Math.random() * arr.length); 43 | if(!result[i]) result[i] = []; 44 | result[i].push(arr.splice(index, 1)[0]); 45 | if(++i === size) i = 0; 46 | } 47 | 48 | return result; 49 | } 50 | }(); 51 | 52 | // node 53 | try{ 54 | process.argv; 55 | module.exports = random; 56 | } catch(e) {}; 57 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # 一、简介 2 | 3 | 大多数情况,部门出去玩,是大家取在一起放松心情,还有一个很重要的点是 4 | :相互之间有更深入的了解。所以我们会分组,玩游戏,blahbla... 5 | 6 | 分组最重要的就是随机。作为一群程序员,当然不是靠某个人来分, 7 | 而是通过我们的管家 —— 程序。之前出去玩的时候写过一个随机分组 8 | 的程序,重新整理一下,发出来。我们面试的时候, 9 | 最经常了的一道题就是从里面分离出来的 —— 如何随机分组。 10 | 11 | # 二、程序 12 | 查看 [random.js](https://github.com/ElemeFE/random/blob/master/random.js) 的源文件,可直接运行于浏览器或者 Node 环境。 13 | 14 | # 三、测试 15 | 16 | 可以直接访问 [/random](http://elemefe.github.io/random) 来查看 DEMO,或者在 CLI 环境中运行: 17 | 18 | ```ruby 19 | $node test.js 20 | 21 | # Usage: 默认 size 为组的数量,可以强制为每组的人数 22 | # -分成多少组: node test 23 | # 区分性别,多少人为一组: node test true 24 | # 不区别性别,多少人为一组:node test false true 25 | ``` 26 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var random = require('./random'); 2 | 3 | var all = [ 4 | {name: 'A', sex: 'male'}, {name: 'B', sex: 'male'}, {name: 'C', sex: 'female'}, 5 | {name: 'D', sex: 'male'}, {name: 'E', sex: 'male'}, {name: 'F', sex: 'male'}, 6 | {name: 'G', sex: 'male'}, {name: 'H', sex: 'male'}, {name: 'I', sex: 'male'}, 7 | {name: 'J', sex: 'male'}, {name: 'K', sex: 'male'}, {name: 'L', sex: 'male'}, 8 | {name: 'M', sex: 'male'}, {name: 'N', sex: 'male'}, {name: 'O', sex: 'male'}, 9 | {name: 'P', sex: 'male'}, {name: 'Q', sex: 'female'}, {name: 'R', sex: 'male'}, 10 | {name: 'S', sex: 'male'}, {name: 'T', sex: 'female'}, {name: 'U', sex: 'male'}, 11 | {name: 'V', sex: 'male'}, {name: 'W', sex: 'female'}, {name: 'X', sex: 'male'}, 12 | {name: 'Y', sex: 'female'}, {name: 'Z', sex: 'male'} 13 | ]; 14 | 15 | var size = +process.argv[2] || 2; 16 | var isSexSensitive = process.argv[3]; 17 | var isForceSizeAsMemberCountInGroup = process.argv[4] 18 | 19 | console.log('- 结果:') 20 | console.log('-', random(all, size, isSexSensitive, isForceSizeAsMemberCountInGroup)); 21 | 22 | console.log(); 23 | console.log('- Usage: 默认 size 为组的数量,可以强制为每组的人数') 24 | console.log('- 分成多少组: node test '); 25 | console.log('- 区分性别,多少人为一组: node test true'); 26 | console.log('- 不区别性别,多少人为一组:node test false true'); 27 | --------------------------------------------------------------------------------