본문 바로가기

Python_Matter/Solve

Python Learn the basics Quiz 80

반응형

Q>

I believe that many of you have dealt with such a problem.

(나는 당신들 중 많은 사람들이 그런 문제를 다루었다고 믿습니다.)

One day you are working in the text editor, saving the document and closing it.

(언젠가는 텍스트 편집기에서 문서를 저장하고 닫는 작업을하고 있습니다.)

And the next day you are re-reading the text and realizing that one of the previous versions was better but there is no way to get it back.

(그리고 그 다음날 당신은 텍스트를 다시 읽고 이전 버전 중 하나가 더 좋았다는 것을 깨달았지만 그것을 되돌릴 방법이 없습니다.)

This thing can be easily handled by the version control system (for example, git), but it’s used mostly by the developers and not the ordinary people who work with texts.

(이 것은 버전 제어 시스템 (예 : git)에서 쉽게 처리 할 수 ​​있지만 개발자가 주로 사용하고 텍스트로 작업하는 일반 사용자는 사용하지 않습니다.)

In this mission you’ll help the latter by creating a text editor prototype that supports the version control system, which will allow to save different versions of the text and restore any one of them.

(이 미션에서 버전 관리 시스템을 지원하는 텍스트 편집기 프로토 타입을 작성하여 텍스트의 다른 버전을 저장하고 그 중 하나를 복원 할 수 있습니다.)
Your task is to create 2 classes: Text and SavedText. The first will works with texts (adding, font changing, etc.), the second will control the versions and save them.
(귀하의 작업은 텍스트와 SavedText의 두 클래스를 만드는 것입니다. 첫 번째는 텍스트 (추가, 글꼴 변경 등)와 함께 작동하고, 두 번째 버전은 버전을 제어하고 저장합니다.)
Class Text should have the next methods:

(클래스 텍스트는 다음 메소드를 가져야합니다.)
write(text) - adds (text) to the current text;

(쓰기 (텍스트) - 현재 텍스트에 (텍스트)를 추가합니다.)
set_font(font name) - sets the chosen font. Font is applied to the whole text, even if it’s added after the font is set.

(set_font (글꼴 이름) - 선택한 글꼴을 설정합니다. 글꼴은 글꼴이 설정된 후에 추가 된 경우에도 전체 텍스트에 적용됩니다.)

The font is displayed in the square brackets before and after the text: "[Arial]...example...[Arial]".

(글꼴은 "[Arial] ... example ... [Arial]"텍스트 앞뒤에 대괄호로 표시됩니다.)

Font can be specified multiple times but only the last variant is displays;

(글꼴은 여러 번 지정할 수 있지만 마지막 변형 만 표시됩니다. )
show() - returns the current text and font (if is was set);

(show () - 현재 텍스트와 글꼴을 반환합니다 (설정된 경우).)
restore(SavedText.get_version(number)) - restores the text of the chosen version.
(restore (SavedText.get_version (number)) - 선택한 버전의 텍스트를 복원합니다.)
Class SavedText should have the next methods:

(SavedText 클래스에는 다음 메서드가 있어야합니다.)
save_text(Text) - saves the current text and font. The first saved version has the number 0, the second - 1, and so on;

(save_text (Text) - 현재 텍스트와 글꼴을 저장합니다. 첫 번째 저장된 버전은 숫자 0, 두 번째 -1 등이 있습니다.)
get_version(number) - this method works with the 'restore' method and is used for choosing the needed version of the text.
(get_version (number) -이 메서드는 'restore'메서드와 함께 작동하며 필요한 텍스트 버전을 선택하는 데 사용됩니다.)
In this mission you can use the Memento design pattern.

(이 임무에서 Memento 디자인 패턴을 사용할 수 있습니다.)

 

Example:

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.")
text.show() == "[Arial]At the very beginning there was nothing.[Arial]"
    
text.restore(saver.get_version(0))
text.show() == "At the very beginning "

 

Input: information about the text and saved copies.

         (텍스트 및 저장된 사본에 대한 정보.)

 

Output: the text after all of the commands.

           (모든 명령 뒤의 텍스트.)

 

How it is used: To save the object’s previous states with the ability to return to them, in case something goes wrong.

                     (개체의 이전 상태를 반환 할 수있는 기능이 있습니다.)

 

Precondition: No more than 10 saved copies.

                   (저장된 사본이 10 개를 넘지 않아야합니다.)

 

A>

class Text:
    text = ''
    font = ''

    def write(self, text):
        self.text += text

    def restore(self, old):
        self.text, self.font = old

    def set_font(self, font):
        self.font = f'[{font}]'

    def show(self):
        return f'{self.font}{self.text}{self.font}'

class SavedText(list):
    get_version = list.__getitem__

    def save_text(self, text):
        self.append((text.text, text.font))


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!")

 

O>

Coding complete? Let's try tests!

Process finished with exit code 0

 

S>

https://py.checkio.org

반응형

'Python_Matter > Solve' 카테고리의 다른 글

Python Learn the basics Quiz 82  (0) 2019.06.21
Python Learn the basics Quiz 81  (0) 2019.06.20
Python Learn the basics Quiz 79  (0) 2019.06.20
Python Learn the basics Quiz 78  (0) 2019.06.20
Python Learn the basics Quiz 77  (0) 2019.06.20