Skip to content

Instantly share code, notes, and snippets.

@jmsevold
jmsevold / reverse.js
Created March 24, 2017 03:14
reverse words in a sentence
let sentence = "Hello World";
function replaceChar(str,targetIndex,newChar){
return str.substr(0,targetIndex) + newChar + str.substr(targetIndex + 1)
}
function stringReverser(str, startIndex, endIndex) {
if (!str || str.length < 2) {
return;
}
@jmsevold
jmsevold / set-covering-problem.py
Created December 29, 2016 04:16
Set covering problem using easy visuals and friendly variable names
colors_needed = set(["blue", "yellow", "green", "orange", "purple", "white", "black", "pink"])
boxes = {}
boxes["box_1"] = set(["orange", "purple", "white"])
boxes["box_2"] = set(["yellow", "orange", "blue"])
boxes["box_3"] = set(["green", "purple", "black"])
boxes["box_4"] = set(["purple", "white"])
boxes["box_5"] = set(["black", "pink"])
final_boxes = set()
/*
Represent the following graph as an adjacency list.
write a dfs function that takes the adjacency list, a start node, and a target node. return true if it's in the graph, and false if it is not
A
/ \
F B
/ / \
G D C
/ | \ |
/*
1.Create an adjacency list to represent a graph of alphabetical letters as follows:
A
B C
D E H
F G
2. Write a function that takes a graph, a node (a letter) as a starting point, and lastly a node to search for. Return true if exists, and false if it does not
@jmsevold
jmsevold / merge.js
Last active September 9, 2016 19:47
// write a function that merges two unsorted arrays
function merge(leftArr,rightArr){
let merged = [];
let i = 0;
let j = 0;
// while we are still inbounds for both arrays
while(i < leftArr.length && j < rightArr.length){
if(leftArr[i] < rightArr[j]){
merged.push(leftArr[i]);
# helpful logging statements
def binary_search(list,target):
lower_limit = 0;
upper_limit = len(list) - 1
while(lower_limit <= upper_limit):
midpoint = (lower_limit + upper_limit) // 2
midpoint_guess = list[midpoint]
print("Guess:", midpoint_guess,"\n")
class TweetBox extends React.Component {
constructor(){
super()
this.state = {
text: "",
photoAdded: false
};
}
render() {
class ContainerComponent extends React.Component{
// excluding everything else
handleButtonClick(){
// stuff
}
render(){
<PresentationalComponent onButtonClick={this.handleButtonCLick.bind(this)}
}
/*
If you know the four steps of delegate creation and usage, you know 90% of the steps
involved to create and call a multicast delegate. As a refresher, the four steps are:
1. Declare a delegate.
2. Instantiate the delegate.
3. Store an appropriate method in the delegate instance (anonymous method, named method, or lambda)
4. Call the delegate, passing the appropriate arguments.
A multi-cast delegate begins at step 2 above, and continues to steps 3 and 4 with two differences:
- Rather than storing a single method, you store a delegate.
var numCount = (list) => {
return list.reduce((acc,current) =>{
var numKey = current.toString();
acc[numKey] === undefined ? acc[numKey] = 1 : acc[numKey] += 1;
return acc;
},{});
};