Python - Wolf in Sheep's Clothing - More Exercises: Basic Syntax, Conditional Statements and Loops
Здравейте,
Може ли идеи/решения за тази задача:
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, wolf, sheep, sheep] (YOU ARE HERE AT THE FRONT OF THE QUEUE)
4 3 2 1
If the wolf is the closest animal to you, print "Please 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.
Input
The input will be a single string containing the animals separated by comma and a single space ", "
Examples
Input |
Output |
sheep, sheep, wolf |
Please go away and stop eating my sheep |
wolf, sheep, sheep, sheep, sheep, sheep |
Oi! Sheep number 5! You are about to be eaten by a wolf! |
Не мога да я измисля. След дълго ровене в интернет успях да достигна до следното:
def warn_the_sheep(queue): queue.reverse() the_wolf_position = queue.index("wolf") if the_wolf_position == 0: return 'Pls go away and stop eating my sheep' else: return "Oi! Sheep number " + str(the_wolf_position) + "! You are about to be eaten by a wolf!"
но не разбирам как точно работи, а и не получавам никакви резултати в PyCharm.
Ето още едно решение без цикъл. :)
Страхотно решение на задачата, поздравление!