Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8342595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:49:07+00:00 2026-06-09T05:49:07+00:00

I’m doing some tutorials online…and I’m stuck at an exercise:Define a function postalValidate(S) which

  • 0

I’m doing some tutorials online…and I’m stuck at an exercise:Define a function postalValidate(S) which first checks if S represents a postal code which is valid. Note: I’m supposed to solve it with strings, lists, if statements, loops, and other basic constructs like variables and functions.

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.

I’ve created a code for these exercise, but the grader said that is wrong, however I will post it too.

def postalValidate(S):
   while " " in S:
     S.remove(" ")
   for i in range(1,6,2):
     S.isdigit(S[i])
   for j in range(0,5,2):
     S.upper(S[j]
     S.isalpha(S[j])
   return True
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T05:49:08+00:00Added an answer on June 9, 2026 at 5:49 am

    This is clearly a job for regular expressions, although I don’t know if you’re supposed to use them in the exercise…

    I’m posting this as an answer just in case you can. Otherwise, let us know…

    #/usr/bin/evn python
    
    import re
    zipCode = re.compile(r"\s*(\w\d\s*){3}\s*")
    
    if __name__ == "__main__":
        samples = [
            "           44F 4 F", #Invalid
            "  L0L0L0    ", #Valid
            "  L0  L0  L0    ", #Valid
        ]
    
        for sample in samples:
            if zipCode.match(sample):
                print "The string %s is a valid zipCode (nice and clean: %s)" % (sample, sample.replace(" ", "").upper())
            else:
                print "The string %s is NOT a valid zipCode" % sample
    

    Edit:

    Since you can not use regular expressions, I’d recommend you change the way of thinking… Instead of checking if the characters belong to a valid postal code, I’d recommend you do the opposite: check if they DON’T belong in a valid postal code, returning False as soon as you detect a misplaced (or wrong) character:

    def postalValidate(S):
        S = S.upper().replace(" ", "")
        if len(S) == 6:
            for i in range(len(S)):
                if i % 2 == 0:
                    #Even index (0, 2, 4, 6...) , has to be 'letter'
                    if not(S[i].isalpha()):
                        return False 
                else:
                    #Odd index (1, 3, 5, 7...), must be 'number'
                    if not(S[i].isdigit()):
                        return False
    
        else:
            #You can save some cpu ticks here... at this point, the string has to be of length 6 or you know it's not a zip
            return False
        return S
    

    The return statement stops the execution of the current function so as soon as you realize that there’s something “wrong” with the string to check, you can return False (there’s no point on keeping checking once you know it’s not valid, right?)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.