This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Use Gists to store code you would like to remember later on | |
console.log(window); // log the "window" object to the console |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Topological sorting - Example of library dependencies | |
//Imagine you are given library dependencies in this form in a FILE: | |
// 4 libraries, 5 dependencies | |
// 0 -> 1 | |
// 1 -> 2 | |
// 2 -> 3 | |
// 2 -> 4 | |
// 3 -> | |
// 4 -> | |
// Meaning library 0 depends on lib 1...lib 3 depends on nothing etc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
You have a function rand7() that generates a random integer from 1 to 7. | |
Use it to write a function rand5() that generates a random integer from 1 to 5. | |
rand7() returns each integer with equal probability. | |
rand5() must also return each integer with equal probability. | |
*/ | |
function rand5(){ | |
var x = rand7(); | |
while(x > 5){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*You have an array of integers, and for each index you want to find the product | |
of every integer except the integer at that index. | |
Write a function get_products_of_all_ints_except_at_index() that takes an array | |
of integers and returns an array of the products.*/ | |
public static int[] get_products_of_all_ints_except_at_index_dp(int[] arr){ | |
//build front | |
int[] front = new int[arr.length]; | |
front[0] = 1; | |
for(int i = 1; i < arr.length; i++){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//return -1: correct parentheses | |
//return n: Position of first offending bracket | |
public static int parantheses(char[] chrs){ | |
Stack s = new Stack(); | |
int i = 0; | |
for(;i<chrs.length;i++){ //Checks for erroneous chars omitted | |
char c = chrs[i]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//This method works only with integers | |
int a = 5; | |
int b = 7; | |
a = a-b; //diff. a = -2 | |
b = a+b; // b = 5 | |
a = b-a; // a = 7 | |
//This method relies on bit manipulation and does not depend on the number format | |
int c = 3; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Decide whether a given string or ANY | |
* of its permutations is a Palindrome. | |
* | |
* The given solution works in O(n). Building the parity map | |
* takes n steps, iterating through the map takes | |
* not more than n steps (size of the alphabet). | |
* Space consumption: Map with the size of the alphabet. (constant) | |
* @param str | |
* @return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Find the greatest number in an array with a multi-threaded fork/join approach. | |
* | |
* This is a simple recursive divide&conquer approach to search through a list | |
* and return the max-element. It is using a subclass of java.util.concurrent.ForkJoinTask | |
* (namely RecursiveTask<V>). | |
* | |
* Theoretical aspect: Amdahl's Law. | |
* Possible speedup with n processors: S(n) = 1 / (B + 1-B*(1/n)) | |
* where B is the fractal of the programm which can only be |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RomanConversion { | |
public static void main(String[] args){ | |
System.out.println(toRoman(1904)); | |
} | |
public static String toRoman(int x){ | |
StringBuilder sb = new StringBuilder(); | |
String number = x + ""; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//We can do this with only one loop | |
//thanks to the fact that we are looking for | |
//EXACTLY 2 movies which match the length. | |
//Duplicates are not being counted | |
//as we check for a hit FIRST and then put the | |
//value of the first movie into the map. | |
public static boolean isTwoMovies(int fl, int[] ml){ | |
HashMap<Integer, Boolean> hm = new HashMap<Integer, Boolean >(); |
OlderNewer