Skip to content

Instantly share code, notes, and snippets.

@say2joe
Created October 18, 2014 07:30
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 say2joe/4deff974f6efa8999a65 to your computer and use it in GitHub Desktop.
Save say2joe/4deff974f6efa8999a65 to your computer and use it in GitHub Desktop.
Sorting and intersection
MS = Sharecize = {
d3: d3,
DOM: {
$view: $('#MS-container'),
$content: $('section#content'),
$console: $('aside#console > div')
},
Writer: function (DOM) {
if (!DOM) DOM = Sharecize.DOM.$content;
if (!DOM.jquery) DOM = $(DOM);
return function(html) {
DOM.append($('<p/>').html(html));
}
},
console: {
log: function (msg) {
var console = Sharecize.DOM.$console.show();
console.append($('<p/>').text(msg));
}
}
};
Sharecize.Exercises = [
{
problem: '1. Given a list of names, write a Javascript (without using Javascript sort) program to sort them alphabetically.',
input: 'Pok, Ocie, Justine, Marta, Adelle, Gisele, Karin, Ginny, Stormy, Riva, Dawna, Marcelino, Starr, Cleveland, Cyril, Brendan, Myrl, Porfirio, Salena, Kristel'.split(', '),
solution: function() {
var input = this.input;
var bi, bucketSort = [];
input.forEach(function(v, i, a) {
bi = v.toUpperCase().charCodeAt(0);
if (!bucketSort[bi]) {
bucketSort[bi] = [v];
} else {
bucketSort[bi].push(v);
bucketSort[bi].forEach(
function innerSort(v, i, a) {
var index = 1, next = i + 1;
if (next === a.length) return;
for (var l = v.length; index < l; index++) {
if (v.charCodeAt(index) !== a[next].charCodeAt(index)) {
if (v.charCodeAt(index) > a[next].charCodeAt(index)) {
a[i] = a[next]; a[next] = v;
a.forEach(innerSort);
}
break;
}
}
}
);
}
});
input = [];
bucketSort.forEach(function(v, i, a) {
if (v && v.length) {
v.forEach(function(name) {
input[input.length] = name;
});
}
});
bucketSort = null;
return input;
}
},
{
problem: '2. MOST IMPORTANT: Given a list of number sets, write a Javascript program to determine the intersection of sets and output the results. Extra points for using D3.js to visualize the results.',
input: [
'14, 9, 1, 6, 16'.split(', '),
'4, 15, 16, 14, 11'.split(', '),
'16, 10, 2, 3, 8'.split(', '),
'3, 17, 16, 6, 14'.split(', '),
'19, 18, 14, 6, 20'.split(', '),
'6, 15, 8, 7, 2'.split(', '),
'15, 14, 2, 19, 3'.split(', '),
'8, 2, 14, 10, 5'.split(', '),
'11, 6, 8, 10, 18'.split(', '),
'14, 10, 12, 4, 18'.split(', ')
],
findNumber: function (val, i, a) {
return parseInt(val,10) === parseInt(this, 10);
},
x3d: {
scene: {},
element: {},
note: "Most of this functionality was inspired by http://bl.ocks.org/ZJONSSON/1291667",
plotAxes: function (axes) {
var scale, ticks,
scene = this.scene,
numTicks = parseInt('10'),
size = parseFloat('0.01');
for (var axis in axes) {
// the axis line
axis = axes[axis];
console.log('Axis',axis);
scale = d3.scale.linear().domain(axis.domain).range(axis.range);
scene.append("x3d:transform")
.attr("translation", axis.locus.replace("D", (scale.range()[0] + scale.range()[1]) / 2))
.append("x3d:shape").append("x3d:box")
.attr("size", axis.locus.replace(/0/g, size).replace("D",scale.range()[1]));
// ticks along the axis
ticks = scene.selectAll("abcd").data(scale.ticks(numTicks)).enter().append("x3d:transform")
.attr("translation", function(d){ return axis.locus.replace("D",scale(d)); });
ticks.append("x3d:shape").append("x3d:box").attr("size",size*3+" "+size*3+" "+size*3);
ticks.append("x3d:billboard").append("x3d:shape").append("x3d:text")
.attr("string",scale.tickFormat(10)).attr("solid","true")
.append("x3d:fontstyle").attr("size",0.67).attr("justify","middle");
console.log('scale',scale,'ticks',ticks);
}
},
plotData: function (points) {
/* points = points.data(d3.range(50).map(function() {
return {
x: Math.random()*100, y: Math.random()*100, z: Math.random()*100
};
}));
points.exit().remove(); // Remove any excess datapoints, if needed
points.enter() // Draw a box for each new datapoint
.append("x3d:transform").attr("class","datapoints")
.append("x3d:shape").attr("diffuseColor", function() {
return Math.random()+" "+Math.random()+" "+Math.random();
})
.append("x3d:box").attr("size","0.2 0.2 0.2");
points.transition().duration(2000) // Move each box to the right point location
.attr("translation", function(d) { return x(d.x)+" "+y(d.y)+" "+z(d.z)}); */
}
},
drawGraph: function (element) {
var x3d = this.x3d,
data = this.intersections,
visual = d3.select(element);
d3.ns.prefix.x3da = "http://www.web3d.org/specifications/x3d-namespace";
var range = [0, 10], domain = [0, 100];
x3d.element = visual.append("x3d:x3d");
x3d.scene = x3d.element.append("x3d:scene");
var data = x3d.scene.selectAll(".datapoints");
x3d.plotAxes({
x: { locus: "D 0 0", domain: domain, range: range },
y: { locus: "0 D 0", domain: domain, range: range },
z: { locus: "0 0 D", domain: domain, range: range }
});
console.log('plotData starting ...');
visual.style('display','block');
x3d.plotData(data);
console.log(visual,'plotData finished ...');
},
solution: function () {
var exercise = this,
setIndices = arguments,
intersects = exercise.intersections,
sects = [], find = exercise.findNumber,
input = setIndices ? [] : exercise.input;
if (setIndices.length) {
setIndices = [].slice.apply(setIndices);
setIndices.forEach(function(v, i, a) {
input.push(this.input[parseInt(v,10)]);
}, exercise);
}
input.forEach(function(arraySet, i, a) {
var nextSet, tmp = [],
tmpLast, lastSects;
if (++i >= a.length) return;
nextSet = a[i];
arraySet.forEach(function(item, i, a) {
exercise.max = Math.max(exercise.max, item);
if (nextSet.some(find, item)) {
tmp.push(item);
}
});
if (i > 1) {
tmpLast = [];
lastSects = sects[sects.length-1];
lastSects.forEach(function(item, i, a) {
if (tmp.some(find, item)) {
tmpLast.push(item);
}
});
if (tmpLast.length) {
sects[i-1] = tmpLast;
}
} else {
sects[i] = tmp;
}
tmpLast = tmp = null;
});
intersects[setIndices.join('-')] = sects.shift();
return setIndices +": "+ sects;
},
intersections: {},
max: 0
}
];
var writer = new Sharecize.Writer();
Sharecize.Exercises.forEach(function(exercise, i) {
writer(exercise.problem);
if (i === 1) {
writer(exercise.solution(1,2));
writer(exercise.solution(1,2,3));
writer(exercise.solution(1,2,4));
writer(exercise.solution(3,0,4));
writer(exercise.solution(5,8));
writer(exercise.solution(4,9));
//exercise.drawGraph('#visual');
console.log(
'Intersections:', exercise.intersections
);
} else {
writer(JSON.stringify(exercise.solution()));
}
this.console.log(
"Completed exercise "+ exercise.problem.charAt(0) + "!"
);
}, Sharecize);
@say2joe
Copy link
Author

say2joe commented Dec 6, 2014

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