Please write in Python! ! !
Python 3.6 ! ! 1
Thanks!
The third lock consists of a stone with a set of irregular holes carved out of it. Lin realises that an oddly-shaped gem found elsewhere in the cave that might fit this lock. Your task is to write a function third_lock(key, stone) that determines whether the "key" gem will fit the pattern of holes in the "stone" lock. If the key is smaller than the stone, then the key can be moved to any location relative to the stone, so long as it doesn't extend beyond the edge of the stone in any direction. The key can also be rotated to any of four possible orientations: (N)orth (with the top of the key facing up), (E)ast (with the key rotated 90 degree clockwise such that the top faces to the right), (S)outh (with the key rotated 180 degrees clockwise such that the top faces downwards), or (W)est (with the key rotated 270 degrees clockwise such that the top faces to the left). The key can only be moved or rotated, it cannot be flipped over. Your function should take as input two nested lists describing the shape of the key (which parts are "raised") and the shape of the stone (which parts contain "holes" that raised parts of the key fit within). Your function should return a tuple specifying the location (in terms of row-index and column-index) and orientation of the key if it will fit, or None if the key will not fit for any location or orientation. Note that while the key will only open the lock if every raised part fits within a hole in the stone, it is not necessary for every hole in the stone needs to be matched by a raised part of the key. If a key will fit a stone in multiple orientations and/or locations, then your function should return the location that is closest to the top of the stone. If there are multiple such locations then your function should return the location that is closest to the left of the stone. If the key at this location will fit in multiple orientations, then your function should return the orientation that requires the smallest clockwise rotation. For example, if the key would fit at a particular location when oriented both (E)ast and (S)outh, then your function should return (East, as that only entails a 90, rather than 180, degree clockwise rotation.
Assumptions: • You may assume that the width and height of both the key and the stone are greater than zero. • You may assume that the key and the stone are both rectangular (including square; ie width == height). • You may assume that the initial orientation of the key (ie, as specified by the input) is (N)orth. • The entirety of the key must fit within the stone, even parts that are not raised Example: In a nested list describing a key,*denotes a raised part of the key while '.' denotes blank space. In a nested list describing a stone,'#' denotes a solid part of the stone while '.' denotes a hole in the stone. Thus the key can only be used to unlock the door if all of the raised parts of the key (* ) line up with holes in the stone (':') (for some location and orientation). The following shows a simple representation of two keys and three stones, together with the corresponding nested list representation: Key 1 key1 = [['*', '*'], ['*', '.']] Key 2 key2 [['.', '*'], ['*', '.'], ['.', '*']]
Stone 1 #. stone1 = [[':', '.', ','], ['.', '#',':'], ['.', Stone 2 #.. ##. # stone2 = [['#',' 1 .'], ['#','#', '.'], ['.', Stone 3 ##. #. #.# stone3 = [['#', '#', '.'], ['.', '#','.'], ['#', '.' Example calls: >>> print(third_lock(key1, stonel)) (0, 0, 'N') >>> print(third_lock(key2, stonen)) (0, 0, 'N') >>> print(third_lock(key1, stone2)) (0, 1, 'E') >>> print(third_lock (key2, stone2)) (0, 1, 'S') >>> print(third_lock(key1, stone3)) None >>> print(third_lock (key2, stone3)) (1, 0, 'W')
In the first two examples, both keys 1 and 2 fit the stone 1 in their original location (0,0) and orientation, (N)orth. In the third example (key 1 and stone 2) the key needs to be rotate once to be oriented (E)ast, and also moved one location to the right (0, 1) in order to fit. In the fourth example (key 2 and stone 2) the key needs to be rotated twice to be oriented (South, and also moved one location to the right (0, 1) in order to fit. In the fifth example, (key 1 and stone 3) there is no location or orientation for wich key 1 will fit the stone. In the sixth example, (key 2 and stone 3) the key needs to be rotated three times to be oriented (W)est, and also moved one location down (1, 0) in order to fit.
The third lock consists of a stone with a set of irregular holes carved out of it. Lin realises that an oddly-shaped gem
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
The third lock consists of a stone with a set of irregular holes carved out of it. Lin realises that an oddly-shaped gem
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!