Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions 16주차/(P)17677/17677_python_kimjoohye.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import math

def changeStr(string):
#문자열 나누기
result = [string[i:i+2] for i in range(0, len(string)-1)]
#특수문자, 공백, 숫자 제거 & 소문자 변환
addIndex = []
for i in range(len(result)):
if result[i].isalpha():
addIndex.append(i)
string = []
if len(addIndex)!=0:
for i in addIndex:
string.append(result[i].lower())
print(string)
return string
def solution(str1, str2):
answer = 0
str1 = changeStr(str1)
str2 = changeStr(str2)

cnt=0
tmp_s2 = str2.copy()
for s1 in str1:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

str1의 길이가 n, str2의 길이가 k 일 때
동일 로직에 대해 O(nk) 가 아니라 O(n + k) 로 해결할 수 있는 방법에 대해 고민해보시면 좋을 것 같습니다

for s2 in tmp_s2:
if s1==s2:
cnt+=1
tmp_s2.remove(s2)
break
#분자 : cnt
#분모 : add
add = len(str1)+len(str2)-cnt
if len(str1)==len(str2)==0:
answer = 1
else:
answer = cnt/add
print(answer)
answer = math.trunc(answer*65536)
return answer
80 changes: 80 additions & 0 deletions 16주차/(P)81302/81302_python_kimjoohye.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
def checkPerson(places):
PersonAxis = []
#'P'자리 모든 좌표 구하기 - PersonAxis
for row in range(len(places)):
for col in range(len(places[0])):
if places[row][col]=='P':
PersonAxis.append([row, col])
#하나의 PersonAxis좌표마다 2보다 거리가 작은 좌표 append (열방향으로)
#result = [[0,0]
# [0,4]
# [2,1], [2,3]
# [2,3], [2,1]
# [4,0]
# [4,4]]
result = []
for [i1, j1] in PersonAxis:
tmp = []
tmp.append([i1, j1])
for [i2, j2] in PersonAxis:
if abs(i2-i1) + abs(j2-j1) <= 2:
if i2!=i1 or j2!=j1:
tmp.append([i2, j2])
result.append(tmp)
# print(result)
return result

def manhattan2(places,row):
#row에 있는 좌표배열 사이에 파티션이 없으면 거리두기 실패 - True

#바로 옆에 사람이 붙어있는 경우 ex) 'XXPPX'
for p in row:
[i1, j1] = p[0]
for [i2, j2] in p:
if i1 != i2 or j1 != j2:
if abs(i1-i2) == 1 and abs(j1-j2) != 1: return True
Copy link
Collaborator

@lomuto lomuto Jul 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 두 if 문을 (35, 36번째 줄) 하나의 return 문으로 축약할 수 있는 방법에 대해 고민해보시면 좋을 것 같습니다

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

참고로 이 질문은 제가 라이브 코딩테스트 면접에서 받았던 질문이기도 합니다 :)

if abs(i1-i2) != 1 and abs(j1-j2) == 1: return True

#거리가 2인 자리에 사람이 있는 경우
#사람간에 X자리가 있는 경우를 다 따져 생각해보기
for p in row:
[i1, j1] = p[0]
for [i2, j2] in p:
#거리가 2인 자리에 있는 행에 사람이 있는 경우 그 사이에 x가 있는지 확인 ex) 'OPXPX'
if i1==i2 and j1!=j2 and abs(j1-j2)==2:
if places[i1][int((j1+j2)/2)] != 'X':
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기있는 대부분의 if 문들도 하나의 return 문으로 어떻게 줄일 수 있는지 고민해보시면 좋을 것 같아요

return True
#거리가 2인 자리에 있는 열에 사람이 있는 경우 그 사이에 x가 있는지 확인 ex) 'OOPXX', 'OXXXX', 'OXPXX'
if j1==j2 and i1!=i2 and abs(i1-i2)==2:
if places[int((i1+i2)/2)][j1] != 'X':
return True
#거리가 2인 자리에 있는 대각선에 사람이 있는 경우 확인
if i1!=i2 and j1!=j2:
i_tmp = max(i1,i2)
j_tmp = max(j1,j2)
if [i_tmp, j_tmp] == [i1, j1] or [i_tmp, j_tmp] == [i2, j2]:
if places[max(i1,i2)-1][max(j1,j2)] != 'X':
return True
if places[max(i1,i2)][max(j1,j2)-1] != 'X':
return True
else:
if places[i_tmp][j_tmp] != 'X':
return True
if places[i_tmp-1][j_tmp-1] != 'X':
return True
return False

def solution(places):

answer = []

for p in places:
pAxis = []
pAxis = checkPerson(p)
flag = 1
if len(pAxis) != 0 and manhattan2(p, pAxis):
flag = 0
answer.append(flag)


return answer