-2

I found this practice today:

Detect whether a check number is valid or not. The check number must have 10 digits, can not have 2 or more zeros followed, nor three or more digits other than zero followed

I've able to complete the first part with this code:

num_chk=input('Enter chk number: ')

if (len(num_chk) == 10):
    print('valid')
else:
    print('not valid')

any thoughts on how to create the logic to check if the check number has 2 consecutive 0.

thanks

Mohd
  • 5,523
  • 7
  • 19
  • 30
Isaac Rivera
  • 101
  • 1
  • 10
  • You can use the `in` keyword to see if a substring is in a string. Alternatively, see the documentation for `str.find`. – a p Jul 27 '17 at 01:36

6 Answers6

1

Let's say your check number is the variable check_number and it is 12300.

check_number = 12300

convert it to a string:

str_check_num = str(check_number)
# str_check_num now equals "12300" a a string
has_00 = "00" in str_check_num
# Returns True if "00" is in str_check_num. in this case it is.
Cory Madden
  • 5,026
  • 24
  • 37
1

num_chk is type str, which gives you access to:

a in b - return True if string a is in string b, e.g. '00' in num_chk

To check whether the trailing part is some number of zeroes, you can try endswith or look into regular expressions (re package in Python) if you don't know how many zeroes it might be.

Riaz
  • 874
  • 6
  • 15
1

You can check if the number has two or more zeroes, or three or more digits by using built-in function any(), for example:

if len(num) == 10:
    if any((x in num) for x in [str(i) + str(i) + str(i) for i in range(1, 10)]):
        print('invalid')

    elif '00' in num:
        print('invalid')

    else:
        print('valid')
else:
    print('invalid')

any() returns True if any of the supplied expressions is True.

Mohd
  • 5,523
  • 7
  • 19
  • 30
0

A solution with regex for detecting the others condition, as suggested by @Riaz, should be definitely better than what I'm about to suggest, but for the sake of completeness, one can also do the following using itertools.groupby, grouped_L returns a list of tuples with a number followed by it's number of consecutive duplicates (Reference):

from itertools import groupby

num = 1234567890  # Using input as an int
L = [int(x) for x in str(num)]
grouped_L = [(k, sum(1 for i in g)) for k,g in groupby(L)]

validity = True
if len(str(num)) == 10:
    for i in grouped_L:
        if (i[0]!=0 and i[1]>2):
            validity = False
            break
        elif (i[0]==0 and i[1]>1):
            validity = False
            break
        else:
            validity = True
else:
    validity = False

if validity:
    print('valid')
else:
    print('not valid')
Vinícius Figueiredo
  • 6,300
  • 3
  • 25
  • 44
0

something like :

>>> def check(num):
...     l=[str(i)+str(i) for i in range(10)]
...     l[0]='000'
...     if len(num)!=10: 
...             return "not valid"
...     else:
...             for i in l:
...                     if i in num:
...                             return "not valid"
...     return "valid"
... 
>>> n="1234567890"
>>> check(n)
'valid'
>>> n="1234500090"
>>> check(n)
'not valid'
Dadep
  • 2,796
  • 5
  • 27
  • 40
0

If I understand the question ("two or more zeroes followed" means "two or more consecutive zeroes"?), the number must have exactly 10 digits. It must not have two (or more) consecutive zeroes. It must not have three (or more) consecutive digits other than zero. (Three consecutive zeroes implies two consecutive zeroes, so that does constitute an invalid number because of the 2nd rule--we do not need to exclude zero from the last test).

That gives that code

def validate(s):
  # 10 characters
  if len(s) != 10:
    return False
  # all digits
  for c in s:
    if not c.isdigit():
      return False
  # no two or more consecutive '0'
  for a,b in zip(s[0:9],s[1:10]):
    if a == b == '0':
      return False
  # no three or more consecutive digits
  for a,b,c in zip(s[0:8],s[1:9],s[2:10]):
    if (a == b == c) and a.isdigit():
      return False
  #
  return True

TESTS = (
  '12345678x0',  # one character not digit
  '123456789',   # not 10 characters
  '1234567890',  # valid
  '1230067890',  # two consecutive zeroes
  '1224567890',  # valid
  '1114567890',  # three consecutive digits (other than zero)
)
for t in TESTS:
  print( t, 'valid' if validate(t) else 'not valid' )

n = raw_input("Enter number: ")
print( n, 'valid' if validate(n) else 'not valid' )
Sci Prog
  • 2,651
  • 1
  • 10
  • 18