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