Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # --------- tasks/backward_words.py ------------
- def backward_string_by_word(text: str) -> str:
- words = text.split(" ")
- res = []
- for word in words:
- word = reverse(word)
- res.append(word)
- return " ".join(res)
- def reverse(text: str) -> str:
- return text[::-1]
- def backward_string_by_word_gen(text: str) -> str:
- return " ".join(reverse(word) for word in text.split(" "))
- def backward_string_by_word_map(text: str) -> str:
- return " ".join(map(reverse, text.split(" ")))
- # --------- tests/test_backward_words.py ----------------
- import pytest
- from tasks.backward_words import (backward_string_by_word, reverse,
- backward_string_by_word_gen,
- backward_string_by_word_map)
- @pytest.mark.parametrize("callable", [backward_string_by_word,
- backward_string_by_word_gen,
- backward_string_by_word_map])
- @pytest.mark.parametrize(["text", "result"], [
- ['', ''],
- ['world', 'dlrow'],
- ['hello world', 'olleh dlrow'],
- ['hello world', 'olleh dlrow'],
- ['welcome to a game', 'emoclew ot a emag'],
- ])
- def test_backward_words(callable, text, result):
- assert callable(text) == result
- def test_reverse():
- assert reverse("123") == "321"
- assert reverse("") == ""
Advertisement
Add Comment
Please, Sign In to add comment