Some number of bombs sit in a linear chamber, at time 0. At time 1, the bombs ex
ID: 3821870 • Letter: S
Question
Some number of bombs sit in a linear chamber, at time 0. At time 1, the bombs explode. Each sends one piece of shrapnel to the left and one piece of shrapnel to the right. At each successive time iteration, the pieces move at a constant speed on their due courses, passing through each other unimpeded, until all have left the chamber.! ! You are to create a NetHack-esque animation of this process. Given a String describing the initial locations of the bombs, and an Integer describing the bombs’ concussive power, you are to compute the locations of the pieces of shrapnel at each time iteration, terminating once all have left the chamber.! ! Your method will take as input a String bombs and an Integer force. bombs will have a "B" at each position containing a bomb, and a "." at each empty position. At time 1, the B’s disappear, sending one "<" piece of sharpnel to the left, and one ">" piece of shrapnel to the right, each moving at the constant speed force. If a "<" and ">" ever occupy the same position at the same time, they will be collectively represented by a single "X".! ! Your method will return an array of Strings in which each successive element shows the occupied locations at each time unit. The rst element should show the initial locations of the bombs, using "B" and "." characters. The last element should show the empty chamber at the rst time that it becomes empty.! ! (Please see the examples on the following page if this is unclear — it helps to see it in motion.)! ! Your method signature should conform to your chosen language’s variant of:! ! public String[] explode(String bombs, int force) ! explode should accept its input under the following constraints: • bombs should contain between 1 and 50 characters, inclusive.! • Each character in bombs should be either a "." or a “B”.! • force should be between 1 and 10, inclusive
Explanation / Answer
"""
'explode' produces an animation of exploding bombs.
"""
def explode(bombs, force):
"""
Given representation of chamber with bombs, creates an animation
of shrapnel as the bombs explode. Each bomb will produce
shrapnel moving left and right at constant speed, given by the 'force'
parameter. Left-moving shrapnel will appear as '<', right-moving shrapnel
as '>' and locations with shrapnel moving in both directions as 'X'.
@param bombs: A string of length 1 to 50 in which every character is
either a . or a B, the latter representing a bomb.
@param force: The speed at which shrapnel moves both to the left and
right.
@return: A list of strings, representing a unit of time, starting
with the initial input string and ending when the chamber is
empty.
"""
# Checking input parameters obey required constraints.
chamber_size = len(bombs)
if chamber_size < 1 or chamber_size > 50:
raise Exception("The length of 'bombs' is '%s'. The string must be 1 "
"to 50 characters long." % len(bombs))
if force < 1 or force > 10:
raise Exception("The value of force is '%s'. Only a force between 1 "
"and 10 inclusive is permitted." % force)
# Initializing the animation sequence to start with the input string.
animation = [bombs]
left, right = _initialize_shrapnel_locations(bombs)
chamber = None
EMPTY_CHAMBER = '.' * chamber_size
# Until the chamber is empty, we contine updating our animation.
while chamber != EMPTY_CHAMBER:
# Update the locations of the shrapnel.
left, right = _update_shrapnel(left, right, force)
# Build the updated picture of the chamber as a list.
chamber = []
for location in xrange(chamber_size):
if location in left:
chamber.append('X' if location in right else '<')
else:
chamber.append('>' if location in right else '.')
# Join chamber into a string and add it to animation.
chamber = "".join(chamber)
animation.append(chamber)
return animation
def _initialize_shrapnel_locations(bombs):
"""
Returns two sets of locations, representing locations of left-moving and
right-moving shrapnel, initialized to the positions of the bombs from
which they come.
@param bombs: A string comprised of '.' and 'B' characters, the 'B'
representing bombs, the locations of which in the string
are to be placed in left and right shrapnel sets.
@return: Two identical sets, each with the locations in the input string
of the bombs.
"""
left = set([])
right = set([])
for index, char in enumerate(bombs):
if char == 'B':
left.add(index)
right.add(index)
elif char != '.':
raise Exception("Improper string input 'bombs'.")
return left, right
def _update_shrapnel(left, right, force):
"""
Given two sets with locations of left and right moving shrapnel,
returns two sets with updated locations after they have moved
a distance given by the parameter 'force'.
"""
# Update the locations of left-moving shrapnel.
nextleft = set([])
for shrapnel in left:
nextleft.add(shrapnel - force)
# Update locations of right-moving shrapnel.
nextright = set([])
for shrapnel in right:
nextright.add(shrapnel + force)
return nextleft, nextright
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.