Skip to content

Instantly share code, notes, and snippets.

import math
def binary_search(array, target, left=None, right=None):
# Recursively finds target within a sorted array using binary search.
# Returns position of target or -1 if not found.
if left is None:
left = 0
# If BinarySearch called without right value, set right to final index.
if right is None:
right = len(array) - 1
@sethschori
sethschori / tictactoe.html
Created February 26, 2017 18:35
A simple JavaScript Tic Tac Toe game
<html>
<body>
<script src="tictactoe.js"></script>
</body>
</html>
/*
FEDERAL AUDIT
Create a function that reads the dictionary of government spendings and returns a number representing the sum of total spending.
ex)
var spending = {
Military : {
Pensions : 1000000,
//Complete sumCart so that it will take the cart array and return the total cost for all
// the line items in the cart.
var cart = [
["tofu", {"quantity" : 3,"price" : 4.5} ],
["sriracha", {"quantity" : 1,"price" : 5} ],
["toilet paper", {"quantity" : 12,"price" : 1.75} ],
["Drano", {"quantity" : 1,"price" : 13} ],
["orichette", {"quantity" : 2,"price" : 7.5} ],
["hummus", {"quantity" : 2,"price" : 5.99} ],
/*
SPECIAL SNOWFLAKES
Create a function that takes 3 arrays of mixed elements and returns an array of only elements that are unique to the arrays
eg)
snowflake(['dog','cat','mouse'], [3, 'dog'], ['cat', 2, 1]) ==> [3,2,1,'mouse'];
*/
var myTestText = 'In pre-revolutionary France, the president of a Parlement evolved into a powerful magistrate, a member of the so-called noblesse de robe ("nobility of the gown"), with considerable judicial as well as administrative authority. The name referred to her primary role of presiding over trials and other hearings. In the 17th and 18th centuries, seats in the Parlements, including presidencies, became effectively hereditary, since the holder of the office could ensure that it would pass to an heir by paying the crown a special tax known as the paulette. The post of "first president" (premier président), however, could only be held by the King\'s nominees. The Parlements were abolished by the French Revolution. In modern France the chief judge of a court is known as its president (président de la cour).'
function buildMarkovChain(str) {
var cleanStr = str.toLowerCase().replace(/[,.?()"]/g,''); // remove punctuation
// var cleanStr = str.toLowerCase().replace(/[^a-zA-Z0-9]/g,' '); // this approach
// Attach a method to the Array prototype called myReduce.
// It should be utilized and perform exactly as Array.prototype.reduce does. Use the code snippet below as a guide.
Array.prototype.myReduce = function(callback, initVal) {
var startingIndex = 0;
var returnVal;
if (!initVal) {
startingIndex = 1;
returnVal = this[0];
} else {
// Attach a method to the Array prototype called myForEach.
// It should be utilized and perform exactly as Array.prototype.forEach does. Use the code snippet below as a guide.
Array.prototype.myForEach = function(callback) {
for (var i = 0; i < this.length; i++) {
callback(this[i],i,this);
}
}
var sum = 0;
// Attach a method to the Array prototype called myMap.
// It should be utilized and perform exactly as Array.prototype.map does. Use the code snippet below as a guide.
Array.prototype.myMap = function(callback) {
var returnArr = [];
for (var i = 0; i < this.length; i++) {
returnArr.push(callback(this[i],i,this));
}
return returnArr;
}
// Attach a method to the Array prototype called myFilter.
// It should be utilized and perform exactly as Array.prototype.filter does. Use the code snippet below as a guide.
Array.prototype.myFilter = function(func) {
var newArr = [];
for (var i = 0; i < this.length; i++) {
if(func(this[i],i,this)) {
newArr.push(this[i]);
}
}