[a / b / c / d / e / f / g / gif / h / hr / k / m / o / p / r / s / t / u / v / vg / vm / vmg / vr / vrpg / vst / w / wg] [i / ic] [r9k / s4s / vip] [cm / hm / lgbt / y] [3 / aco / adv / an / bant / biz / cgl / ck / co / diy / fa / fit / gd / hc / his / int / jp / lit / mlp / mu / n / news / out / po / pol / pw / qst / sci / soc / sp / tg / toy / trv / tv / vp / vt / wsg / wsr / x / xs] [Settings] [Search] [Mobile] [Home]
Board
Settings Mobile Home
/g/ - Technology

Name
Options
Comment
Verification
4chan Pass users can bypass this verification. [Learn More] [Login]
File
  • Please read the Rules and FAQ before posting.
  • You may highlight syntax and preserve whitespace by using [code] tags.

08/21/20New boards added: /vrpg/, /vmg/, /vst/ and /vm/
05/04/17New trial board added: /bant/ - International/Random
10/04/16New board for 4chan Pass users: /vip/ - Very Important Posts
[Hide] [Show All]


[Advertise on 4chan]


File: valid number.jpg (418 KB, 1437x2006)
418 KB
418 KB JPG
>too many edge cases
>mental breakdown and brain stops functioning in the interview
wat do?
>>
>>106508374
Should be pretty easy with regex. I'll cook something up.
>>
>>106508592
Here
^[+\-]?(\d+|\.\d+|\d+\.|\d+\.\d+)([eE]\d+)?$
>>
>>106508374
if they ask you leetcode hards they probably don't wanna hire you
focus on improving your personality
>>
>>106508660
https://regex101.com/r/zND6BN/1
You're missing the sign check after [eE]

>>106508374
literally took me less than 10 minutes:
bool isNumber(char* s) {
bool has_floatp = false;
bool has_intp = false;
bool valid = true;

if (*s == '+' || *s == '-') ++s;
if (!*s) return false;
while (*s >= '0' && *s <= '9' && (has_intp = true)) ++s;
if (*s == 'e' || *s == 'E')
{
if (!valid) return false;
++s;
if (*s == '+' || *s == '-') ++s;
valid = false;
while (*s >= '0' && *s <= '9' && (valid = true)) ++s;
return !*s && valid && has_intp;
}
if (*s == '.') ++s; else return !*s;
while (*s >= '0' && *s <= '9' && (has_floatp = true)) ++s;
if (*s == 'e' || *s == 'E')
{
++s;
if (*s == '+' || *s == '-') ++s;
valid = false;
while (*s >= '0' && *s <= '9' && (valid = true)) ++s;
return !*s && valid && (has_floatp || has_intp);
}
return !*s && valid && (has_floatp || has_intp);
}
>>
>>106508710
>You're missing the sign check after [eE]
Oops
^[+\-]?(\d+|\.\d+|\d+\.|\d+\.\d+)([eE][+\-]?\d+)?$/
>>
>>106508374
It took me 40 mins to figure out the finite state machine for this problem. Maybe you can get faster the more you practice it, but I think this kind of problems are there to waste your time and will take you 30 mins minimum.
>>
>>106508792
>finite state machine
bloated and slow, this problem only requires a deterministic finite automata
>>
>>106508374
the real solution is to just use the built in number checking function, like you would do in a real project
>>
>>106508374
huh i don't get how this is hard. but i'm a nocoder
>>
valid = False
dot = False
canM = True
exp = False
for i in range(len(s)):
# is digit
if 48 <= ord(s[i]) <= 57:
valid = True
canM = False
elif s[i] == "-":
if not canM:
return False
valid = False
canM = False
elif s[i] == "+":
if not canM:
return False
valid = False
canM = False
elif s[i] == ".":
if dot:
return False
dot = True
valid = valid
canM = False
elif s[i] == "e" or s[i] == "E":
if exp:
return False
if not valid:
return False
valid = False
exp = True
canM = True
dot = True
else:
return False

return valid

0ms
>>
>>106508374
Write it as a grammar, convert to regex
>>
>>106508374
Just use eval
>>
>>106508828
not hard, just too many edge cases
>>
>>106508374
That's like a day 3 tier Advent of Code puzzle.
I'd probably spend 15 minutes torturing a regular expression until it does what I want.
>>
>>106508374
ez
>>
>>106508374
Hit them with Haskell:
import Text.Parsec

main = sequence_ $ fmap (\x -> putStrLn $ x ++ " => " ++ validate x)
[ "0", "e", "."
, "2", "0089", "-0.1", "+3.14", "4.", "-.9", "2e10", "-90E3", "3e+7", "+6e-1", "53.5e93", "-123.456e789"
, "abc", "1a", "1e", "e3", "99e2.5", "--6", "-+3", "95a54e53" ]

validate = either (const "false") (const "true") . parse validNum "" where
validNum = do
optional $ oneOf "-+"
choice . fmap try $ [ many1 digit *> char '.' *> many digit
, char '.' *> many1 digit
, many1 digit ]
optional $ oneOf "eE" *> optional (oneOf "-+") *> many1 digit
eof



[Advertise on 4chan]

Delete Post: [File Only] Style:
[Disable Mobile View / Use Desktop Site]

[Enable Mobile View / Use Mobile Site]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.