how can i use a function to identify if zero is the first number in an alphanumeric string?

Viewed 51

I'm trying to create a function that will identify if 0 is the first number in an alphanumeric sequence; for example, the function should evaluate to True if given the string "J02". I came up with a for loop that would work for inputs that are alphanumeric but not strictly alpha inputs.

x = "J02"
def not_starting_zero(m):
    zero_string = []
    for char in m:
        if char.isdigit() == True:
            zero_string.append(char)
            if m[len(m)-1] == char:
                if zero_string[0] == "0":
                    return False
                else:
                    return True
not_starting_zero(x)

I tried using an else statement that aligned with the indentation of the if char.isdigit() == True: ; but that would make the return of the function true if the first index of the string is a letter.

5 Answers

You could use a regular expression to find the first digit:

import re

def not_starting_zero(m):
    first_digit = re.search(r"([0-9])", m)
    if first_digit:
        return first_digit.group(0) == "0"
    else:
        return False

Alternatively, you could use your looping version - I think you can just stop after you encounter the first digit:

x = "J02"
def not_starting_zero(m):
    for char in m:
        if char.isdigit() == True:
            if char == "0":
                return True
            # otherwise, return False
            return False

    # if we get here, there weren't any numbers
    return False

not_starting_zero(x)

I suppose you could use regex and extract the numeric digits into a list. Something like this -

import re

def not_starting_zero(m):
    numbers = re.findall('[0-9]', m)

    if numbers[0] == '0':
        return false
    else
        return true

I'm assuming you mean the first digit you find is 0 (because otherwise it should return False for "J02" because 02 is the first number)

x = "J02"
def not_starting_zero(m):
    for char in m:
        if char.isdigit() == True:
            if char == "0":
                return True
            else:
                return False
    return False
not_starting_zero(x)

This works because once return is executed in a function the rest of the function is not executed. Hope this clears up your doubt.

You don't need to make an array of the digits because you only need to check the first digit in the string; if its a 0 then return True else return False.

Below code can find if 0 is the first number in alphanumeric string.

import regex as re
x = "J02"
True if re.findall('[0-9]', x)[0] == '0' else False

Output:

True

Take note on type safety, how expressive the code is, and how it instills referential transparency for code correctness (accuracy). All those loops cause too many mutations, and mutations cause errors (bugs). See here for referential transparency.

import re

str1 = 'jdh487y3hef8ty483rhfeih89t4389jf0dwiefh38uth'
str2 = '0dh487y3hef8ty483rhfeih89t4389jfdwiefh38uth'

# check if a given char is first in letter in string
def check_for_char(alpha_num:str, _char) -> bool:
   if re.search('0', alpha_num).span(0)[0] == _char: 
      return True
   else:
      return False

is_false = check_for_char(
    alpha_num=str1,
    _char=0
)
is_true = check_for_char(
    alpha_num=str2,
    _char=0
)

print(is_false) # False
print(is_true) # True

# search if a given char exists at all within the alphanum str
print(str1[re.search('0', str1).span(0)[0]]) # 0
Related