Skip to content

Instantly share code, notes, and snippets.

@rchatley
Created April 5, 2022 14:06
Show Gist options
  • Save rchatley/1f54e16571b85ad6de50c76b9a79336a to your computer and use it in GitHub Desktop.
Save rchatley/1f54e16571b85ad6de50c76b9a79336a to your computer and use it in GitHub Desktop.
LineEncoders
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
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