fkudinov

8. Backward Each Word / Вирішуємо задачі на Python CheckIO Українською

Sep 1st, 2023
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.40 KB | Source Code | 0 0
  1. # --------- tasks/backward_words.py  ------------
  2.  
  3.  
  4. def backward_string_by_word(text: str) -> str:
  5.  
  6.     words = text.split(" ")
  7.  
  8.     res = []
  9.     for word in words:
  10.         word = reverse(word)
  11.         res.append(word)
  12.  
  13.     return " ".join(res)
  14.  
  15.  
  16. def reverse(text: str) -> str:
  17.     return text[::-1]
  18.  
  19.  
  20. def backward_string_by_word_gen(text: str) -> str:
  21.     return " ".join(reverse(word) for word in text.split(" "))
  22.  
  23.  
  24. def backward_string_by_word_map(text: str) -> str:
  25.     return " ".join(map(reverse, text.split(" ")))
  26.  
  27.  
  28. # ---------      tests/test_backward_words.py   ----------------
  29.  
  30.  
  31. import pytest
  32.  
  33. from tasks.backward_words import (backward_string_by_word, reverse,
  34.                                   backward_string_by_word_gen,
  35.                                   backward_string_by_word_map)
  36.  
  37.  
  38. @pytest.mark.parametrize("callable", [backward_string_by_word,
  39.                                       backward_string_by_word_gen,
  40.                                       backward_string_by_word_map])
  41. @pytest.mark.parametrize(["text", "result"], [
  42.     ['', ''],
  43.     ['world', 'dlrow'],
  44.     ['hello world', 'olleh dlrow'],
  45.     ['hello   world', 'olleh   dlrow'],
  46.     ['welcome to a game', 'emoclew ot a emag'],
  47. ])
  48. def test_backward_words(callable, text, result):
  49.     assert callable(text) == result
  50.  
  51.  
  52. def test_reverse():
  53.     assert reverse("123") == "321"
  54.     assert reverse("") == ""
  55.  
Tags: checkio
Advertisement
Add Comment
Please, Sign In to add comment