Question

In: Computer Science

# Write three functions to return the pieces of a string containing # a city, state,...

# Write three functions to return the pieces of a string containing

# a city, state, and zip code.

# This function needs a better comment

def get_zip(city_state_zip):   

return city_state_zip[-1]

# This function needs a better comment

def get_city(city_state_zip):

return city_state_zip[0:2]

# This function needs a better comment

def get_state(city_state_zip):   

return 'Confusion'

def main():   

place1 = 'Rolla, MO 65402'   

place2 = 'Cape Girardeau,     MO 63780'   

place3 = 'St. Louis,MO63111'   

place4 = 'International Falls,MN               56650     '  

# Print the individual components   

print ('City:', get_city(place1))   

print ('State:', get_state(place1))   

print ('Zip:', get_zip(place1))    

# Repeat printing for the rest of the placesmain()

Directions

Your assignment is to write 3 functions, each of which takes a single string parameter representing [city, state, and zip code] and returns the indicated piece of the string. The main program should call each function, and print the returned values, one per line. However, the data that is passed to the functions was input on a keyboard that had a temperamental space bar. Sometimes it doesn't work and other times it generates multiple spaces, so the data may look like

'Rolla,MO   65402'

or

'Springfield, MO65897'

The one thing that is consistent is that there is a comma after the city name, the state is always two upper case characters, and the zip code is 5 adjacent characters 0-9.

For the first case listed, your program should output:

City: Rolla
State: MO
Zip: 65402

To make things interesting, add two more variables with cities of your choice. Choose the spacing to help test your program.

Solutions

Expert Solution

CODE -

# This function extracts and returns the zip of a city from a string containing city name, state name, and zip code

def get_zip(city_state_zip):   

    city, state_zip = city_state_zip.split(",")

    state_zip = state_zip.strip()

    return state_zip[-5:]


# This function extracts and returns the name of a city from a string containing city name, state name, and zip code

def get_city(city_state_zip):

    city, state_zip = city_state_zip.split(",")

    city = city.strip()

    return city

# This function extracts and returns the state name of a city from a string containing city name, state name, and zip code

def get_state(city_state_zip):   

    city, state_zip = city_state_zip.split(",")

    state_zip = state_zip.strip()

    return state_zip[0:2]

def main():   

    place1 = 'Rolla, MO 65402'   

    place2 = 'Cape Girardeau,     MO 63780'   

    place3 = 'St. Louis,MO63111'   

    place4 = 'International Falls,MN               56650     '

    place5 = 'Jefferson City,             MO            65043'

    place6 = 'Saint Paul,      MN       55101'

    # Print the individual components   

    print ('City:', get_city(place1))   

    print ('State:', get_state(place1))   

    print ('Zip:', get_zip(place1))

    print ('City:', get_city(place2))   

    print ('State:', get_state(place2))   

    print ('Zip:', get_zip(place2))

    print ('City:', get_city(place3))   

    print ('State:', get_state(place3))   

    print ('Zip:', get_zip(place3))

    print ('City:', get_city(place4))   

    print ('State:', get_state(place4))   

    print ('Zip:', get_zip(place4))

    print ('City:', get_city(place5))   

    print ('State:', get_state(place5))   

    print ('Zip:', get_zip(place5))

    print ('City:', get_city(place6))   

    print ('State:', get_state(place6))   

    print ('Zip:', get_zip(place6))


main()

SCREENSHOTS -

CODE -

OUTPUT -

If you have any doubt regarding the solution, then do comment.
Do upvote.


Related Solutions

Write a program that receives an integer and must return a string containing the hexadecimal representation...
Write a program that receives an integer and must return a string containing the hexadecimal representation of that integer.
A. State three functions of the bacterial cytoskeleton. B. Name and state the function of three...
A. State three functions of the bacterial cytoskeleton. B. Name and state the function of three organelles that are unique to bacteria. Now name three organelles unique to eukaryotic cells. C. Describe the special organization of organelles and cytoskeleton that is only found in eukaryotic cells. How does this special organization affect eukaryotic cell physiology?
Using the string functions below, write new functions to do the following, and test them in...
Using the string functions below, write new functions to do the following, and test them in your main() function: Determine whether the first or last characters in the string are any of the characters a, b, c, d, or e. Reverse a string Determine whether a string is a palindrome (spelled the same way forward or backward FUNCTIONS REFERENCE: string myString = "hello"; // say we have a string… // … we can call any of the following // string...
For these of string functions, write the code for it in C++ or Python (without using...
For these of string functions, write the code for it in C++ or Python (without using any of thatlanguage's built-in functions) You may assume there is a function to convert Small string into the language string type and a function to convert your language's string type back to Small string type. 1. int [] searchA,ll(string in...str, string sub): returns an array of positions of sub in in...str or an one element array with -1 if sub doesn't exist in in...str
Write a function that will take a string containing only alphanumeric characters that are in lowercase...
Write a function that will take a string containing only alphanumeric characters that are in lowercase (if you think your logic requires you to use more than one argument, please go ahead). Your task is to see if the string becomes a palindrome if only one character is removed from anywhere in the string.
Java- creat a method that takes two char parameters. Return a String containing all characters, in...
Java- creat a method that takes two char parameters. Return a String containing all characters, in order, from the first char parameter (inclusive) to the last (inclusive). For instance, input('a', 'e'), and return "abcde".
Write a python program that asks the user to enter a string containing numbers separated by...
Write a python program that asks the user to enter a string containing numbers separated by commas, e.g., s = '1.23,2.4,3.123', Your program should then calculate and print the sum of the numbers entered. Hint: you need to iterate over the string searching for the commas, i.e. their index. The first number is obtained by slicing between the start of the string and the index of the first comma. The second number is between the last comma and the next...
C++ programming question class Address { public: Address(const std::string& street, const std::string& city, const std::string& state,...
C++ programming question class Address { public: Address(const std::string& street, const std::string& city, const std::string& state, const std::string& zip) : StreetNumber(street), CityName(city), StateName(state), ZipCode(zip) {} std::string GetStreetNumber() const { return StreetNumber; } void SetStreetNumber(const std::string& street) { StreetNumber = street; } std::string GetCity() const { return CityName; } void SetCity(const std::string& city) { CityName = city; } std::string GetState() const { return StateName; } void SetState(const std::string& state) { StateName = state; } std::string GetZipCode() const { return ZipCode; }...
-Write in C++ -Use Char library functions Write a function that accepts a string representing password...
-Write in C++ -Use Char library functions Write a function that accepts a string representing password and determines whether the string is a valid password. A valid password as the following properties: 1. At least 8 characters long 2. Has at least one upper case letter 3. Has at least one lower case letter 4. Has at least one digit 5. Has at least on special character
Write a short program that performs the following: - Method city returns the string "Seattle": -...
Write a short program that performs the following: - Method city returns the string "Seattle": - Method named state returns the string "Washington ": - Method named report calls the city methodand calls the state method. and prints their return values, so that the output will appear as : "Seattle Washington "
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT