Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Text Editor by Sim0000
class State:
def __init__(self, font, text):
self.font = font
self.text = text
class Text:
def __init__(self):
self.text = ''
self.font = None
def set_font(self, font):
self.font = font
def write(self, text):
self.text += text
def create_state(self):
return State(self.font, self.text)
def restore(self, state):
self.font = state.font
self.text = state.text
def show(self):
font = '' if self.font is None else f'[{self.font}]'
return f"{font}{self.text}{font}"
class SavedText:
def __init__(self):
self.log = []
def save_text(self, text):
state = text.create_state()
self.log.append(state)
def get_version(self, version):
return self.log[version]
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
text = Text()
saver = SavedText()
text.write("At the very beginning ")
saver.save_text(text)
text.set_font("Arial")
saver.save_text(text)
text.write("there was nothing.")
assert text.show() == "[Arial]At the very beginning there was nothing.[Arial]"
text.restore(saver.get_version(0))
assert text.show() == "At the very beginning "
print("Coding complete? Let's try tests!")
June 24, 2018
Comments: