Skip to content

Instantly share code, notes, and snippets.

@ttowncompiled
ttowncompiled / MinHeap.py
Last active May 21, 2017 06:42
A MinHeap to use for HackerRank problems.
###
# Python 3 - MinHeap
# H = []
###
def size(H):
return len(H)
def is_empty(H):
return len(H) == 0
def peek(H):
return H[0] if len(H) > 0 else None
@ttowncompiled
ttowncompiled / MaxHeap.py
Last active May 21, 2017 06:42
A Max Heap to use for HackerRank problems.
###
# Python 3 - MinHeap
# H = []
###
def size(H):
return len(H)
def is_empty(H):
return len(H) == 0
def peek(H):
return H[0] if len(H) > 0 else None
@ttowncompiled
ttowncompiled / Queue.py
Last active May 21, 2017 06:50
A Queue implemented with 2 stacks to use for HackerRank problems.
###
# Python 3
# Q = [ [], [] ]
###
def size(Q):
return len(Q[0]) + len(Q[1])
def is_empty(Q):
return (len(Q[0]) + len(Q[1])) == 0
def peek(Q):
if len(Q[1]) == 0:
@ttowncompiled
ttowncompiled / Stack.py
Last active May 21, 2017 06:49
A Stack to use for HackerRank problems.
###
# Python 3
# S = []
###
def size(S):
return len(S)
def is_empty(S):
return len(S) == 0
def peek(S):
return S[-1] if len(S) > 0 else None
@ttowncompiled
ttowncompiled / BinarySearchTree.py
Last active May 21, 2017 07:38
A BinarySearchTree to use for HackerRank problems.
###
# Python 3
# T = [int, BinarySearchTree, BinarySearchTree, int] or None
###
def size(T):
return T[3] if not T is None else 0
def is_empty(T):
return T is None
def v(T):
return T[0]
@ttowncompiled
ttowncompiled / settings.json
Created May 26, 2017 19:30
VS Code user settings
// Overwrite settings by placing them into your settings file.
// See http://go.microsoft.com/fwlink/?LinkId=808995 for the most commonly used settings.
{
// Editor
// Controls the font family.
"editor.fontFamily": "Menlo, Monaco, 'Courier New', monospace",
// Controls the font weight.
@ttowncompiled
ttowncompiled / Primes.py
Last active June 14, 2018 13:54
Generates a complete list of primes P s.t. for all p in P, p < N. complexity: O(N**2).
# python 3.6.5
# from typing import List
def Primes( N: int ) -> List[int]:
""" Generates a complete list of primes P s.t. for all p in P, p < N. """
P: List[int] = []
A: List[int] = [ n+1 for n in range( N ) ]
for n in range( 1, N+1 ):
if A[n-1] == 1:
continue
P.append( A[n-1] )
@ttowncompiled
ttowncompiled / PrimesGen.py
Last active June 14, 2018 13:55
Generates the first N primes starting with 2. complexity: O(N**2).
# python 3.6.5
# from typing import List
def PrimesGen( N: int ) -> List[int]:
""" Generates the first N primes starting with 2. """
P: List[int] = []
i: int = 2
while len(P) < N:
is_prime: boolean = True
for p in P:
if i % p == 0:
@ttowncompiled
ttowncompiled / Prod.py
Last active June 14, 2018 14:58
Computes the product of the elements of A. complexity: O(n), where n is the number of elements in A.
# python 3.6.5
# from typing import List
def prod( A: List[int] ) -> int:
""" Computes the product of the elements of A. """
if A == None or len(A) == 0:
return 0
z: int = 1
for a in A:
z *= a
return z
@ttowncompiled
ttowncompiled / hackerrank.rb
Last active September 15, 2018 13:10
Adds a set of methods to classes of Ruby to make them more applicable to algorithmic and mathematical challenges.
class Array
def prod
if self.length == 0
return 0
end
z = 1
self.each do |a_i|
z *= a_i
end
return z