Skip to content

Instantly share code, notes, and snippets.

@Shiggiddie
Shiggiddie / Udacity, Intro to Programming, Lesson 8: Optional Challenge
Created July 31, 2013 14:53
Classes written for Udacity Lesson 8, Optional Challenge.
//Manager Class
import java.util.ArrayList;
public class Manager
{
private ArrayList<Photographer> photographers;
private ArrayList<Assignment> assignments;
private Portfolio portfolio;
public Manager() {
// Sum of a Range
function range (start, finish, step) {
if (step === undefined) {
step = 1;
}
array = [];
var reverse = false;
step = Math.abs(step)
console.log(start, finish, step);
if (start > finish){
// Flattening
var arrays = [[1, 2, 3], [4, 5], [6]];
//arrays = arrays.concat([5000]);
arrays = arrays.reduce(function(prev, cur) { return prev.concat(cur); });
console.log(arrays);
// → [1, 2, 3, 4, 5, 6]
// A Vector Type
function Vector(x, y){
this.x = x;
this.y = y;
};
Vector.prototype.plus = function(other_vector) {
return new Vector(this.x + other_vector.x, this.y + other_vector.y);
};
// Artificial Stupidity
function SmartPlantEater() {
this.energy = 20;
this.last_reproduction = 0;
}
SmartPlantEater.prototype.act = function(view) {
var space = view.find(" ");
if (this.energy > 60 && space && this.last_reproduction > 60) {
this.last_reproduction = 0;
return {type: "reproduce", direction: space};
// Retry
function MultiplicatorUnitFailure() {}
MultiplicatorUnitFailure.prototype = Object.create(Error.prototype);
function primitiveMultiply(a, b) {
if (Math.random() < 0.5)
return a * b;
else
throw new MultiplicatorUnitFailure();
}
// Regex Golf
verify(/ca[rt]/,
["my car", "bad cats"],
["camper", "high art"]);
verify(/pr?op/,
["pop culture", "mad props"],
["plop"]);
// Month Names
var monthName = function() {
var names = ["January", "February", "March", "April", "May",
"June", "July", "August", "September", "October",
"November", "December"];
return {
name: function(number) { return names[number]; },
number: function(name) { return names.indexOf(name); }
};
}();
@Shiggiddie
Shiggiddie / tic-tac-toe elegant vs performant solutions
Last active August 29, 2015 14:08
The tic-tac-toe project presented itself with a problem of "checking for a win state", this gist contains an elegant non-performant, and a less-elegant performant solution.
/*
Background:
- The Grid object has an array of Vector objects accessible in the Grid's "vectors" attribute
- A Vector object has a "value" attribute that contains "O", "X", or null if not set
- The checkForWin method on the Grid returns true if and only if there is a win state in tic-tac-toe
* The win state is determined by looping through all the various winCombos, where winCombos is an array of arrays, whose inner arrays represent the 3 indexes of the Grid's "vectors" array that refer to row, collumn, or diagonal on a tic-tac-toe board which -if all containing the same Vector "value" value- constitutes a win.
*/
// Elegant, non-performant
@Shiggiddie
Shiggiddie / gist:6c0a70ec1eba06bde255
Created November 20, 2014 12:50
After writing tic-tac-toe, there were some subtle improvements possible, HW was assigned and these are the answers
//1) Fix computer algorithm (assigning to Anth)
// In Player's .makeMove method:
if (index == null) {
// No block? Try for center...
//console.log('computer is attempting to center');
if (grid.vectors[4].value == null) {
index = 4;
}
}