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

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...
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?
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 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 "
Write a program in "RISC-V" assembly to convert an ASCII string containing a positive or negative...
Write a program in "RISC-V" assembly to convert an ASCII string containing a positive or negative integer decimal string to an integer. ‘+’ and ‘-’ will appear optionally. And once they appear, they will only appear once in the first byte. If a non-digit character appears in the string, your program should stop and return -1.
Write a program in MIPS assembly language to convert an ASCII number string containing positive and...
Write a program in MIPS assembly language to convert an ASCII number string containing positive and negative integer decimal strings, to an integer. Your program should expect register $a0 to hold the address of a nullterminated string containing some combination of the digits 0 through 9. Your program should compute the integer value equivalent to this string of digits, then place the number in register $v0. If a non-digit character appears anywhere in the string, your program should stop with...
3. Write a function named "countNonAlpha" that accepts a string. It will return the number of...
3. Write a function named "countNonAlpha" that accepts a string. It will return the number of non-alphabet characters (excluding blanks) in the string. For example, if the string is "Hello, World!", it will return 2 for ',' and '!" in the string. 4. Write a function named "deleteZeros" that takes two arguments: a. an array of integer values; b. an integer for the number of elements in the array; The function will return the number of zeros that it has...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT