---
# System prepended metadata

title: Python Challenge Day 9 - A wolf in sheeps clothing

---

---
title: 'Python Challenge Day 9 - A wolf in sheeps clothing'
disqus: hackmd
---


## Python Challenge Day 9 - A wolf in sheep's clothing
### DESCRIPTION

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:
```gherkin=
[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:
```gherkin=
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
---
目的：如果狼是離你最近的動物，返回“Pls go away and stop eating my sheep”。 否則返回“Oi! Sheep number N! You are about to be eaten by a wolf!” 其中 N 是羊在隊列中的位置。
注意：您站在隊列的最前面，也就是數組的末尾，數組中總是只有一隻狼。

#### Practice 1:
``` python
def warn_the_sheep(queue):
    n = len(queue) - queue.index('wolf') - 1
    if n < 1:
        return 'Pls go away and stop eating my sheep' 
    else:
        return f'Oi! Sheep number {n}! You are about to be eaten by a wolf!'
        
```
* 一個元素(wolf)存放到陣列之後，不論是在陣列的哪個地方，只要利用索引值（ index ），就能找到元素。
* 用 len(queue)取得queue長度（幾個元素）。
例如：[羊,羊,狼,羊] 
元素位置：[0,1,2] 程式語言中的 index 是從 0 開始的。所以如果想要取得第一個位置的元素，要輸入的位置值會是 0 。
n=4-2-1，減1是為因為要求靠近狼的羊的位置。

#### Practice 2:
``` python
def warn_the_sheep(queue):
    n = len(queue) - queue.index('wolf') - 1
    return f'Oi! Sheep number {n}! You are about to be eaten by a wolf!' if n else 'Pls go away and stop eating my sheep'
```


## 參考資源

:::info
* [Kata](https://www.codewars.com/kata/5c8bfa44b9d1192e1ebd3d15)
:::


