Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Text Editor by vnkvstnk
class SavedText:
def __init__(self):
self.versions = []
def save_text(self, text):
self.versions.append((text.current_text, text.font))
def get_version(self, number):
return self.versions[number]
class Text:
def __init__(self):
self.current_text = ""
self.font = ""
def write(self, text):
self.current_text += text
def set_font(self, font):
self.font = "[" + font + "]" if font else ""
def show(self):
return self.font + self.current_text + self.font
def restore(self, saved_text):
self.current_text, self.font = saved_text
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!")
Sept. 29, 2019
Comments: