Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Text Editor by k_dutch
from copy import copy
class Text:
def __init__(self):
self.font = None
self.text = []
def write(self, text):
self.text.append(text)
def set_font(self, font):
self.font = font
def show(self):
text = ''.join(self.text)
if self.font is not None:
return '[{0}]{1}[{0}]'.format(self.font, text)
return text
def restore(self, version):
self.text = version[0]
self.font = version[1]
class SavedText:
def __init__(self):
self.versions = []
def save_text(self, text):
version = (copy(text.text), text.font)
self.versions.append(version)
def get_version(self, number):
return self.versions[number]
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!")
Aug. 22, 2018
Comments: