2023년 1학기 기출 풀이

이찬·2023년 8월 14일
0
post-thumbnail

Question


Answer

import time
from datetime import datetime

class Time:
# Problem1
  def __init__(self, hour, min, sec):
    self.hour = int(hour)
    self.min = int(min)
    self.sec = int(sec)
    self.time = [0,0,0]
    if (0 <= self.hour) and (self.hour < 24):
      self.time[0] = self.hour
    if (0 <= self.min) and (self.min < 60):
      self.time[1] = self.min
    if (0 <= self.sec) and (self.sec < 60):
      self.time[2] = self.sec
    self.orginalTime = self.time

# Problem2
  def getTimeTuple(self):
    return tuple(self.time)

# Problem3
  def getTimeString (self):
    self.strtime = ['0','0','0']
    if self.time[0] < 10 :
      self.strtime[0] = self.strtime[0] + str(self.time[0])
    else:
      self.strtime[0] = str(self.time[0])

    if self.time[1] < 10 :
      self.strtime[1] = self.strtime[1] + str(self.time[1])
    else:
      self.strtime[1] = str(self.time[1])

    if self.time[2] < 10 :
      self.strtime[2] = self.strtime[2] + str(self.time[2])
    else:
      self.strtime[2] = str(self.time[2])
    self.strstr = f"{self.strtime[0]}:{self.strtime[1]}:{self.strtime[2]}"
    return self.strstr

# Problem4
class Time_24_View (Time):
  def getViewString(self):
    return self.getTimeString()

# Problem5
  def getTimeString (self):
    super().getTimeString()
    return self.strstr

# Problem6
class Time_12_View (Time):
  def getViewString(self):
    self.strtime = ['0','0','0']
    self.q6AMPM = ""
    if self.time[0] < 10 :
      self.strtime[0] = self.strtime[0] + str(self.time[0])
    else:
      self.strtime[0] = str(self.time[0])

    if self.time[1] < 10 :
      self.strtime[1] = self.strtime[1] + str(self.time[1])
    else:
      self.strtime[1] = str(self.time[1])

    if self.time[2] < 10 :
      self.strtime[2] = self.strtime[2] + str(self.time[2])
    else:
      self.strtime[2] = str(self.time[2])

    if int(self.strtime[0]) > 12:
      a = str(int(self.strtime[0])- 12)
      if int(a) < 11:
        a = "0" + a
      self.strtime[0] = a
      self.strstr = f"{self.strtime[0]}:{self.strtime[1]}:{self.strtime[2]}"
      self.q6AMPM = self.strstr + " PM"

    else:
      self.strstr = f"{self.strtime[0]}:{self.strtime[1]}:{self.strtime[2]}"
      self.q6AMPM = self.strstr + " AM"

    return self.q6AMPM


# Problem7
  def getTimeString (self):
    super().getTimeString()
    strTime = self.strstr
    self.amPm = ""
    if int(self.strstr[0] + self.strstr[1]) < 13:
      self.amPm = str(self.strstr) + " AM"
    else:
      k = int(self.strstr[0] + self.strstr[1])-12
      self.strstr = f"{k}:{self.strstr[3:]}"
      self.amPm = str(self.strstr) + " PM"
    return self.amPm

# Problem8
class Clock:
  def __init__(self, objct):
    self.alarmTime = []  # 이게 init에 없고 12번 문제 메서드 안에 있어서 문제가 안 풀렸음!!
    self.remainTime = []
    self.objct = objct
    kk = Time_12_View(2,3,4)
    vv = Time_24_View(5,6,7)
    if not (type(self.objct) == type(kk) or type(self.objct) == type(vv)):
      self.objct = Time_24_View(0,0,0)

# Problem9
  def getClockTime(self):
    if type(self.objct) == type(Time_12_View(2,3,4)):
      a = self.objct.getViewString()

    elif type(self.objct) == type(Time_24_View(5,6,7)):
      a = self.objct.getViewString()
    return a

