Wolves have been reintroduced to Great Britain. You are a sheep farmer, and are now plagued by wolves which pretend to be sheep. Fortunately, you are good at spotting them.
Warn the sheep in front of the wolf that it is about to be eaten. Remember that you are standing at the front of the queue which is at the end of the array:
[sheep, sheep, sheep, sheep, sheep, wolf, sheep, sheep] (YOU ARE HERE AT THE FRONT OF THE QUEUE) 7 6 5 4 3 2 1 If the wolf is the closest animal to you, return "Pls go away and stop eating my sheep". Otherwise, return "Oi! Sheep number N! You are about to be eaten by a wolf!" where N is the sheep's position in the queue.
Note: there will always be exactly one wolf in the array.
Examples Input: ["sheep", "sheep", "sheep", "wolf", "sheep"] Output: "Oi! Sheep number 1! You are about to be eaten by a wolf!"
Input: ["sheep", "sheep", "wolf"] Output: "Pls go away and stop eating my sheep"
Solution:
def warn_the_sheep(queue):
number = len(queue) - queue.index("wolf") - 1
return number\
and f"Oi! Sheep number {number}! You are about to be eaten by a wolf!"\
or "Pls go away and stop eating my sheep"\
Other Solution:
def warn_the_sheep(queue):
i = queue[::-1].index('wolf')
if i == 0:
return 'Pls go away and stop eating my sheep'
return f'Oi! Sheep number {i}! You are about to be eaten by a wolf!'
'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글
0828. Basic Mathematical Operations (0) | 2022.08.28 |
---|---|
0827. Sum Arrays (0) | 2022.08.27 |
0825. Find the first non-consecutive number (0) | 2022.08.25 |
0824. N-th Power (0) | 2022.08.24 |
Alternate capitalization (0) | 2022.08.23 |