Skip to content

Instantly share code, notes, and snippets.

@JoeUnsung
Created January 12, 2018 06:05
Show Gist options
  • Save JoeUnsung/fb79526038b122936e02a6b07ea7d47d to your computer and use it in GitHub Desktop.
Save JoeUnsung/fb79526038b122936e02a6b07ea7d47d to your computer and use it in GitHub Desktop.
coding algorithm questions from kakao blind recruiting
############################
## 카카오 블라인드 알고리즘 문제 ##
###########################
## 1
## 2
## 3
## 4
## 5 자카드 유사도
import math
## 5-1 문자열 두개를 입력받기
lst = []
for i in range(0, 2):
while True:
x = input('str{0} :'.format(i + 1))
if not (len(x) <= 1000 and len(x) >= 2):
print("2와 1000 사이의 길이의 문자열을 입력하세요")
continue
lst.append(x)
break
## 5-2
temp = [[], []]
for j in range(2):
for i in range(len(lst[j]) - 1):
temp[j].append(temp[j][i:i + 2])
print(temp)
for i in range(len(x2) - 1):
temp2.append(x2[i:i + 2])
# print(temp1)
# print(temp2)
## 5-3
product_a = []
product_b = []
for i in temp1:
if re.sub('[^a-zA-Z]', '', i) == i:
product_a.append(i.lower())
for i in temp2:
if re.sub('[^a-zA-Z]', '', i) == i:
product_b.append(i.lower())
## 합집합 / 교집합 만들기
def union_set(A, B):
temp = [];
donelist = [];
for i in A:
if i not in B: ## A 에만 존재하는 것 다루는 부분
temp.append(i)
else: ## 양쪽에 모두 존재하는 요소들 다루는 부분
for j in range(max(A.count(i), B.count(i))):
if i not in donelist:
temp.append(i)
donelist.append(i)
for i in B:
if i not in A:
temp.append(i)
return temp
# print(union_set(A,B))
def intersect_set(A, B):
temp = [];
donelist = [];
for i in A:
for j in range(min(A.count(i), B.count(i))):
if i not in donelist:
temp.append(i)
donelist.append(i)
return temp
# print(intersect_set(A,B))
try:
math.floor(len(intersect_set(product_a, product_b)) / len(union_set(product_a, product_b)) * 65536)
except ZeroDivisionError:
print(65536)
### 6. 프렌즈 4블록 (난이도 : 상)
### 입력으로 블록의 첫 배치가 주어졌을 때, 지워지는 블록은 모두 몇 개인지 판단하는 프로그램을 제작하라.
## 6-1 입력
m = 4;n = 5;x = ["CCBDE", "AAADE", "AAABF", "CCBBF"];
m = 6;n = 6;x = ["TTTANT", "RRFACC", "RRRFCC", "TRRRAA", "TTMMMF", "TMMTTJ"];
## index를 통한 변경 작업의 편의를 위해 미리 스트링을 리스트 형태로 변경
for i in range(len(x)):
x[i] = list(x[i])
## 6-2 탐색
cnt = 0;fin = 1; ## 총파괴된 갯수 : cnt // 루프 종료 여부 : fin
#while fin == 1:
for z in range(5):
countBoard = [] ## 해당 위치의 인자가 파괴되는지 아닌지의 여부를 체크하는 보드를 0으로 초기화 (1 파괴 0 파괴노노)
for i in range(m):
temp = []
for j in range(n):
temp.append(0)
countBoard.append(temp)
#print(countBoard)
## 파괴할 블록 영역 탐색
fin = 0; donelist=[]; ## fin을 0으로 변경 donelist 초기화
for i in range(m - 1):
for j in range(n - 1):
if x[i][j] == x[i][j + 1] and x[i][j] == x[i + 1][j] and x[i][j] == x[i + 1][j + 1] and x[i][j] != '9': ## 사각형모양이 완성되었다면
donelist.append(x[i][j]) ## 파괴된 인자를 리스트에 등록하고
countBoard[i][j], countBoard[i + 1][j], countBoard[i][j + 1], countBoard[i + 1][j + 1] = 1, 1, 1, 1 ## 해당 위치를 파괴된 값으로 변경해준다
fin = 1 ## fin은 해당 차수에 사각형모양이 한번이라도 완성되었다면 1로 변한다 fin이 그대로 0일 경우 루프를 종료한다
## 파괴한 블록수 카운트
for i in range(len(countBoard)):
cnt += countBoard[i].count(1)
#print("파괴한 블록수 : {0}".format(cnt))
## 6-3 제거 (파괴된 블럭을 9로 변환)
donelist = set(donelist) ## 해당 차수에 파괴된 인자값을 저장
for i in range(m):
for j in range(n):
if x[i][j] in donelist and countBoard[i][j] == 1: ## 파괴가 수행된 인자이고 해당 위치가 파괴가 수행된 위치라면
x[i][j] = '9' ## 빈자리로 바꿔라 (9로)
## 6-4 정렬 (아래가 파괴되었다면 [9라면] 아래로 옮기기)
for i in range(m-1): ## 맨 아래행은 수행하지 않는다!
for j in range(n):
if x[i+1][j] == '9': ## 아래가 빈자리이면
x[i][j] , x[i+1][j] = x[i+1][j], x[i][j] ## 내려가라 인자야
#print(x)
## 출력
for i in x:
print(i)
print("파괴한 블록수 : {0}".format(cnt))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment