Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Text Editor by neshta85
class Text:
def __init__(self):
self.text = ""
self.font = ""
def write(self, text):
if len(text) > 0:
self.text += text
def set_font(self, font):
if len(font) > 0:
self.font = "[{0}]".format(font)
def show(self):
return "{0}{1}{0}".format(self.font, self.text)
def restore(self, version):
self.text = version['cont']
self.font = version['font']
class SavedText:
def __init__(self):
self.version = []
def save_text(self, text):
self.version.append({'cont': text.text, 'font': text.font})
def get_version(self, index):
return self.version[index]
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!")
Jan. 11, 2019