1

When I was doing questions on Chapter 14 on Computer Science Circles, I found something weird.

The question said: "Define a function postalValidate(S) which first checks if S represents a postal code which is valid: first, delete all spaces; the remainder must be of the form L#L#L# where L are letters (in either lower or upper case) and # are numbers. If S is not a valid postal code, return the boolean False. If S is valid, return a version of the same postal code in the nice format L#L#L# where each L is capital."

and this is my working for this question:

def postalValidate(S):
   if S.count(' ') > 0:
      S.replace(' ', '')
   if S.isalpha() or S.isdigit() == True:
      return False
   else:
      if S[0].isalpha() and S[2].isalpha() and S[4].isalpha() == False:
         return False
      else:
         if S[1].isdigit() and S[3].isdigit() and S[5].isdigit() == False:
            return False
         else:
            return S.upper()

All the inputs were correct except this postalValidate(' d3 L3 T3'). I used S.count to identify how many ' ' are there in the input, but it didn't work. Instead, the input turned out to be 'd3L3T3' right after it goes into the function.

Is there proper way to count ' ' string? Or is there other ways to remove ' ' in the string instead of using string methods?

10
  • 3
    You don't need to check if there are spaces, just do the replacement BUT re-assign to S, S = S.replace(' ', ''); strings are immutable and str.replace is not in-place Commented Feb 26, 2018 at 10:12
  • 3
    Is if S[0].isalpha() and S[2].isalpha() and S[4].isalpha() == False: intended to check whether all of those conditions are False? At the moment it's equivalent to if S[0].isalpha() == True and S[2].isalpha() == True and S[4].isalpha() == False: which I don't think you intended Commented Feb 26, 2018 at 10:14
  • @Chris_Rands Thanks, it worked. Commented Feb 26, 2018 at 10:18
  • @roganjosh Yes I intended to check all of those conditions whether the input conforms to 'L#L#L#' form. Commented Feb 26, 2018 at 10:19
  • Then you need to be explicit, it is not understood by Python in the way you might express it in English. if not S[0].isalpha() and not S[2].isalpha() and not S[4].isalpha():. For longer lists of conditions you might want to use any() or all(). But at the moment, it's not doing what you think it's doing. I'm not even sure you should be using and but or instead. Commented Feb 26, 2018 at 10:21

1 Answer 1

1

As it was mentioned in the comments if you have several conditions then you need to check all of them:

if S[0].isalpha() and S[2].isalpha() and S[4].isalpha() == False: 
    ...

Is not what you want. You need to write this:

if S[0].isalpha() == True and S[2].isalpha() == True and S[4].isalpha() == False: 
    ...

As another option you can use regex matching:

import re

def postalValidate(S):
    S = S.replace(' ', '')
    pattern = re.compile('^([a-zA-Z][0-9]){3}$')

    if pattern.match(S):
        return S.upper()
    else:
        return False


if __name__ == '__main__':
    print(postalValidate('l3D1 z1 '))
    print(postalValidate('11z'))
    print(postalValidate('a1b2c3 '))
    print(postalValidate('3 l D1 z1 '))
    print(postalValidate('3 l D1 z1 b2 '))
    print(postalValidate(''))
    print(postalValidate(' '))
    print(postalValidate(' L3 z0 V1 '))

This variant is more flexible and easier to change.

Sign up to request clarification or add additional context in comments.

2 Comments

Both of those if statements are identical (if S[0].isalpha() and if S[0].isalpha() == True are the same thing). In fact, if the OP intended those conditions to be checked as being True, their initial code was more pythonic. But they didn't, they wanted to check if they were False.
I just identified that my working has many logical errors, and I will be trying making new method using regex with reference to your advice, thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.