Skip to content

Instantly share code, notes, and snippets.

@pyrofolium
pyrofolium / fib.py
Last active November 17, 2017 01:58
def fib(n):
if n <= 1:
return n
mem = [0,1]
for i in xrange(2,n+1):
mem[(i+1)%2] = sum(mem)
return max(mem)
def fib(n):
if n <= 1:
import time
# Given the function:
def hello(name):
print "hello %s!" % name
# Implement logic such that the function cannot be called more than 3 times per second. If that rate is exceeded, raise an exception.
@pyrofolium
pyrofolium / interview.js
Last active October 11, 2017 02:22
questions used in the last interview.
function recursive_mult(x, y) {
if(y === 0 || x === 0) {
return 0;
}
return x + recursive_mult(x, y-1);
}
console.log(recursive_mult(3,2));
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def __init__(self):
self.result = []
from random import randrange
#returns your total money after you made a bet.
def bet(bet_func, money_pool, coin_func):
bet_amount = bet_func(money_pool)
return money_pool + bet_amount if coin_func() else money_pool - bet_amount
#runs a set of bets and determines whether or not your bets made it to goal(true means it did, false means it hit max_trials before making it to the goal or the money_pool dipped past zero.).
def gamble(bet_func, max_trials, goal, money_pool, bet_amount, coin_func):
for i in xrange(max_trials):
class Solution(object):
def maxSumSubmatrix(self, matrix, k):
"""
:type matrix: List[List[int]]
:type k: int
:rtype: int
"""
memory = [[None for col in row] for row in matrix]
result = float("-inf")
for i in xrange(len(matrix)):
class Node(object):
def __init__(self, value, left, right):
self.left = left
self.right = right
self.value = value
def swap_wrong_nodes(root):
def get_wrong_nodes(root, cap, bottom):
if root is None:
return []
class Solution(object):
def __init__(self):
self.memo = {}
def is_bounded(self,i,j,matrix):
return not(i < 0 or j < 0 or i >= len(matrix) or j >= len(matrix[0]))
def longestIncreasingPathStart2(self, matrix, start_i, start_j, travelled):
if str([start_i,start_j]) in self.memo:
#functional libraries
cons = lambda x, y : lambda a : x if a is True else y
car = lambda x : x(True)
cdr = lambda x : x(False)
pair = lambda x, y : cons(x, cons(y, None))
fst = car
snd = lambda x: car(cdr(x))
length = lambda x : 0 if isEmpty(x) else 1 + length(cdr(x))
isEmpty = lambda x : True if x is None else False
f_list = lambda *args : None if len(args) == 0 else cons(args[0], f_list(*args[1:]))
def longest_non_repeating_string(input_str):
repeated_chars = {}
longest_string = ""
current_string = ""
for char in input_str:
if char in repeated_chars:
current_string = ""
repeated_chars = {}
current_string += char