Skip to content

Instantly share code, notes, and snippets.

View MrNocTV's full-sized avatar

loctv MrNocTV

View GitHub Profile
@MrNocTV
MrNocTV / MergeSortFolkJoin.java
Created December 7, 2019 08:08
Merge Sort with ForkJoin framework
import java.util.concurrent.CountedCompleter;
public class MergeSortTask extends CountedCompleter<Void> {
private static final long serialVersionUID = -5183127767439978300L;
private Comparable[] data;
private int start, end;
private int middle;
public MergeSortTask(Comparable[] data, int start, int end, MergeSortTask parent) {
@MrNocTV
MrNocTV / MaxHeap.scala
Created March 11, 2019 08:37
Max Heap array-based implementation using Scala
import java.io._
import scala.io._
import scala.collection.mutable._
import scala.math._
class MaxHeap(val maxSize: Int) {
private val heap = Array.fill[Int](maxSize)(0)
var size:Int = 0
private def parent(i: Int): Int = i / 2
@MrNocTV
MrNocTV / reloadall.py
Created November 9, 2017 04:57
Script use reload to reload all submodules inside a module.
import types
from imp import reload
def status(module):
print('reloading ' + module.__name__)
def try_reload(module): #FAIL (sometimes in 3.3)
try:
reload(module)
except:
@MrNocTV
MrNocTV / LarryArray.py
Created July 11, 2017 06:12
Larry's Array - Check if an array can be sort by rotating 3 consecituve elements (all elements are unique)
'''
Given an array, all elements are unique, you can only perform rotation on 3 consecutive elements:
A B C
Example
A B C -> B C A -> C A B -> A B C
Given an array, find out if it is possible to sort it by only performing rotation.
Solution:
https://www.hackerrank.com/challenges/larrys-array/editorial
@MrNocTV
MrNocTV / FloydWarshallAlgorithm.py
Created July 11, 2017 03:10
shortest distance between all pairs of vertexes in a graph (floyd warshall algorithm)
from sys import stdin, stdout
INF = float('Inf')
def build_floyd_table(n, m):
# initalize with all values to INF
distance_table = [[INF]*n for i in range(n)]
# set the diagonal values to 0
for i in range(0, n):
distance_table[i][i] = 0
# all has been set, input the edges
@MrNocTV
MrNocTV / QuickSort.py
Created July 9, 2017 16:44
Quick Sort
def partition(arr, low, high):
# choose pivot as
pivot = arr[high]
i = -1
j = 0
# move all elements that are <= pivot to the left of it
# and bigger elements to the right
while j < high:
if arr[j] <= pivot:
i += 1
@MrNocTV
MrNocTV / MinimumRangeQuery.py
Last active July 9, 2017 07:57
Segmen tree - minimum range query
'''
Segmentree:
- construction: recursive like MergeSort, build leaves first then internal nodes
- Explanation: http://www.geeksforgeeks.org/segment-tree-set-1-range-minimum-query/
- Basically:
We need to construct a segmentree from an input array, then when query, there are three case than can be happened:
+ Case1: Range of current node is inside query range (full overlap)
+ Case2: Range of current node is completely outside query range (no overlap)
+ Case3: Range of current node is part of query range (partion overlap)
'''
@MrNocTV
MrNocTV / NextPowerof2.py
Created July 8, 2017 08:52
Next Power of 2
def is_power_of_2(n):
return n > 0 and (n & (n-1)) == 0
def next_power_of_2(n):
if is_power_of_2(n):
return n
count = 0
while n > 0:
count += 1
n = n >> 1
@MrNocTV
MrNocTV / Disjoinset.py
Created July 7, 2017 14:58
Disjointset implentations
# 1. using set (slow)
class Disjoint:
'''
Disjoinset class, support three main methods:
+ make_set(X): return a set for a vertexes v
+ find_set(X): find out which set X are in
+ union(a, b): union two set a,b and remove a,b
'''
def __init__(self):
# nested sets inside set
@MrNocTV
MrNocTV / PrimAlgorithm.py
Created July 7, 2017 13:52
Prim algorithm
'''
Tutorial:
https://www.youtube.com/watch?v=oP2-8ysT3QQ
'''
import bisect
class Graph:
def __init__(self, start, vertexes, edges):
self._vertexes = vertexes
self._start = start