# Problem10
  def  changeClockTimeMode(self):
    if type(self.objct) == type(Time_12_View(2,3,4)):
       h = self.objct.hour
       m = self.objct.min
       s = self.objct.sec
       self.objct = Time_24_View(h,m,s)
       return self.objct.getViewString()

    elif type(self.objct) == type(Time_24_View(5,6,7)):
      h = self.objct.hour
      m = self.objct.min
      s = self.objct.sec
      self.objct = Time_12_View(h,m,s)
      return self.objct.getViewString()

# Problem11
  def setClockTime(self,strring):
    self.strring = strring
    if len(self.strring) == 11:
      h = self.strring[0:2]
      m = self.strring[3:5]
      s = self.strring[6:8]
      self.objct = Time_24_View(h,m,s)

    elif len(self.strring) == 8:
      h = self.strring[0:2]
      m = self.strring[3:5]
      s = self.strring[6:8]
      self.objct = Time_12_View(h,m,s)

    return self.objct.getViewString()

# Problem12
  def addAlarm(self, obj):
    self.obj = obj
    kk = Time_12_View(2,3,4)
    vv = Time_24_View(5,6,7)
    if not (type(self.obj) == type(kk) or type(self.obj) == type(vv)):
      pass
    else:
      lstt = []
      h = int(self.obj.hour)
      m = int(self.obj.min)
      s = int(self.obj.sec)
      lstt.append(h)
      lstt.append(m)
      lstt.append(s)
      self.alarmTime.append(lstt)
    return len(self.alarmTime)

# Problem13
  def getAlarmRemainsInSecondList(self):
    now = datetime.now() # datetime.datetime.now()..?
    strNow = str(now)
    hour = strNow[11:13]
    min = strNow[14:16]
    sec = strNow[17:19]
    for i in self.alarmTime:
      alarmSec = (3600 * int(i[0])) + (60 * int(i[1])) + (1 * int(i[2]))
      nowSec = (3600 * int(hour)) + (60 * int(min)) + (1 * int(sec))
      if alarmSec < nowSec:
        reslt = (24 *3600) - nowSec + alarmSec
        self.remainTime.append(reslt)
      elif alarmSec == nowSec:
        reslt = 0
        self.remainTime.append(reslt)
      else:
        reslt = alarmSec - nowSec
        self.remainTime.append(reslt)
    return self.remainTime

확인 코드

print("# 1")
aa = Time(2,13,25)
print(aa.time)
print("# 2")
print(aa.getTimeTuple())
print("# 3")
print(aa.getTimeString())
print("# 4")
bb = Time_24_View(15,35,46)
print(bb.getViewString())
print("# 5")
print(bb.getTimeString())
print("# 6")
cc = Time_12_View(23,41,53)
print(cc.getViewString())
print("# 7")
print("cc.getTimeString()")
print("# 8")
dd = Time_12_View(15,23,6)
ee = Clock(dd)
ff = Time_24_View(3,3,6)
pp = Time_12_View(1,37,36)
qq = Time_24_View(23,47,58)
gg = Clock(ff)
hh = 'any string'
ii = Clock(hh)
print("#9")
print(ee.getClockTime())
print(gg.getClockTime())
print(ii.getClockTime())
print("#10")
print(ee.changeClockTimeMode())
print(gg.changeClockTimeMode())
print(ii.changeClockTimeMode())
print("#11")
print(ee.setClockTime("02:11:25"))
print("#12")
print(ee.addAlarm(bb))
print(ee.addAlarm(cc))
print(ee.addAlarm(pp))
print(ee.addAlarm(qq))
print("#13")
print(ee.getAlarmRemainsInSecondList())
"""
# 1
[2, 13, 25]
# 2
(2, 13, 25)
# 3
02:13:25
# 4
15:35:46
# 5
15:35:46
# 6
11:41:53 PM
# 7
cc.getTimeString()
# 8
#9
03:23:06 PM
03:03:06
00:00:00
#10
15:23:06
03:03:06 AM
00:00:00 AM
#11
02:11:25 AM
#12
1
2
3
4
#13
[14355, 43522, 50465, 43887]
"""
profile
Kyunghee univ. IE 21

0개의 댓글