2020년 1학기 기출 풀이

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

Question


Answer code

import csv

class RemoteControl:
  def __init__(self):
    self.enabledChannelList = []
    self.nowChannel = 0
    self.blockedChannelList = []
    self.forG = []

  
  def powerOnRemoteControl (self, channelNumChannelName):
    self.channelNumChannelName = channelNumChannelName
    self.nowChannel = channelNumChannelName[0][0]
    for i in range(len(self.channelNumChannelName)):
      k = self.channelNumChannelName[i][0]
      self.enabledChannelList.append(k)
    lenChannel = len(self.channelNumChannelName)
    return lenChannel

  def gotoChannel(self, hopeChangeChannel):
    self.hopeChangeChannel = int(hopeChangeChannel)
    if self.hopeChangeChannel in self.enabledChannelList:
      self.nowChannel = self.hopeChangeChannel
      return self.nowChannel
    else:
      return self.nowChannel
    
  def nextChannel(self):
    k = self.enabledChannelList.index(self.nowChannel)
    if k == (len(self.enabledChannelList) - 1):
      self.nowChannel = self.enabledChannelList[0]
      return self.channelNumChannelName[0][1]

    else:
      self.nowChannel = self.channelNumChannelName[k+1][0]
      return self.channelNumChannelName[k+1][1]
  
  def previousChannel(self):
    self.originalIndex = self.enabledChannelList.index(self.nowChannel)
    if self.originalIndex == 0:
      self.nowChannel = self.enabledChannelList[-1]
      return self.channelNumChannelName[-1][1]
    else:
      self.nowChannel = self.enabledChannelList[self.originalIndex-1]
      return self.channelNumChannelName[self.originalIndex-1][1]

  def blockChannel(self):
    self.originalNum = self.nowChannel
    self.originalList = self.enabledChannelList
    self.indx = self.enabledChannelList.index(self.originalNum)
    self.enabledChannel = self.enabledChannelList.pop(self.indx)
    self.blockedChannelList.append(self.enabledChannel)
    self.afterNowNum = self.enabledChannelList[self.indx]
    self.nowChannel = self.afterNowNum
    for i in self.channelNumChannelName:
      if i[0] == self.afterNowNum:
        retrn = i[1]
    return retrn

  def unblockChannel(self, HopeReinsertNum):
    self.HopeReinsertNum = int(HopeReinsertNum)
    if self.HopeReinsertNum in self.blockedChannelList:
      self.enabledChannelList.insert(self.indx, self.HopeReinsertNum)
      self.blockedChannelList.remove(self.HopeReinsertNum)
      self.nowChannel = self.HopeReinsertNum
      return 1
    else:
      return -1

  def powerOffRemoteControl(self):
    f = open('openoutput.csv','w')
    writer = csv.writer(f)
    for i in range(len(self.channelNumChannelName)):
      if self.channelNumChannelName[i][0] in self.enabledChannelList:
        self.forG.append(self.channelNumChannelName[i])
    writer.writerows(self.forG)
    f.close()

  def favorChannel(self):
    self.favor = dict()
    if self.nowChannel not in list(self.favor.keys()):
      self.favor[self.nowChannel] = 1
      return 1
    else:
      self.favor[self.nowChannel] = int(self.favor[self.nowChannel]) + 1
      return 1
    return -1

  def aiNextChannel(self):
    self.favorNum = self.favor[self.nowChannel]
    self.lstd = {}
    for i in range(len(self.favor)):
      if list(self.favor.values())[i] < int(self.favorNum):
        self.lstd[list(self.favor.values())[i]] = list(self.favor.keys())[i]
    if len(self.lstd) > 1:
      kkkk = self.nowChannel
      a = list(self.lstd.keys())[0]
      self.nowChannel = a
      return kkkk

    elif len(self.lstd) == 1:
      kkkk = self.nowChannel
      self.nowChannel = list(self.lstd.keys())[0]
      return  kkkk
    else:
      kkkk = self.nowChannel
      favorlist = list(self.favor.values())
      maxx = max(favorlist)
      aaa = favorlist.index(maxx)
      self.nowChannel = list(self.favor.keys())[aaa]
      return kkkk 

  def aiPreviousChannel(self):
    self.favorNum = self.favor[self.nowChannel]
    self.lstd = {}
    for i in range(len(self.favor)):
      if list(self.favor.values())[i] > int(self.favorNum):
        self.lstd[list(self.favor.values())[i]] = list(self.favor.keys())[i]
    if len(self.lstd) > 1:
      kkkk = self.nowChannel
      a = list(self.lstd.keys())[0]
      self.nowChannel = a
      return kkkk

    elif len(self.lstd) == 1:
      kkkk = self.nowChannel
      self.nowChannel = list(self.lstd.keys())[0]
      return  kkkk
    else:
      kkkk = self.nowChannel
      favorlist = list(self.favor.values())
      minn = min(favorlist)
      aaa = favorlist.index(minn)
      self.nowChannel = list(self.favor.keys())[aaa]
      return kkkk 

결과 확인 코드

remocon = RemoteControl()
# a
print(remocon.powerOnRemoteControl([[6,"SBS"],[7,"KBS2"],[9,"KBS1"],[11,"MBC"]]))
# b
print(remocon.gotoChannel(7))
# c
print(remocon.nextChannel())
# d
print(remocon.previousChannel())
# e
print(remocon.blockChannel())
#f
print(remocon.unblockChannel(7))
#g
remocon.powerOffRemoteControl()
f = open('openoutput.csv','r')
read = csv.reader(f)
for i in read:
  print(i)
f.close()
# h
print(remocon.favorChannel())
# i
print(remocon.aiNextChannel())
#j
print(remocon.aiPreviousChannel())
profile
Kyunghee univ. IE 21

0개의 댓글