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
isPalindrome :: Eq a => [a] -> Bool | |
isPalindrome list = result where | |
(_, result) = checkPal list (length list) | |
checkPal (_) 0 = ([], True) | |
checkPal (x:xs) 1 = (xs, True) | |
checkPal (x0:x1:xs) 2 = (xs, x0 == x1) | |
checkPal (x:xs) l = (rs, b && x == r) where (r:rs, b) = checkPal xs (l - 2) | |
main = do | |
print ( isPalindrome [1] ) |
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
def popMin(stack, aux) | |
# aux stack is reused to hold the sorted numbers and to inspect all numbers | |
aux_empty_len = aux.length | |
# Find the max. Need to inspect every entry. | |
max = stack.last | |
while !stack.empty? | |
v = stack.pop | |
aux.push v |
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
import Control.Monad.Writer | |
agcd a 0 = do | |
tell ["Result: " ++ show a] | |
return a | |
agcd a b = do | |
tell ["Recusive call: " ++ (show a) ++ " % " ++ (show b)] | |
agcd b (a `mod` b) |
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
λ> instance Num b => Num (a -> b) where f + g = \x -> f x + g x | |
<interactive>:2:10: Warning: | |
No explicit implementation for | |
‘*’, ‘abs’, ‘signum’, ‘fromInteger’, and (either ‘negate’ or ‘-’) | |
In the instance declaration for ‘Num (a -> b)’ | |
(0.04 secs, 9972760 bytes) | |
λ> :t (*2) + (*3) | |
(*2) + (*3) :: Num a => a -> a | |
λ> (*2) + (*3) $ 4 -- 4*2 + 4*3... | |
20 |
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
import Control.Monad.State | |
type Stack = [Int] | |
pop :: State Stack Int | |
pop = state $ \(x:xs) -> (x,xs) | |
push :: Int -> State Stack () | |
push a = state $ \xs -> ((),a:xs) |
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
// http://algs4.cs.princeton.edu/14analysis/ | |
// http://algs4.cs.princeton.edu/14analysis/DoublingRatio.java.html | |
public class DoublingRatio | |
{ | |
public static double timeTrial(int N) | |
public static void main(String[] args) | |
{ | |
double prev = timeTrial(125); | |
for (int N = 250; true; N += N) |
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
// http://stackoverflow.com/questions/52353/in-java-what-is-the-best-way-to-determine-the-size-of-an-object | |
import java.lang.instrument.Instrumentation; | |
public class ObjectSizeFetcher { | |
private static Instrumentation instrumentation; | |
public static void premain(String args, Instrumentation inst) { | |
instrumentation = inst; | |
} |
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
class UnionFind { | |
val int[] ids | |
var count = 0 | |
new(int size) { | |
count = size | |
ids = newIntArrayOfSize(size) | |
(0 ..< size).forEach[i|ids.set(i, 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 is an alternative to using a priority queue for the same purpose. | |
// http://algs4.cs.princeton.edu/24pq/ | |
// http://en.wikipedia.org/wiki/Selection_algorithm | |
public static Comparable select(Comparable[] a, int k) { | |
shuffle(a); // since this algo uses quicksort's partition, it is better to shuffle the arr to avoid qs worst case | |
int lo = 0, hi = a.length - 1; | |
while (hi > lo) { | |
int j = partition(a, lo, hi); // invoke quicksort's partition, we get all smaller items to the left. | |
if (j == k) return a[k]; // if the pivot lies on the kth position, we are done (also, all numbers to left are smaller) |
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
function findSolution(target) { | |
function find(number, history) { | |
if (number > target) { | |
return []; | |
} else if (number < target) { | |
return find(number * 3, "(" + history + " * 3)").concat( | |
find(number + 5, "(" + history + " + 5)")); | |
} else { // if (number == target) { | |
return [history]; | |
} |