Skip to content

Instantly share code, notes, and snippets.

View jinwolf's full-sized avatar

Jin jinwolf

  • Chime
  • San Francisco
View GitHub Profile
@jinwolf
jinwolf / find_first_k.js
Last active November 26, 2015 15:41
A function that returns top K numbers from an integer array
/*
* It gets an array of top k maximum or minimum elements from the given integer array
* For example, findFirstK([2, 10, 1, 3, 4 ,6, 7], 3) returns [10, 7, 6], the top 3 maximum values from the array in the descending order
* or findFirstK([2, 10, 1, 3, 4, 6, 7], 3, true) return [1, 2, 3]
*/
function shift(arr, k, value, asc) {
if (k > arr.length - 1) {
return;
}
@jinwolf
jinwolf / Permute.java
Created December 5, 2015 06:14
Java String Permutation
public class Permute {
public static void main(String args[]) {
permute(args[0]);
}
public static void permute(String list) {
boolean[] used = new boolean[list.length()];
char[] in = list.toCharArray();
StringBuffer out = new StringBuffer();
@jinwolf
jinwolf / permute.js
Last active December 5, 2015 07:02
JavaScript permutation
// a + permute(b, c, d);
// a + b + permute(c, d);
// a + c + permute(b, d);
// a + d + permute(b, c)
function permute(str) {
var used = new Array(4);
var list = str.split('');
var out = [];
doPermute(used, list, 0, out);
# Enable tab completion
source ~/git-completion.bash
# colors!
green="\[\033[0;32m\]"
blue="\[\033[0;34m\]"
purple="\[\033[0;35m\]"
reset="\[\033[0m\]"
# Change command prompt
@jinwolf
jinwolf / factor_combo.js
Created December 19, 2015 01:11
Unique combination of factors
/*
PrintFactors(12)
12 * 1
6 * 2 // 2 * 6 ( 2 * 2 * 3)
4 * 3
3 * 2 * 2 ( 3 * 4)
PrintFactors(32)
32 * 1
16 * 2
@jinwolf
jinwolf / combo_string.js
Created December 19, 2015 08:28
Prints combinations of given string
var combo = (function() {
var limit = 0;
function doCombo(charList, buffer, level, start) {
if (level === limit) {
console.log(buffer.join(' * '));
return;
}
for (var i = start; i < charList.length; i++) {
@jinwolf
jinwolf / combo.js
Last active December 19, 2015 09:10
All possible unique combination of a given string
var combo = (function() {
var charList;
function doCombo(buffer, start) {
for (var i = start; i < charList.length; i++) {
buffer.push(charList[i]);
console.log(buffer.join(''));
doCombo(buffer, i + 1);
@jinwolf
jinwolf / binary_search_iterative.js
Created December 19, 2015 23:07
Search a number in an array using Binary Search iteratively (JavaScript)
var numList = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var NOT_FOUND = -1;
var NOT_SORTED = -2;
function search(list, num) {
var start = 0;
var end = list.length - 1;
while (true) {
if (start === end && list[start] !== num) {
@jinwolf
jinwolf / binary_search.js
Created December 19, 2015 23:34
Builds a binary tree from a sorted array and searches a value using the binary tree (JavaScript)
function Node(val) {
this.value = val;
this.left = null;
this.right = null;
}
var numList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
function buildTree(list, start, end) {
if (start > end) {
@jinwolf
jinwolf / merge_sorted_arrays.js
Last active December 31, 2015 06:34
Merge sorted arrays into one (JavaScript)
var sorted_lists = [
[3, 4, 6, 10, 11, 15],
[1, 5, 8, 12, 14, 19],
[2, 7, 9, 13, 16, 17, 18],
[20, 22, 23, 25]
];
function mergeSortedArrays(sortedLists) {
var pointers = new Array(sortedLists.length);