Question
Expected Behavior Write a Python program in, street.py, that prompts the user for a one-line specification of a city street and then prints a simple
Expected Behavior
Write a Python program in, street.py, that prompts the user for a one-line specification of a city street and then prints a simple ASCII rendering of it. The user should be prompted as follows:
input("Street: ")
The user responds with a specification that has some number of buildings, parks, and empty lots. As an example, the input line
p:19,* b:5,7,x e:7,__~
is rendered as this:
+-------------------------------+ | | | xxxxx | | xxxxx | | * xxxxx | | *** xxxxx | | ***** xxxxx | | | xxxxx | | | xxxxx ~ ~ | +-------------------------------+
Input format
The input will be a single line that contains one or more whitespace-separated specifications for buildings, parks, and/or empty lots that appear along the street.
A building is specified with a string of the form "b:WIDTH,HEIGHT,BRICK". The building on the street shown above has this specification:
b:5,7,x
The string above specifies a building with a width of five, a height of seven, that is built of "x"s. Assume that WIDTH and HEIGHT are integers greater than zero and that BRICK is always a single non-whitespace character.
A park is specified with a string of the form "p:WIDTH,FOLIAGE". A park has exactly one tree and that tree is centered in the park. Parks always have an odd integer width that is greater than or equal to five. Every park's tree has the same shape and size but has the FOLIAGEthat is specified. Like the brick for a building, FOLIAGE is always a single non-whitespace character. The park shown on the street above has this specification:
p:19,*
An empty lot is specified with a string of the form "e:WIDTH,TRASH". (Sadly, empty lots tend to accumulate trash.) The empty lot shown on the street above has this specification:
e:7,__~
The TRASH string, "__~" in this case (two underscores and tilde), specifies a string that is repeated as necessary to span the width of the empty lot. Replicating "__~" to its width of seven produces "__~__~_". Further, an underscore in a TRASH string is interpreted as a blank. Combining those rules for this lot ("e:7,__~") produces this seven character string: " ~ ~ ".
A TRASH string may be longer than the width of the lot. If so, the excess characters simply don't appear in the rendered lot.
Here is a more complex street:
b:3,10,a e:10,+- p:13,* b:7,15,x b:17,4,% b:10,8,y e:5,_ b:1,3,$ p:13,^ b:10,3,# e:9,__.
and its rendering:
+--------------------------------------------------------------------------------------------------+ | | | xxxxxxx | | xxxxxxx | | xxxxxxx | | xxxxxxx | | xxxxxxx | |aaa xxxxxxx | |aaa xxxxxxx | |aaa xxxxxxx yyyyyyyyyy | |aaa xxxxxxx yyyyyyyyyy | |aaa xxxxxxx yyyyyyyyyy | |aaa * xxxxxxx yyyyyyyyyy ^ | |aaa *** xxxxxxx%%%%%%%%%%%%%%%%%yyyyyyyyyy ^^^ | |aaa ***** xxxxxxx%%%%%%%%%%%%%%%%%yyyyyyyyyy $ ^^^^^ ########## | |aaa | xxxxxxx%%%%%%%%%%%%%%%%%yyyyyyyyyy $ | ########## | |aaa+-+-+-+-+- | xxxxxxx%%%%%%%%%%%%%%%%%yyyyyyyyyy $ | ########## . . .| +--------------------------------------------------------------------------------------------------+
Output format
Along with the specifications above, note these points:
A border surrounds the rendered street.
The height of the rendering is one more than the tallest element that is drawn; effectively that produces a single line of padding at the top. The height of a park is always five. The height of an empty lot is always one.
More examples
Some additional examples of the behavior of this program are given here.
Errors
This program does no error handling whatsoever. The behavior is undefined if any of the above specifications are not met. That phrase, "behavior is undefined", tells the user that the program's handling of invalid input is unpredictable. For example, if a building specification is missing the BRICK, the program might terminate with an error, or terminate with an assertion failure, or it might use the same brick that was used in a previous building. A negative height might cause the program to go into an infinite loop.
The bottom line is that you should write code that assumes the input is correct. The tester won't have any cases with invalid input. No cases with invalid input will be used when grading. All you've got to worry about is the "happy case".
Programming Requirements
An essential requirement of this problem is that you may not use any for or while statements or list comprehensions in your solution.Use recursion in place of looping, as you've seen done in lecture and have done yourself when solving the CloudCoder problems.
Hint: !!!!!!!!!!!!!!!!!!!!!!!!!!!!
Think about a function print_street_at_height(elements, height) that produces a single line of output: the street at the specified height. For example, again consider this street, with heights shown in a column on the right:
height +-------------------------------+ | | 7 | xxxxx | 6 | xxxxx | 5 | * xxxxx | 4 | *** xxxxx | 3 | ***** xxxxx | 2 | | xxxxx | 1 | | xxxxx ~ ~ | 0 +-------------------------------+
The call print_street_at_height(..., 0) would print this line:
| | xxxxx ~ ~ |
The call print_street_at_height(..., 2) would print this line:
| ***** xxxxx |
Thus, calling print_street_at_height with a series of descending heights would produce the rendering, excluding the top and bottom borders.
Think about having Building, Park, and EmptyLot classes. Give each class an at_height method that returns a string that is the text for that element at the specified height. Example: (blank lines added for clarity)
>>> b = Building(3,4,"x") # NOTE: this is not the same building that's shown above! >>> b.at_height(0) 'xxx' >>> b.at_height(4) ' '
Next, consider this:
>>> s = [EmptyLot(5,"_"), Building(3,4,"x"), Park(7,"*")] >>> h = 0 >>> s[0].at_height(h) + s[1].at_height(h) + s[2].at_height(h) ' xxx | ' >>> h = 2 >>> s[0].at_height(h) + s[1].at_height(h) + s[2].at_height(h) ' xxx ***** '
Combining the ideas of (1) and (2) leads to this:
>>> s = [EmptyLot(5,"_"), Building(3,4,"x"), Park(7,"*")] >>> print_street_at_height(s, 5) >>> print_street_at_height(s, 4) * >>> print_street_at_height(s, 3) xxx *** >>> print_street_at_height(s, 2) xxx ***** >>> print_street_at_height(s, 1) xxx | >>> print_street_at_height(s, 0) xxx |
Note: The version of print_street_at_height used above output a newline after the text to make for a clear example but in practice you may find that having print_street_at_height output a newline creates problems.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started