Skip to content

Instantly share code, notes, and snippets.

@hpoit
hpoit / Iterative_factorial.jl
Last active March 10, 2018 00:14
Khan iterative factorial with BigInt
julia> function iterative_factorial(n)
result = big(1)
for i in range(1, n)
result *= i
end
result
end
iterative_factorial (generic function with 1 method)
julia> iterative_factorial(0)
@hpoit
hpoit / recursive_factorial.jl
Last active March 10, 2018 00:14
Khan recursive factorial with BigInt
julia> function recursive_factorial(n)
# first case (base)
if n == 0
return 1
end
# second case (recursive)
if n >= 1
return n*factorial(big(n - 1))
end
end
@hpoit
hpoit / palindrome.jl
Last active March 10, 2018 17:25
Khan palindrome (iterative)
julia> function ispalindrome(S::String)
i1 = 1
i2 = length(S)
while i2 > i1
if S[i1] != S[i2]
println(S[i1])
println(S[i2])
return false # `false` is not a variable, that's why `return` is necessary
end
i1 += 1
@hpoit
hpoit / exp_by_square.jl
Last active March 11, 2018 16:13
Khan recursive power (instead using exponentiation by squaring)
# Exponentiation by squaring is the standard method for modular exponentiation on big numbers in asymmetric cryptography
julia> function expbysquare(b::Int, e::Int)
result = BigInt(1)
bigb = BigInt(b)
while e > 0
if isodd(e)
result *= bigb
end
e >>= 1 # set e to itself shifted by one bit to the right, evaluate the new e after shift
@hpoit
hpoit / hanoitowers.jl
Last active March 13, 2018 13:54
Khan Hanoi Towers
julia> function hanoi(n, source, dest, spare)
if n == 1
println("Move disk 1 from rod $source to rod $dest")
else n >= 2
hanoi(n - 1, source, spare, dest)
println("Move disk $n from rod $source to rod $dest")
hanoi(n - 1, spare, dest, source)
end
end
hanoi (generic function with 1 method)
@hpoit
hpoit / quicksort.jl
Last active March 13, 2018 23:06
Khan quick sort
# https://rosettacode.org/wiki/Sorting_algorithms/Quicksort#Julia
julia> function quicksort_ex!(A, i=1, j=length(A))
if j > i
pivot = A[rand(i:j)] # random element of A
left, right = i, j
while left <= right
while A[left] < pivot # partition
left += 1
end
@hpoit
hpoit / insertionsort.jl
Last active March 15, 2018 00:39
Cormen problem 2.4c - insertion sort wrt number of inversions
julia> function insertionsort_ex(A::Array{T}) where T <: Number
for i in 1:length(A)-1
value = A[i+1]
j = i
while value < A[j] && 1 < j # each iteration eliminates one inversion
A[j+1] = A[j] # running time depends on number of inversions to eliminate, θ(n+d)
j -= 1 # where n = length(A)-1 and d = number of inversions
end
A[j+1] = value
end
@hpoit
hpoit / ang_assignment1a.jl
Last active May 7, 2018 22:08
Coursera ML week 2 - assignment 1a - parts 1-3a
# Linear regression
# part 1: basic function, A = eye(5), trivial
# part 2: plotting
julia> using CSV
julia> data = CSV.read("/Users/kevinliu/Downloads/machine-learning-ex1/ex1/ex1data1.txt", datarow=1)
97×2 DataFrames.DataFrame
│ Row │ Column1 │ Column2 │
@hpoit
hpoit / ang_assignment1b.jl
Last active May 17, 2018 02:12
Coursera ML week 2 - assignment 1b - parts 3b-4
# Linear regression
# part 3b: plotting linear fit
using Plots, CSV
data = CSV.read("/Users/kevinliu/Downloads/machine-learning-ex1/ex1/ex1data1.txt", datarow=1) # or data = rand(97,2)
m = length(data[:, 2])
X = hcat(ones(m, 1), data[:, 1]);
y = data[:, 2]
#thetasA = [-4.090664703327588; -2.386107508525324]; # incorrect when compared to scatter()
thetasB = [0.4559997241594341; 2.786203914586563];
@hpoit
hpoit / ang_assignment2-v01.jl
Last active May 24, 2018 13:49
Coursera ML week 3 - assignment 2 v0.1 - implementing Logit model on two datasets
# Visualize the data
using CSV, Plots; pyplot();
data = CSV.read("/Users/kevinliu/Documents/machine-learning-ex2/ex2/ex2data1.txt", datarow=1)
X = data[:, [1,2]]; y = data[:, 3];
pos = find(y); neg = find(iszero, y); # or neg = find(t -> t == 0, y);
scatter(xaxis=("exam 1 score", (30,100), 30:10:100))
scatter!(yaxis=("exam 2 score", (30,100), 30:10:100))
scatter!(X[pos, 1], X[pos, 2], markershape=:+, label="admitted")