본문 바로가기

Python_Matter/Solve

Python Learn the basics Quiz 101

반응형

Q>

There is a lunch place at your work with the 3 microwave ovens (Мicrowave1, Мicrowave2, Мicrowave3), which are the subclasses of the MicrowaveBase class.

(MicrowaveBase 클래스의 하위 클래스 인 세 개의 전자 레인지 (전자 레인지 1, 전자 레인지 2, 전자 레인지 3)가 있습니다.)

Every microwave can be controlled by a RemoteControl. The RemoteControl uses the next commands:

(모든 전자 레인지는 RemoteControl로 제어 할 수 있습니다. RemoteControl은 다음 명령을 사용합니다. )

set_time("xx:xx"), where "xx:xx" - time in minutes and seconds, which shows how long the food will be warming up. For example: set_time("05:30");

(set_time ( "xx : xx"), 여기서 "xx : xx"- 음식이 워밍업되는 시간을 나타내는 분과 초 단위의 시간. 예를 들면 다음과 같습니다. set_time ( "05:30"); )
add_time("Ns"), add_time("Nm"), where N - the number of seconds("s") or minutes("m"), which should be added to the current time;

(add_time ( "Ns"), add_time ( "Nm"). N은 현재 시간에 추가되어야하는 초 ( "s") 또는 분 ( "m")의 숫자입니다. )
del_time("Ns"), del_time("Nm"), where N - the amount of the seconds("s") or minutes("m"), which should be subtracted from the current time;

(del_time ( "Ns"), del_time ( "Nm"), N - 현재 시간에서 뺀 초 ( "s") 또는 분 ( "m")의 양. )
show_time() - shows the current time for the microwave.

(show_time () - 전자 레인지의 현재 시간을 보여줍니다. )

The default time is 00:00.

(기본 시간은 00:00입니다.)

The time can't be less than 00:00 or greater than 90:00, even if add_time or del_time causes it. 
(add_time 또는 del_time이 발생하더라도 시간은 00:00보다 작거나 90:00보다 클 수 없습니다. )
Your task is to create a few classes - one for the MicrowaveBase, three for the three different microwaves, and one for the RemoteControl - just as described above.

(당신의 임무는 몇 가지 클래스를 만드는 것입니다 - 하나는 MicrowaveBase 용, 다른 하나는 Microwaves 용, 다른 하나는 RemoteControl 용입니다.)

The RemoteControl will have 1 parameter - an instance of the Microwave (for example, RemoteControl(microwave_1), where microwave_1 = Microwave1()) that shows which Microwave should be controlled by the current RemoteControle.

(RemoteControl에는 Microwave 인스턴스 (예 : RemoteControl (microwave_1), 여기서 microwave_1 = Microwave1 ()) 매개 변수가 있습니다.)

Only the third oven works properly and shows the full time.

(세 번째 오븐 만 제대로 작동하고 전체 시간을 보여줍니다.)

The other two have some flaws with their displays - the first shows '_' instead of the first digit and the second does the same with the last digit. They show time like this:

(나머지 2 개는 디스플레이에 결함이 있습니다. 첫 번째 숫자는 첫 번째 숫자 대신 '_'을 표시하고 두 번째 숫자는 마지막 숫자와 동일합니다. 그들은 다음과 같은 시간을 보여줍니다 :)

microwave_1 = Microwave1()
microwave_2 = Microwave2()
microwave_3 = Microwave3()

RemoteControl(microwave_1).show_time() == "_0:00"
RemoteControl(microwave_2).show_time() == "00:0_"
RemoteControl(microwave_3).show_time() == "00:00"

In this mission you could use the Bridge design pattern.

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

Its main task is - to decouple an abstraction from its implementation so that the two can vary independently.

(그것의 주된 임무는 추상화를 구현과 분리하여 두 가지가 독립적으로 달라질 수 있도록하는 것입니다.)

 

Example:

microwave_1 = Microwave1()
microwave_2 = Microwave2()
microwave_3 = Microwave3()

remote_control_1 = RemoteControl(microwave_1)
remote_control_1.set_time("01:00")

remote_control_2 = RemoteControl(microwave_2)
remote_control_2.add_time("90s")
    
remote_control_3 = RemoteControl(microwave_3)
remote_control_3.del_time("300s")
remote_control_3.add_time("100s")
    
remote_control_1.show_time() == "_1:00"
remote_control_2.show_time() == "01:3_"
remote_control_3.show_time() == "01:40"

 

Input: methods of the RemoteControl class and time.

         (RemoteControl 클래스 및 시간의 메서드)

 

Output: time on the display of the microwave.

           (전자 레인지 디스플레이의 시간.)

 

How it is used: For work with time.

                     (시간에 따른 작업.)

 

Precondition: 00:00 <= time <= 90:00

 

A>

'''
set_time("xx:xx"), where "xx:xx" - time in minutes and seconds, which shows how long the food will be warming up.
                                   For example: set_time("05:30");
add_time("Ns"), add_time("Nm"), where N - the number of seconds("s") or minutes("m"),
                                which should be added to the current time;
del_time("Ns"), del_time("Nm"), where N - the amount of the seconds("s") or minutes("m"),
                                          which should be subtracted from the current time;
show_time() - shows the current time for the microwave.
'''


def to_seconds(time):
    if time[-1] == 's':
        return int(time[:-1])
    elif time[-1] == 'm':
        return int(time[:-1]) * 60
    min, sec = time.split(':')
    return int(min) * 60 + int(sec)

def from_seconds(seconds):
    min, sec = divmod(seconds, 60)
    return '{:02d}:{:02d}'.format(min, sec)

class MicrowaveBase:
    def show_time(self, seconds):
        pass

class Microwave1(MicrowaveBase):
    def show_time(self, seconds):
        time = from_seconds(seconds)
        return '_' + time[1:]

class Microwave2(MicrowaveBase):
    def show_time(self, seconds):
        time = from_seconds(seconds)
        return time[:-1] + '_'

class Microwave3(MicrowaveBase):
    def show_time(self, seconds):
        return from_seconds(seconds)

class RemoteControl(object):
    def __init__(self, microwave):
        self.seconds = 0
        self.microwave = microwave
    def set_time(self, time):
        seconds = to_seconds(time)
        if 0 <= seconds <= 5400:
            self.seconds = seconds
    def add_time(self, time):
        self.seconds = min(self.seconds + to_seconds(time), 5400)
    def del_time(self, time):
        self.seconds = max(self.seconds - to_seconds(time), 0)
    def show_time(self):
        return self.microwave.show_time(self.seconds)

if __name__ == '__main__':
    # These "asserts" using only for self-checking and not necessary for auto-testing

    microwave_1 = Microwave1()
    microwave_2 = Microwave2()
    microwave_3 = Microwave3()

    remote_control_1 = RemoteControl(microwave_1)
    remote_control_1.set_time("01:00")

    remote_control_2 = RemoteControl(microwave_2)
    remote_control_2.add_time("90s")

    remote_control_3 = RemoteControl(microwave_3)
    remote_control_3.del_time("300s")
    remote_control_3.add_time("100s")

    assert remote_control_1.show_time() == "_1:00"
    assert remote_control_2.show_time() == "01:3_"
    assert remote_control_3.show_time() == "01:40"
    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' 카테고리의 다른 글