Skip to content

Instantly share code, notes, and snippets.

View railsstudent's full-sized avatar
🏠
Researching third-party library

Connie Leung railsstudent

🏠
Researching third-party library
View GitHub Profile
function DocumentParser(reader)
{
this.reader = reader;
this.reset();
}
DocumentParser.prototype.reset = function()
{
this.wordCount = 0;
this.charCount = 0;
function traverseTCPStates(eventList){
var state = "CLOSED"; // initial state, always
// Traversal code goes here
var tcpStates = {
'CLOSED' : [{ event: 'APP_PASSIVE_OPEN', new_state: 'LISTEN' }, { event: 'APP_ACTIVE_OPEN', new_state: 'SYN_SENT'}],
'LISTEN': [ { event: 'RCV_SYN', new_state: 'SYN_RCVD' },
{ event: 'APP_SEND', new_state: 'SYN_SENT' },
{ event: 'APP_CLOSE', new_state: 'CLOSED' } ],
'SYN_RCVD': [ { event: 'APP_CLOSE', new_state: 'FIN_WAIT_1' },
{ event: 'RCV_ACK', new_state: 'ESTABLISHED' } ],
function buildString(value, unit) {
if (value > 0) {
return value + ' ' + unit + (value > 1 ? 's' : '') + ', ';
}
return '';
}
function formatDuration (seconds) {
// Complete this function
if (seconds === 0) {
function decomposeHelper(sumSqr, lastRemovedNum) {
var root = Math.sqrt(sumSqr);
var rootFloor = Math.floor(root);
if (rootFloor === root) {
return [ root ];
}
// find the solution
for (var i = rootFloor; i >= 1; i--) {
function toChineseNumeral(num){
var numerals = {
"-":"负",
".":"点",
0:"零",
1:"一",
2:"二",
3:"三",
4:"四",
5:"五",
function NetworkClient (sendFunction, callback) {
this.sendFunction = sendFunction;
this.callback = callback;
this.msgIdAccumulator = [];
this.msgId = 0;
}
NetworkClient.prototype.send = function (data) {
// Could wrap data with extra information to send
this.msgId += 1;
function TicTacToe() {
// fill out the construction function
this.strategy = [5,1,3,7,9,2,4,6,8];
this.board = [ 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u' ]
this.hasWon = false;
this.player = '1'
this.play_as = ''
}
// http://www.cs.virginia.edu/kim/courses/cs3330/notes/ConvertingFP.pdf
// https://en.wikipedia.org/wiki/Single-precision_floating-point_format
var dec2Bin = function (wholeInt) {
let binaryInt = '';
while (wholeInt > 1) {
let remainder = wholeInt % 2;
binaryInt += remainder;
wholeInt = Math.floor(wholeInt / 2);
//https://en.wikipedia.org/wiki/Cache_replacement_policies#Least_Recently_Used_.28LRU.29
// http://stackoverflow.com/questions/17438595/js-non-enumerable-function
function LRUCache(capacity, init) {
this.nextTime = 0;
this.storage = [];
this.initialCapacity = capacity;
Object.defineProperty(this, 'nextTime', { enumerable: false });
Object.defineProperty(this, 'storage', { enumerable: false });
Object.defineProperty(this, 'initialCapacity', { enumerable: false });
function chainFunctions(fns) {
for (let fname in fns) {
let originalFn = fns[fname];
let fn = (...inputs) => {
if (this.result != null) {
inputs.splice(0, 0, this.result);
}
let cloneCF = new chainFunctions(fns);
cloneCF.result = originalFn.apply(this, inputs);
return cloneCF