Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Check If Input Is a Number
00:00
5 left

Check If Input Is a Number

MediumPython

Problem

Write a function that checks if the input is or is not a number.

Treat the input as a string and return True only for valid finite decimal or scientific-notation formats. Allow an optional leading sign, decimal values such as 12, -3.5, .8, and 7., and exponents such as 1e-4; reject whitespace, empty strings, malformed numbers, NaN, and infinity.

Input: one string value.

Output: a Boolean indicating whether value is a valid number.

Constraints

  • value is a string
  • 0 <= len(value) <= 1000
  • Only ASCII decimal digits, signs, decimal points, and exponent markers are relevant
  • Leading or trailing whitespace is invalid

Function Signature

def is_number(value):
Interviewer

Your question is Check If Input Is a Number. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.