Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. """
  2. ZADANIE 6
  3. Napisz funkcję is_stressful. Sprawdza ona temat wiadomości, i na tej podstawie wyciąga wnioski.
  4. Program ma sprawdzać, czy dany temat jest stresujący dla odbiorcy, czy nie.
  5. Poniżej są określone wytyczne:
  6. - wszystkie litery są pisane jako wielkie
  7. - lub kończy się trzema wykrzyknikami
  8. - lub zawiera przynajmniej jedno z niżej wymienionych słów: "help", "asap", "urgent"] wymawianych w dowolny sposób,
  9. np. "HELP", "help", "HeLp", "H!E!L!P!", "H-E-L-P", również HHHEEEEEEEEELLP
  10.  
  11. assert is_stressful("Hi") is False, "First"
  12. assert is_stressful("I neeed HELP") is True, "Second"
  13. """
  14.  
  15.  
  16. def is_urgent_word(word: str) -> bool:
  17. """Funkcja do sprawdzenia czy pojedyncze słowo jest w liście urgent_words"""
  18. urgent_words = ["help", "asap", "urgent"]
  19. lowered_word = word.lower()
  20.  
  21. if lowered_word in urgent_words:
  22. return True
  23.  
  24. result = [lowered_word[0]]
  25. for i, letter in enumerate(lowered_word):
  26. if i == 0:
  27. continue
  28. if letter != lowered_word[i - 1] and letter.isalpha(): # jeżeli poprzednia litera jest różna od obecnie sprawdzanej
  29. result.append(letter) # do listy wynikowej dopisz obecnie sprawdzaną literę, unikamy duplikatów
  30. word = ''.join(result)
  31. return True if word in urgent_words else False
  32.  
  33.  
  34. # True, False
  35. def is_stressful(message_subject: str) -> bool:
  36. words = message_subject.split(sep=' ')
  37. for word in words:
  38. if word.isupper():
  39. return True
  40. elif word.endswith('!!!'):
  41. return True
  42. elif is_urgent_word(word):
  43. return True
  44. return False
  45.  
  46.  
  47. if __name__ == '__main__':
  48. # assert is_stressful("Hi") is False, "First"
  49. # assert is_stressful("Hi!!!") is True, 'Konczy się na trzy wykrzykniki'
  50. assert is_stressful("i neeed H-e-L-P") is True, "Bo help jest w zdaniu"
  51. assert is_stressful("i neeed HELP") is True, "Help w zdaniu"
  52. assert is_stressful("UuuuuuuurgenT") is True, "Inny zapis urgent"
  53. assert is_stressful("aSaP True") is True, "Asap zapisane w inny sposób"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement