-
Notifications
You must be signed in to change notification settings - Fork 9
[김주혜] 16주차 81302번 과제 제출합니다. #114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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: | ||
| 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 | ||
| 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 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 두 if 문을 (35, 36번째 줄) 하나의 return 문으로 축약할 수 있는 방법에 대해 고민해보시면 좋을 것 같습니다
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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': | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment.
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) 로 해결할 수 있는 방법에 대해 고민해보시면 좋을 것 같습니다