My solution to an in-class assignment
Last active
August 29, 2015 14:06
-
-
Save lmullen/2a937ce981bd400ba62f to your computer and use it in GitHub Desktop.
Solution to an in-class assignment for Clio 3
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
</head> | |
<body> | |
<script src="00-bad-assign-groups.js"></script> | |
</body> | |
</html> |
This file contains 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
// This is a quick and dirty way to assign groups | |
var clio = ["Mandy", "Sara", "Anne", "George", "Peter", "Janelle", "Allison"]; | |
var shuffled = d3.shuffle(clio); | |
for (i = 0; i < shuffled.length; i += 2) { | |
console.log(shuffled[i] + " and " + shuffled[i+1]); | |
} |
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
</head> | |
<body> | |
<script src="01-naive-assign-groups.js"></script> | |
</body> | |
</html> |
This file contains 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
// Take an array and return groups | |
var assignGroups = function(participants) { | |
var shuffled = d3.shuffle(participants); | |
var groups = []; | |
do { | |
group = [shuffled.shift(), shuffled.shift()]; | |
groups.push(group); | |
} while(shuffled.length > 0); | |
return groups; | |
}; | |
var clio = ["Mandy", "Sara", "Anne", "George", "Peter", "Janelle", "Allison"]; | |
var groups = assignGroups(clio); | |
console.log("The value returned by the function:"); | |
console.log(groups); | |
console.log("Pretty printed:"); | |
printGroups(groups); | |
// A function to print our groups nicely | |
function printGroups(groups) { | |
for (var i = 0; i < groups.length; i++) { | |
var msg = "Group " + (i + 1) + ": " + groups[i].join(", "); | |
console.log(msg); | |
} | |
} | |
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
</head> | |
<body> | |
<script src="02-better-assign-groups.js"></script> | |
</body> | |
</html> |
This file contains 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
// Take an array and return groups of at least a specified size, but making | |
// sure everyone has someone to work with | |
var assignGroups = function(participants, groupSize) { | |
var shuffled = d3.shuffle(participants.slice()); | |
var groups = []; | |
while(shuffled.length > 2 * groupSize) { | |
var group = []; | |
for (var i = 0; i < groupSize; i++) { | |
group.push(shuffled.shift()); | |
} | |
groups.push(group); | |
} | |
groups.push(shuffled); | |
return(groups); | |
}; | |
var clio = ["Mandy", "Sara", "Anne", "George", "Peter", "Janelle", "Allison"]; | |
console.log("Groups of 2:"); | |
printGroups(assignGroups(clio, 2)); | |
console.log("\nGroups of 3:"); | |
printGroups(assignGroups(clio, 3)); | |
console.log("\nGroups of 4:"); | |
printGroups(assignGroups(clio, 4)); | |
// A function to print our groups nicely | |
function printGroups(groups) { | |
for (var i = 0; i < groups.length; i++) { | |
var msg = "Group " + (i + 1) + ": " + groups[i].join(", "); | |
console.log(msg); | |
} | |
} | |
This file contains 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
assign_groups <- function(participants, size = 2) { | |
n_groups <- floor(length(participants) / size) | |
group <- vector("list", n_groups) | |
for (i in 1:(n_groups - 1)) { | |
group[[i]] <- sample(participants, size) | |
participants <- participants[! participants %in% group[[i]]] | |
} | |
group[[n_groups]] <- participants | |
return(group) | |
} | |
clio <- c("Mandy", "Sara", "Anne", "George", "Peter", "Janelle", "Allison") | |
assign_groups(clio) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment