Python performance query

So, I am learning Python code (I have not studied another language before) and performed the second exercise: http://cscircles.cemc.uwaterloo.ca/15b-python-pushups/

I have created code that works, but I cannot help but feel that there should be a way to do this with fewer lines, here is my current solution:

def check(S):
  a = S.replace(' ', '')
  if len(S) != 19:
     return False

  try:
     int(a)
  except ValueError:
     return False
  lister = []
  for i in range (0, len(a)):
     lister.append(int(a[i]))   
  if sum(lister)%10 != 0:
     return False
  if sum(lister) == 0:
     return False
  return True

number one: Is there a better way to verify that all characters are numbers? I know the .isdigit () method, but not sure how I implement it, does it work? for a.isdigit (): if False: return False

number two: Is there a better way to check the condition right away using "or", for example, this part of my code:

if sum(lister)%10 != 0:
     return False
  if sum(lister) == 0:
    return False

Can I combine them somehow?

+4
source share
4 answers

:

for i in range(len(a)):
     lister.append(int(a[i]))
return sum(lister) % 10 == 0 or sum(lister) == 0

return True, True False, True

a comp:

lister = [int(i) for i in a ]
return sum(lister) % 10 == 0 or sum(lister) == 0

:

lister_sum = sum(int(i) for i in a )
return lister_sum % 10 == 0 or lister_sum == 0

, all str.isdigit, try/except:

if len(S) != 19 or not all(x.isdigit() for x in a)

, :

def check(s):
    # split into individual strings
    spl = s.split() 
    # make sure  format is #### #### #### ####
    if len(spl) != 4 or not all(len(x) == 4 for x in spl): 
        return False
    a = s.replace(" ","")
    elif len(s) != 19 or not all(x.isdigit() for x in a):
        return False
    lister_sum = sum(int(i) for i in a)
    return lister_sum % 10 == 0

try/except, map:

def check(s):
    if len(s) != 19:
        return False
    spl = s.split()
    if len(spl) != 4 or not all(len(x) == 4 for x in spl):
        return False
    a = s.replace(" ","")
    try:
         a = list(map(int,a))
    except ValueError:
        return False
    return sum(a) % 10 == 0
+2
  • , . , 16 , , .
  • ( ) , , . , , a , S.
  • try: int(a) , . , -, , , int(a[0]). False , .
  • if sum(lister) == 0: return False , , 0000 0000 0000 0000 . , , ; -)

, , . " ", Python:

sum(int(d) for d in a)

, , , :

if something:
    return False
return True

, :

return not something

, , , , .

:

def check(s):
    if len(s) != 19:
        return False
    # there more than one way to check this, a regex is fine, but another way is
    expected_spaces = (4, 9, 14)
    for idx, ch in enumerate(s):
        if (ch == ' ') != (idx in expected_spaces):
            return False
    try:
        checksum = sum(int(d) for d in s if d != ' ')
        return checksum % 10 == 0
        # or you might prefer a one-liner:
        # return sum(int(d) for d in s if d != ' ') % 10 == 0
    except ValueError:
        return False

, int() , ( , ), , Unicode U+0660, "Arab-Indic Digit Zero". , , , . , Matt ASCII.

+2

You do not need to hardcode the required string formatting into array indices or regular expressions. You can simply take a given format string and use it to parse the number. Then you can check each character in the appropriate format individually and use the numbers to add to the checksum.

Here is an example:

def check(s):
    # the format string:
    # "#" digit character, " ": space character
    fmt = "#### #### #### ####"
    # check length of input string
    if len(s) != len(fmt):
        return False
    # compute checksum and validate format
    checksum = 0
    # iterate "zipped" format and input string
    for f,i in zip(fmt, s):
        if f == "#":
            # expecting i to be digit
            try:
                checksum += int(i)
            except ValueError:
                # i is not a digit
                return False
        elif i != " ":
            # expected i to be space (but it not)
            return False
    # validate checksum
    return not checksum%10

For reference:

+1
source

This will complete the exercise:

import re
re_credit = re.compile(r'^([0-9]{4}) ([0-9]{4}) ([0-9]{4}) ([0-9]{4})$')
def check(s):
    if not re_credit.match(s):
         return False
    checksum = 0
    for c in s.replace(' ', ''):
         checksum += int(c)
    if (checksum % 10) != 0:
         return False
    return True
0
source

All Articles