Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First - Useless RE solution in Clear category for Reveal the Number by Red_Ale
import re
def reveal_num(line: str) -> int | float | None:
tester = re.findall(r'[\d+-.]', line) # This is totally useless, "for" cycle works directly on line.
# But I studied RE module for this solution and I wanted to keep it :p
plus, digit, dot = 0, 0, 0
sol = ""
for i in tester:
if i in '+-' and digit == 0: # Checks for sign of number and add it to the solution.
plus += 1 # It overwrites "sol" until a digit is found
sol = i
if i.isdigit(): # Check for all the digit and add them to the solution
digit += 1
sol += i
if i == '.' and dot == 0: # Check for dot and add only the first found
dot += 1
sol += i
if len(sol) == 0: return None
sol = float(sol) if '.' in sol else int(sol)
return sol if (sol or sol == 0) else None
print("Example:")
print(reveal_num("+A%+-1-0..."))
# These "asserts" are used for self-checking
assert reveal_num("F0(t}") == 0
assert reveal_num("Utc&g") == None
assert reveal_num("-aB%|_-+2ADS.12+3.ADS1.2") == 2.12312
assert reveal_num("-aB%|_+-2ADS.12+3.ADS1.2") == -2.12312
assert reveal_num("zVâ„–1}3;o.vEf``C.WqTY0") == 13.0
assert reveal_num("!3B'j=(}89JQ6aWvN*%5@uy.r)B
Feb. 27, 2023
Comments: