Skip to content

Instantly share code, notes, and snippets.

View linstantnoodles's full-sized avatar
🎯
Focusing

Alan L. linstantnoodles

🎯
Focusing
View GitHub Profile
@linstantnoodles
linstantnoodles / euclids
Created August 15, 2012 03:13
euclids algo
function eu(a, b){
if(a % b == 0){
return b;
}else{
return eu(b, a % b);
}
}
alert(eu(30,12));
var hash_table = {};
function check_dup(name){
for(i = 0; i < name.length; i++){
if(name[i] in hash_table){
return false;
}else{
hash_table[name[i]] = true;
}
}
@linstantnoodles
linstantnoodles / p67.ml
Created September 20, 2012 00:36
ocaml p6
(* Write a function val zip : 'a list * 'b list -> 'a * 'b list which accepts a
* pair of lists of the same length and returns a list of pairs. For example,
* zip([1; 2; 3], ['A'; 'B'; 'C']) should return the list of pairs [(1, 'A'); (2,
* 'B'); (3, 'C')]. You may assume that the 2 input lists are of the same length.
* (2 Points)*)
let rec zip (a,b) =
match (a,b) with
([],[]) -> []
| ([],n::ns)-> []
@linstantnoodles
linstantnoodles / mapreduce.ml
Created September 23, 2012 19:19
map reduce ocaml
(* The map function *)
let rec map fn list =
match list with []->[]
| n::ns -> fn n::map fn ns;;
(* The reduce function *)
let rec reduce fn id list =
match list with []->id
| n::ns->fn n (reduce fn id ns);;
@linstantnoodles
linstantnoodles / sol7.ml
Created September 25, 2012 15:47
Solution to EC7
let rec unZip list =
match list with
[] -> ([], [])
| (x, y)::rest ->
let (xs, ys) = unZip rest in
(x::xs, y::ys);;
@linstantnoodles
linstantnoodles / KeyPadCombinations.java
Created September 28, 2012 02:16
cs1 combinations
/*
* Question: Generate all the possible combinations of the letters on the telephone keypads. Recursively.
* Basically, the product of N sets.
* e.g given 2,3. Valid combinations include (A,D),(A,E),(B,D)...
*/
public class KeyPadCombinations {
/*
* Parameters
* numbers = the key pad #s index = the position of a letter for
* each number level = the position of the current number
@linstantnoodles
linstantnoodles / transitive.ml
Created October 12, 2012 01:20
checks a relation for transitive property
//checks for existence of (b,c)
let rec exists_bc(b,r) =
match r with
[] -> false
| (x,c)::tail -> (x == b) || exists_bc(b,tail);;
//constructs the list of relations that must exist for element a
let rec create_ac(a,b,r) =
match r with
[] -> []
@linstantnoodles
linstantnoodles / transitive.ml
Created October 12, 2012 01:20
checks a relation for transitive property
(*checks for existence of (b,c)*)
let rec exists_bc(b,r) =
match r with
[] -> false
| (x,c)::tail -> (x == b) || exists_bc(b,tail);;
(*constructs the list of relations that must exist for element a*)
let rec create_ac(a,b,r) =
match r with
[] -> []
@linstantnoodles
linstantnoodles / sublists.ml
Created December 20, 2012 19:51
generate all sublists of a list
(*let A = [1;2;3]
* genSublists A will yield
* [[1]; [1; 2]; [1; 3]; [1; 2; 3];[2]; [2; 3]; [3]; []] *)
let rec gen list head =
match list with
[] -> []
| first::rest -> let start = (head @ [first]) in
let rec append list =
match list with
let rec sublists a = match a with
[] -> [[]]
|first::rest -> let b = sublists rest in
let c = List.map (fun g -> first::g) b in
c @ b;;
(*
val sublists : 'a list -> 'a list list = <fun>
# let a = [1;2;3];;
val a : int list = [1; 2; 3]