Created
April 5, 2022 14:06
-
-
Save rchatley/1f54e16571b85ad6de50c76b9a79336a to your computer and use it in GitHub Desktop.
LineEncoders
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import abc | |
class LineEncoder: | |
def __init__(self, algorithm): | |
self.algorithm = algorithm | |
def encode(self, text): | |
words = text.split(" ") | |
result = [] | |
for word in words: | |
result.append(self.algorithm.encode_word(word)) | |
return " ".join(result) | |
class Reverser: | |
def encode_word(self, word): | |
return word[::-1] | |
class Doubler: | |
def encode_word(self, word): | |
return word + word |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from encoders import Reverser, Doubler, LineEncoder | |
def test_can_reverse_a_single_word(): | |
assert LineEncoder(Reverser()).encode("abcd") == "dcba" | |
def test_can_reverse_each_word_in_line(): | |
assert LineEncoder(Reverser()).encode("abcd 123") == "dcba 321" | |
def test_can_double_a_single_word(): | |
assert LineEncoder(Doubler()).encode("abcd") == "abcdabcd" | |
def test_can_double_each_word_in_line(): | |
assert LineEncoder(Doubler()).encode("abcd 123") == "abcdabcd 123123" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment