Skip to content

Instantly share code, notes, and snippets.

View woodRock's full-sized avatar
🫖
Activate zen mode (CTRL + K, Z)

Jesse Wood woodRock

🫖
Activate zen mode (CTRL + K, Z)
View GitHub Profile
@woodRock
woodRock / pkg-get
Last active March 20, 2020 05:35
Creates a list of packages that I install for linux
#!/usr/bin/env bash
PKG_LIST_DIR=~/Downloads/package_list.txt
INSTALL="sudo apt-get"
REMOVE="sudo apt-get -r"
SEARCH="apt-cache search"
# Search for a package in the package manager
search(){
for PKG in "$@"
@woodRock
woodRock / daily-code-001.java
Last active March 21, 2019 08:27
Can take one or two steps to get to the top of the staircase write function num_ways(N)
import java.util.ArrayList;
class Staircase {
/**
* Can take one or two steps to get to the top of the staircase
* write function num_ways(N)
*/
public static int num_ways(int n){
if (n==0 || n==1)
return 1;
@woodRock
woodRock / daily-code-002.py
Last active March 21, 2019 08:27
Given an array of integers, return a new value such that each elemnent at index i of the new array is a product of all the numbers in the original array except the one at i
def arrayProduct(a=[],*args):
result = [0] * len(a)
product = 1
for i in range (0,len(a)):
product = product * a[i]
for j in range (0,len(a)):
result[j] = product / a[j]
return result
a = [1,2,3,4,5]
@woodRock
woodRock / daily-coding-problem-03.java
Last active December 9, 2023 21:11
Given a list of numbers and a number k, return whether any two numbers from the list add up to k. For example, given [10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17. Bonus: Can you do this in one pass?
import java.util.ArrayList;
import java.util.Arrays;
class ListAddition {
public static boolean list_add(int k, int[] list){
for (int i=0; i<list.length; i++){
for (int j=i+1; j<list.length; j++){
if (list[i]+list[j]==k)
return true;
}
@woodRock
woodRock / daily-coding-problem-04.java
Last active August 28, 2018 08:41
Given the root to a binary tree, implement serialize(root), which serializes the tree into a string, and deserialize(s), which deserializes the string back into the tree. For example, given the following Node class class Node: def __init__(self, val, left=None, right=None): self.val = val self.left = left self.right = right The following test sh…
import java.util.Scanner;
/*
Given the root to a binary tree, implement serialize(root), which serializes the tree into a string, and deserialize(s), which deserializes the string back into the tree.
For example, given the following Node class
class Node:
def __init__(self, val, left=None, right=None):
self.val = val
@woodRock
woodRock / daily-coding-problem-05.py
Last active November 16, 2018 04:45
cons(a, b) constructs a pair, and car(pair) and cdr(pair) returns the first and last element of that pair. For example, car(cons(3, 4)) returns 3, and cdr(cons(3, 4)) returns 4. Given this implementation of cons: def cons(a, b): def pair(f): return f(a, b) return pair Implement car and cdr
def cons(a, b):
return lambda f: f(a, b)
def car(f):
return f(lambda a, b: a)
def cdr(f):
return f(lambda a, b: b)
a, b = (int(n) for n in input().split(','))
@woodRock
woodRock / daily-coding-problem-09.java
Last active May 29, 2022 17:55
Given a list of integers, write a function that returns the largest sum of non-adjacent numbers. Numbers can be 0 or negative. For example, [2, 4, 6, 2, 5] should return 13, since we pick 2, 6, and 5. [5, 1, 1, 5] should return 10, since we pick 5 and 5. Follow-up: Can you do this in O(N) time and constant space?
class LargestSum {
public static int get_sum(int[] arr, int n){
int incl = arr[0]; // Max sum including the previous element
int excl = 0; // Max sum excluding the previous element
int excl_new;
int i;
for (i=0; i<n; i++){
/* current max excluding i */
excl_new = (incl>excl) ? incl:excl;
@woodRock
woodRock / daily-coding-problem-08.java
Last active August 28, 2018 08:39
A unival tree (which stands for "universal value") is a tree where all nodes under it have the same value. Given the root to a binary tree, count the number of unival subtrees. For example, the following tree has 5 unival subtrees: 0 / \ 1 0 / \ 1 0 / \ 1 1
/**
A unival tree (which stands for "universal value") is a tree where all nodes under it have the same value.
Given the root to a binary tree, count the number of unival subtrees.
For example, the following tree has 5 unival subtrees:
0
/ \
1 0
@woodRock
woodRock / daily-coding-problem-10.py
Last active August 28, 2018 08:38
Implement a job scheduler which takes in a function f and an integer n, and * calls f after n milliseconds.
import sched, time
s = sched.scheduler(time.time, time.sleep)
def print_time(): print ("From print_time", time.time())
def job_scheduler(f,n):
s.enter(n, 1, f, ())
s.run()
job_scheduler(print_time, 10)
@woodRock
woodRock / daily-coding-problem-24.rb
Last active August 28, 2018 08:37
Implement locking in a binary tree. A binary tree node can be locked or unlocked only if all of its descendants or ancestors are not locked. Design a binary tree node class with the following methods: is_locked, which returns whether the node is locked lock, which attempts to lock the node. If it cannot be locked, then it should return false. Ot…
class BinaryTreeLock
attr_accessor :locked, :left, :right
def initialize(left=nil, right=nil, unlocked)
@left = left
@right = right
@unlocked = unlocked
end
def lock