-
-
Save paulosuzart/4547930ad007ce911741816fb510b8e1 to your computer and use it in GitHub Desktop.
Dream server to find repeated chards
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
module CharSet = Set.Make(Char) | |
(* Adds a char to the Set and returns the new instance and a | |
bool indicating if the insertion suceeded *) | |
let is_added achar target = | |
let prev_cardinal = CharSet.cardinal target in | |
let added = CharSet.add achar target in | |
prev_cardinal < CharSet.cardinal added, added;; | |
(* Finds the first repeated character in a word *) | |
let solve word = | |
let rec solve' word' set = match word'() with | |
Seq.Cons (c, xs) -> | |
(match is_added c set with | |
(true, s) -> solve' xs s | |
| _ -> Some c) | |
| _ -> None in | |
solve' (String.to_seq word) CharSet.empty;; | |
let res w = match solve w with | |
Some c -> Printf.sprintf "Found %c" c | |
| None -> "No repetition found";; | |
let () = | |
Dream.run | |
@@ Dream.logger | |
@@ Dream.router [ | |
Dream.get "/find_repeated" (fun req -> match Dream.query "word" req with | |
| None -> Dream.empty `Bad_Request | |
| Some word -> Dream.html (res word)) | |
] | |
@@ Dream.not_found |
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
module CharSet = Set.Make(Char) | |
(* Adds a char to the Set and returns the new instance and a | |
bool indicating if the insertion suceeded *) | |
let add achar target = | |
let added = CharSet.add achar target in | |
target <> added, added | |
let add_ht c ht = | |
let lbefore = Hashtbl.length ht in | |
Hashtbl.replace ht c c; | |
lbefore < Hashtbl.length ht, ht | |
let rec solve' word' container add_fn = match word'() with | |
Seq.Cons (c, xs) -> | |
(match add_fn c container with | |
(true, s) -> solve' xs s add_fn | |
| _ -> Some c) | |
| _ -> None | |
(* Finds the first repeated character in a word usingn a set *) | |
let solve word = | |
Dream.info (fun log -> log "Solving [%s] using set" word); | |
solve' (String.to_seq word) CharSet.empty add | |
(* Finds the first repeated character in a word using a hash table *) | |
let solve_ht word = | |
Dream.info (fun log -> log "Solving [%s] using hash table" word); | |
let size = String.length word and seq = String.to_seq word in | |
solve' seq (Hashtbl.create size) add_ht | |
(* Finds the repetition using Set or Hashtable *) | |
let res w s = | |
let res' = match s with | |
Some "set" -> solve w | |
| _ -> solve_ht w in | |
match res' with | |
Some c -> Printf.sprintf "Found %c" c | |
| None -> "No repetition found" | |
let () = | |
Dream.run | |
@@ Dream.logger | |
@@ Dream.router [ | |
Dream.get "/find_repeated" (fun req -> match Dream.query "word" req with | |
| None -> Dream.empty `Bad_Request | |
| Some word -> Dream.html (res word (Dream.query "s" req))) | |
] | |
@@ Dream.not_found |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment