Skip to content

Instantly share code, notes, and snippets.

@games
Created July 9, 2015 01:55
Show Gist options
  • Select an option

  • Save games/6a8b0ff271dd65ca5a16 to your computer and use it in GitHub Desktop.

Select an option

Save games/6a8b0ff271dd65ca5a16 to your computer and use it in GitHub Desktop.
module mytest
open System
open NUnit.Framework
let seq (l: int list) =
let rec helper (acc: int list) (rest: int list) =
match acc, rest with
| [], a :: b -> helper [a] b
| _, [] -> acc
| a :: _, b :: c ->
if a <= b then helper (b :: acc) c
else helper acc c
helper [] l |> List.rev
let rec remove (l: int list) (i: int) =
match l, i with
| h :: t, 0 -> t
| [], _ -> []
| h :: t, _ -> h :: remove t (i - 1)
let max (l: int list) =
let rec helper (last: int list) (rest: int list) =
match rest with
| [] -> last
| _ ->
let curr = seq rest
let last' = if last.Length < curr.Length then curr else last
let rest' = remove rest (curr.Length - 1)
helper last' rest'
helper [] l
let longest (l: int list) =
let rec helper (acc: int list list) (rest: int list) =
match rest with
| [] -> acc
| _ :: t -> helper (max rest :: acc) t
helper [] l
|> List.sortBy (fun l -> - l.Length)
|> List.head
[<Test>]
let testseq () =
Assert.AreEqual (seq [1; 2; 10; 4], [1; 2; 10])
[<Test>]
let testremove () =
Assert.AreEqual (remove [1; 2; 10; 4] 2, [1; 2; 4])
Assert.AreEqual (remove [1; 2; 10; 4] 0, [2; 10; 4])
[<Test>]
let testmax () =
Assert.AreEqual (max [1; 2; 10; 4; 5; 6], [1; 2; 4; 5; 6])
Assert.AreEqual (max [1; 2; 10; 4], [1; 2; 10])
Assert.AreEqual (max [1; 2; 10; 11; 4], [1; 2; 10; 11])
[<Test>]
let testlongest () =
Assert.AreEqual (longest [5; 1; 2; 8; 6; 7], [1; 2; 6; 7])
Assert.AreEqual (longest [1; 2; 10; 4; 5], [1; 2; 4; 5])
Assert.AreEqual (longest [1; 2; 10; 13; 4; 5; 6], [1; 2; 4; 5; 6])
Assert.AreEqual (longest [1; 2; 3; 5; 6; 7; 4; 5; 6; 7; 8; 8; 8; 9; 10; 11; 12; 3; 4],
[1; 2; 3; 5; 6; 7; 7; 8; 8; 8; 9; 10; 11; 12])
Assert.AreEqual (longest [5; 4; 3; 1; 2; 3; 5; 6; 7; 4; 5; 6; 7; 8; 8; 8; 9; 10; 11; 12; 3; 4],
[1; 2; 3; 5; 6; 7; 7; 8; 8; 8; 9; 10; 11; 12])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment