Created
February 24, 2022 12:28
-
-
Save ebresafegaga/cc5105be0b1dcf588a0dff7c644e86bf to your computer and use it in GitHub Desktop.
Valid Number leetcode problem at https://leetcode.com/problems/valid-number/
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 (>=>) p1 p2 input k = p1 input (fun input -> p2 input k) | |
let (>=>>) p1 p2 input = p1 input (fun input -> p2 input List.isEmpty) | |
let digit input k = | |
let rec loop input = | |
match input with | |
| [] -> k [] | |
| x :: input when Char.IsDigit x -> loop input | |
| input -> k input | |
match input with | |
| [] -> false | |
| x :: input when Char.IsDigit x -> loop input | |
| _ :: _ -> false | |
let char ch input k = | |
match input with | |
| x :: input when x = ch -> k input | |
| _ -> false | |
let dot = char '.' | |
let alt this that input k = this input k || that input k | |
let (<|>) = alt | |
let opt parser input k = parser input k || k input | |
let sign = char '+' <|> char '-' | |
let exponent = char 'e' <|> char 'E' >=> opt sign >=> digit | |
let numExp = digit >=> opt exponent | |
let integer = opt sign >=> digit >=>> opt exponent | |
let decimal = opt sign >=> opt digit >=> char '.' >=>> opt numExp | |
// https://leetcode.com/problems/valid-number/ | |
let isNumber (s : string) = | |
let s = s.ToCharArray () |> List.ofArray | |
integer s || decimal s | |
let numbers = ["2"; "0089"; "-0.1"; "+3.14"; "4."; "-.9"; "2e10"; "-90E3"; "3e+7"; "+6e-1"; "53.5e93"; "-123.456e789"] | |
List.forall isNumber numbers |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment