Skip to content

Instantly share code, notes, and snippets.

@lmullen
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lmullen/2a937ce981bd400ba62f to your computer and use it in GitHub Desktop.
Save lmullen/2a937ce981bd400ba62f to your computer and use it in GitHub Desktop.
Solution to an in-class assignment for Clio 3
<!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 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]);
}
<!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>
// 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);
}
}
<!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>
// 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);
}
}
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)

My solution to an in-class assignment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment