## Python Fundamentals - More Exercises #1 -- Problem 3. Wolf in Sheep's Clothing

### PROBLEM_3 ###

import re

input_str = input()

list_input = re.findall(r'\w+', input_str) ### convert input string into a list without the commas

list_reversed = list_input[::-1] ### sort the list in reverse order

counter_sheep = 0

if list_reversed[0] == 'wolf':

print('Please go away and stop eating my sheep')

else:

for i in range(1,len(list_reversed)):

if list_reversed[i] == 'sheep':

counter_sheep += 1

else:

print(f'Oi! Sheep number {counter_sheep}! You are about to be eaten by a wolf!')

Здравейте,

ето този мой опит да реша задачата ми дава максимум 60 от 100 точки в Judge, като причината е Time Limit.

Moже ли някой да разясни защо моят код е лош и бавен?

Поздрави, Илко

Тагове:
0
03/12/2020 09:25:21
Julianh12 2 Точки

Ето ти едно просто решение:)

```string=input().split(", ")
list=[]
list=string
n=len(list)

for i in (range(n)):

if list[n-1]=="wolf" :

print("Please go away and stop eating my sheep")
break
elif list[i]=="wolf" :
print(f"Oi! Sheep number {n-i-1}! You are about to be eaten by a wolf!")
break

```
0
l.markov 1 Точки

Ето още едно предложение за решение:

``````animals = input()

animals_list = []
for animal in animals.split(","):
animals_list.append(str(animal).strip())

farmer_position = len(animals_list) + 1

if animals_list[-1].strip() == "wolf":
print("Please go away and stop eating my sheep")
else:
animal_position = len(animals_list) - animals_list.index("wolf") - 1
print(f"Oi! Sheep number {animal_position}! You are about to be eaten by a wolf!")
``````

0