← Home ← Back to /g/

Thread 106508374

17 posts 4 images /g/
Anonymous No.106508374 >>106508592 >>106508681 >>106508710 >>106508792 >>106508822 >>106508828 >>106508949 >>106508960 >>106509191 >>106509284 >>106509501
>too many edge cases
>mental breakdown and brain stops functioning in the interview
wat do?
Anonymous No.106508592 >>106508660
>>106508374 (OP)
Should be pretty easy with regex. I'll cook something up.
Anonymous No.106508660 >>106508710
>>106508592
Here
^[+\-]?(\d+|\.\d+|\d+\.|\d+\.\d+)([eE]\d+)?$
Anonymous No.106508681
>>106508374 (OP)
if they ask you leetcode hards they probably don't wanna hire you
focus on improving your personality
Anonymous No.106508710 >>106508753
>>106508660
https://regex101.com/r/zND6BN/1
You're missing the sign check after [eE]

>>106508374 (OP)
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);
}
Anonymous No.106508753
>>106508710
>You're missing the sign check after [eE]
Oops
^[+\-]?(\d+|\.\d+|\d+\.|\d+\.\d+)([eE][+\-]?\d+)?$/
Anonymous No.106508792 >>106508816
>>106508374 (OP)
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.
Anonymous No.106508816
>>106508792
>finite state machine
bloated and slow, this problem only requires a deterministic finite automata
Anonymous No.106508822
>>106508374 (OP)
the real solution is to just use the built in number checking function, like you would do in a real project
Anonymous No.106508828 >>106509122
>>106508374 (OP)
huh i don't get how this is hard. but i'm a nocoder
Anonymous No.106508861
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
Anonymous No.106508949
>>106508374 (OP)
Write it as a grammar, convert to regex
Anonymous No.106508960
>>106508374 (OP)
Just use eval
Anonymous No.106509122
>>106508828
not hard, just too many edge cases
Anonymous No.106509191
>>106508374 (OP)
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.
Anonymous No.106509284
>>106508374 (OP)
ez
Anonymous No.106509501
>>106508374 (OP)
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