Problem of the Day
A new programming or logic puzzle every Mon-Fri

Address Parser

The goal of this project is build something that can parse an address out in to different parts. Base the program off your country but here is an example for those in the US.

Address: 1234 Hello There Lane, San Francisco, CA 91444

Street: 1234 Hello There Lane
City: San Francisco
State: CA
Ziip: 94144

One thing to watch out for is that a zip can be 9 digits long rather than just 5 and can include a "-".

Permalink: http://problemotd.com/problem/address-parser/

Comments:

  • David - 9 years ago

    Python 2.7
    Quick and dirty with a touch of format checking.

    def extract_address(input):
      street = ''
      city = ''
      state = ''
      zipcode = ''
      error = ''
      header, colon, address = input.partition(':')
      if((colon != ':') and (header.strip() != 'Address')):
        error += "Error: Format"
      street, delim, tail = address.partition(',')
      if(delim != ','):
        error += "Error: Format"
      street = street.strip()
      city, delim, tail = tail.strip().partition(',')
      if(delim != ','):
        error += "Error: Format"
      city = city.strip()
      state, delim, zipcode = tail.strip().partition(' ')
      if(delim != ' '):
        error += "Error: Format"
      state = state.strip()
      zipcode = zipcode.strip()
      return {'street': street, 'city': city, "state": state, 'zipcode': zipcode, 'error': error}
    
    def output_address(input):
      if(input['error']):
        print "An Error occurred in parsing the address: " + input['error']
      else:
        print "street: " + addr['street']
        print "city: " + addr['city']
        print "state: " + addr['state']
        print "zip: " + addr['zipcode']
    

    reply permalink

Content curated by @MaxBurstein