This file contains hidden or 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
//Calculates the standard deviation | |
let stddev(list:float list)= | |
sqrt (List.fold (fun acc elem -> acc + (float elem - List.average list) ** 2.0 ) 0.0 | |
list / float list.Length) | |
let trueRanks = [1.;2.;3.;4.;5.;5.;7.;8.;9.;10.] | |
let predictedRanks = [1.;2.;3.;4.;6.;7.;5.;8.;10.;9.] | |
let uBar = trueRanks |> List.average | |
let upBar = predictedRanks |> List.average |
This file contains hidden or 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
//Mean Absolute Error | |
let mae (ratings:float list)(predictions:float list) = | |
(List.zip ratings predictions |> List.sumBy (fun t -> abs (fst t - snd t))) | |
/float ratings.Length | |
//Normalized Mean Absolute Error | |
let nmae (ratings:float list)(predictions:float list) = | |
let rMax = ratings |> List.max | |
let rMin = ratings |> List.min | |
(mae ratings predictions )/(rMax - rMin) |
This file contains hidden or 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
let SimuDot (ratings :(float list)list) (a:int)(u:int)= | |
let num = List.zip ratings.[a] ratings.[u] | |
|> List.sumBy (fun item -> fst item * snd item) | |
let d1 = ratings.[a] |> List.sumBy (fun item -> item * item ) | |
let d2 = ratings.[u] |> List.sumBy (fun item -> item * item ) | |
if d1 = 0.0 || d2 = 0.0 then 0.0 else num / (sqrt d1 * sqrt d2) | |
let ratings = [[4.;0.;5.;5.];[4.;2.;1.;0.];[3.;0.;2.;4.];[4.;4.;0.;0.];[2.;1.;3.;5.]] | |
SimuDot ratings 0 1 |
This file contains hidden or 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
//Average rating for all other rated items by the user | |
//except the item "except" | |
let rBaru(u:float list)(except:int)= | |
let filtered = u |> List.mapi(fun i j -> if i <> except then j else 0.0) | |
|> List.filter(fun t -> t <> 0.0) | |
float ( List.sum filtered ) / float filtered.Length | |
//The following function finds the common item indices | |
let commonItemIndices (ratings:(float list)list)(a:int)(u:int)= |
This file contains hidden or 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
let x = [1.;2.;3.;4.;5.] | |
let y = [1.;2.;2.;3.;4.] | |
let z = [3;4] | |
let x_bar = List.average x | |
let y_bar = List.average y | |
let numerator = | |
List.zip x y | |
|> List.sumBy (fun item -> (fst item - x_bar)*(snd item - y_bar)) |
This file contains hidden or 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
//root mean squared error | |
let rmse(ratings:float list)(predictions:float list) = | |
sqrt(( List.zip ratings predictions |> List.map (fun t -> fst t - snd t) |> List.sum ) | |
/(float predictions.Length )) | |
rmse [1.3;4.5] [4.1;0.41] |> Dump |
This file contains hidden or 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
Dictionary<string,List<string>> graph = | |
new Dictionary<string,List<string>>(); | |
graph.Add("A",new List<string>(){"B","C","D"}); | |
graph.Add("B",new List<string>(){"A","E","F"}); | |
graph.Add("E",new List<string>(){"B","G"}); | |
List<string> visited = new List<string>(); | |
Queue<string> qu = new Queue<string>(); |
This file contains hidden or 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
void Main() | |
{ | |
BinarySearch(new int[]{1,3,4,5,9,10},1).Dump(); | |
} | |
int BinarySearch(int[] array,int x) | |
{ | |
int low = 0; | |
int high = array.Length - 1; | |
This file contains hidden or 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
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include <math.h> | |
int jolly( int nums[], int length) | |
{ | |
//Pre-conditions | |
if(length <= 0 ) | |
return 0; | |
int i; |
This file contains hidden or 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
string ops = "+-*/"; | |
string vals = "1234567890"; | |
Stack<char> opStack = new Stack<char>(); | |
Stack<int> valStack = new Stack<int>(); | |
void Main() | |
{ | |
string expr = "(1 + ((2+3)*(4*5)))"; | |
foreach (char c in expr) |