2023-05-21(일) 11:00
모의고사1
문제1
- 문자열(hash,stack),22분
def solution(input_string):
alpha_hash={}
last_ch=None
for ch in input_string:
if last_ch==None or last_ch!=ch:
if alpha_hash.get(ch,False)==False:
alpha_hash[ch]=1
else:
alpha_hash[ch]+=1
last_ch=ch
else:
continue
answer=[]
for key,value in alpha_hash.items():
if value>=2:
answer.append(key)
if answer==[]:
return "N"
else:
return "".join(sorted(answer))
문제2
- 수학,완전탐색(순열과 조합)
import itertools
def solution(ability):
index_list=[i for i in range(len(ability))]
permu_list=list(itertools.permutations(index_list,len(ability[0])))
max=0
for permu in permu_list:
sum=0
for sub,stu_index in enumerate(permu):
sum+=ability[stu_index][sub]
if max<sum:
max=sum
return max
pccp의 코드 시간제한은 10초
문제3
- 수학(stack)
스택방식
def solution(queries):
result=[]
for gen,st in queries:
st_stack=[]
#첫번째는 인덱스 0이므로 처음에 1을 빼준다.
st=st-1
#n세대부터 1세대 까지 각자의 몇번째 인자인지 저장
while gen>1:
gen-=1
pre,child=divmod(st,4)
st_stack.append(child)
st=pre
flag=False
#1세대부터 인자의 순서를 따져가며 결과인자 선택,모든 인자의 자식인자 중 0은 RR,3은 rr,나머지는 Rr이다.
while len(st_stack)>0:
child=st_stack.pop()
if child==0:
result.append("RR")
flag=True
break
elif child==3:
result.append("rr")
flag=True
break
if flag==False:
result.append("Rr")
return result
재귀방식
#gen세대의 st번째 cong을 반환
def find_cong(gen,child):
if gen==1:
return "Rr"
#child//4는 부모세대의 st이다.
cong=find_cong(gen-1,child//4)
st=child%4
if cong=="RR":
return "RR"
elif cong=="rr":
return "rr"
else:
if st==0:
return "RR"
elif st==3:
return "rr"
else:
return "Rr"
def solution(queries):
result=[]
for querie in queries:
result.append((find_cong(querie[0],querie[1]-1)))
return result
문제4
- 구현(동기적 작업순서,힙,정렬)
from heapq import heappop,heappush
from collections import deque
def solution(program):
answer=[0 for _ in range(11)]
sorted_program=deque(sorted(program,key=lambda x:(x[1],x[0])))
to_do_list=[]
while len(sorted_program)>0 or len(to_do_list)>0:
if len(to_do_list)==0:
to_do=sorted_program.popleft()
heappush(to_do_list,(to_do[0],to_do))
current_time=to_do[1]
else:
to_do=heappop(to_do_list)[1]
if to_do[1]<current_time:
score=to_do[0]
#waiting한 시간을 더한다.
answer[score]+=current_time-to_do[1]
current_time+=to_do[2]
while len(sorted_program)>0 and sorted_program[0][1]<=current_time:
to_do=sorted_program.popleft()
heappush(to_do_list,(to_do[0],to_do))
answer[0]=current_time
return answer