Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- ZADANIE 6
- Napisz funkcję is_stressful. Sprawdza ona temat wiadomości, i na tej podstawie wyciąga wnioski.
- Program ma sprawdzać, czy dany temat jest stresujący dla odbiorcy, czy nie.
- Poniżej są określone wytyczne:
- - wszystkie litery są pisane jako wielkie
- - lub kończy się trzema wykrzyknikami
- - lub zawiera przynajmniej jedno z niżej wymienionych słów: "help", "asap", "urgent"] wymawianych w dowolny sposób,
- np. "HELP", "help", "HeLp", "H!E!L!P!", "H-E-L-P", również HHHEEEEEEEEELLP
- assert is_stressful("Hi") is False, "First"
- assert is_stressful("I neeed HELP") is True, "Second"
- """
- def is_urgent_word(word: str) -> bool:
- """Funkcja do sprawdzenia czy pojedyncze słowo jest w liście urgent_words"""
- urgent_words = ["help", "asap", "urgent"]
- lowered_word = word.lower()
- if lowered_word in urgent_words:
- return True
- result = [lowered_word[0]]
- for i, letter in enumerate(lowered_word):
- if i == 0:
- continue
- if letter != lowered_word[i - 1] and letter.isalpha(): # jeżeli poprzednia litera jest różna od obecnie sprawdzanej
- result.append(letter) # do listy wynikowej dopisz obecnie sprawdzaną literę, unikamy duplikatów
- word = ''.join(result)
- return True if word in urgent_words else False
- # True, False
- def is_stressful(message_subject: str) -> bool:
- words = message_subject.split(sep=' ')
- for word in words:
- if word.isupper():
- return True
- elif word.endswith('!!!'):
- return True
- elif is_urgent_word(word):
- return True
- return False
- if __name__ == '__main__':
- # assert is_stressful("Hi") is False, "First"
- # assert is_stressful("Hi!!!") is True, 'Konczy się na trzy wykrzykniki'
- assert is_stressful("i neeed H-e-L-P") is True, "Bo help jest w zdaniu"
- assert is_stressful("i neeed HELP") is True, "Help w zdaniu"
- assert is_stressful("UuuuuuuurgenT") is True, "Inny zapis urgent"
- assert is_stressful("aSaP True") is True, "Asap zapisane w inny sposób"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement