JavaScript provides a built-in parseInt method.

It can be used like this:

- `parseInt("10")` returns `10`
- `parseInt("10 apples")` also returns `10`

We would like it to return `"NaN"` (as a string) for the second case because the input string is not a valid number.

You are asked to write a `myParseInt` method with the following rules:

- It should make the conversion if the given string only contains a single integer value (and possibly spaces - including tabs, line feeds... - at both ends)
- For all other strings (including the ones representing float values), it should return NaN
- It should assume that all numbers are not signed and written in base 10

Difficulty: introductory

Starter code:

def my_parse_int(string):
	

Solutions:
def my_parse_int(s):
    try:
        return int(s)
    except ValueError:
        return 'NaN'

def my_parse_int(string):
    try:
        return int(string)
    except ValueError:
        return 'NaN'


def my_parse_int(s):
    return int(s) if s.strip().isdigit() else "NaN"


def my_parse_int(string):
    try:
        return int(string)
    except:
        return "NaN"


def my_parse_int(stg):
    try:
        return int(stg)
    except ValueError:
        return "NaN"

def my_parse_int(str):
  try: return int(str)
  except: return 'NaN'

def my_parse_int(num_str):
  try:
    return int(num_str.strip())
  except:
    return 'NaN'

import re


def my_parse_int(string):
    return int(''.join([x for x in string if x.isdigit()])) if re.match(r'\s*\d+\s*$', string) else 'NaN'

def my_parse_int(string):
    """Parses an str for an int value. If the str does not convert to a valid int, return the "NaN" string."""
    try:
        return int(string)
    except ValueError:
        return "NaN"

def my_parse_int(string):
    #your code here, return the string "NaN" when the input is not an integer valueNaN
    try:
        return int(string)
    except:
        return "NaN"