Hacker News new | ask | show | jobs
by ThinkWriteMute 5956 days ago
Here's a ruby version, smaller more compact and easier to update (probably more correct too):

  def state?(zip)
   case zip[0]
    when 0 then states = ['CT','MA','ME','NH','NJ','PR','RI','VT','VI','AE']
    when 1 then states = ['DE','NY','PA']
    when 2 then states = ['DC','MD','NC','SC','VA','WV']
    when 3 then states = ['AL','FL','GA','MS','TN','AA']
    when 4 then states = ['IN','KY','MI','OH']
    when 5 then states = ['IA','HN','MT','ND','SD','WI']
    when 6 then states = ['IL','KS','MO','NE']
    when 7 then states = ['AR','LA','OK','TX']
    when 8 then states = ['AZ','CO','ID','NM','NV','UT','WY']
    when 9 then states = ['AK','AS','CA','GU','HI','MH','FM','MP','OR','PW','WA','AP']
    else return null
   end
   return states
  end
1 comments

That will be useful for narrowing down the states, but it doesn't actually convert zip code to state.

It just converts first digit of zip code to list of states to which it could apply.

There is no exact way to pump it out. No part of the Zip code actually comes down to a city.

  [#][##][##]
  [State][County/Parish][Area]
Instead you should be giving your users a choice made on exact facts instead of probabilities.