Guest User

Untitled

a guest
Jan 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. ############################
  2. ## 카카오 블라인드 알고리즘 문제 ##
  3. ###########################
  4. ## 1
  5.  
  6.  
  7. ## 2
  8.  
  9.  
  10. ## 3
  11.  
  12.  
  13. ## 4
  14.  
  15.  
  16. ## 5 자카드 유사도
  17. import math
  18.  
  19. ## 5-1 문자열 두개를 입력받기
  20. lst = []
  21. for i in range(0, 2):
  22. while True:
  23. x = input('str{0} :'.format(i + 1))
  24. if not (len(x) <= 1000 and len(x) >= 2):
  25. print("2와 1000 사이의 길이의 문자열을 입력하세요")
  26. continue
  27. lst.append(x)
  28. break
  29.  
  30. ## 5-2
  31. temp = [[], []]
  32.  
  33. for j in range(2):
  34. for i in range(len(lst[j]) - 1):
  35. temp[j].append(temp[j][i:i + 2])
  36. print(temp)
  37.  
  38. for i in range(len(x2) - 1):
  39. temp2.append(x2[i:i + 2])
  40. # print(temp1)
  41. # print(temp2)
  42.  
  43. ## 5-3
  44. product_a = []
  45. product_b = []
  46.  
  47. for i in temp1:
  48. if re.sub('[^a-zA-Z]', '', i) == i:
  49. product_a.append(i.lower())
  50. for i in temp2:
  51. if re.sub('[^a-zA-Z]', '', i) == i:
  52. product_b.append(i.lower())
  53.  
  54. ## 합집합 / 교집합 만들기
  55. def union_set(A, B):
  56. temp = [];
  57. donelist = [];
  58. for i in A:
  59. if i not in B: ## A 에만 존재하는 것 다루는 부분
  60. temp.append(i)
  61. else: ## 양쪽에 모두 존재하는 요소들 다루는 부분
  62. for j in range(max(A.count(i), B.count(i))):
  63. if i not in donelist:
  64. temp.append(i)
  65. donelist.append(i)
  66. for i in B:
  67. if i not in A:
  68. temp.append(i)
  69. return temp
  70.  
  71. # print(union_set(A,B))
  72. def intersect_set(A, B):
  73. temp = [];
  74. donelist = [];
  75. for i in A:
  76. for j in range(min(A.count(i), B.count(i))):
  77. if i not in donelist:
  78. temp.append(i)
  79. donelist.append(i)
  80. return temp
  81. # print(intersect_set(A,B))
  82.  
  83. try:
  84. math.floor(len(intersect_set(product_a, product_b)) / len(union_set(product_a, product_b)) * 65536)
  85. except ZeroDivisionError:
  86. print(65536)
  87.  
  88.  
  89.  
  90.  
  91.  
  92. ### 6. 프렌즈 4블록 (난이도 : 상)
  93. ### 입력으로 블록의 첫 배치가 주어졌을 때, 지워지는 블록은 모두 몇 개인지 판단하는 프로그램을 제작하라.
  94.  
  95. ## 6-1 입력
  96. m = 4;n = 5;x = ["CCBDE", "AAADE", "AAABF", "CCBBF"];
  97. m = 6;n = 6;x = ["TTTANT", "RRFACC", "RRRFCC", "TRRRAA", "TTMMMF", "TMMTTJ"];
  98. ## index를 통한 변경 작업의 편의를 위해 미리 스트링을 리스트 형태로 변경
  99. for i in range(len(x)):
  100. x[i] = list(x[i])
  101.  
  102. ## 6-2 탐색
  103. cnt = 0;fin = 1; ## 총파괴된 갯수 : cnt // 루프 종료 여부 : fin
  104. #while fin == 1:
  105. for z in range(5):
  106. countBoard = [] ## 해당 위치의 인자가 파괴되는지 아닌지의 여부를 체크하는 보드를 0으로 초기화 (1 파괴 0 파괴노노)
  107. for i in range(m):
  108. temp = []
  109. for j in range(n):
  110. temp.append(0)
  111. countBoard.append(temp)
  112. #print(countBoard)
  113.  
  114. ## 파괴할 블록 영역 탐색
  115. fin = 0; donelist=[]; ## fin을 0으로 변경 donelist 초기화
  116. for i in range(m - 1):
  117. for j in range(n - 1):
  118. 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': ## 사각형모양이 완성되었다면
  119. donelist.append(x[i][j]) ## 파괴된 인자를 리스트에 등록하고
  120. countBoard[i][j], countBoard[i + 1][j], countBoard[i][j + 1], countBoard[i + 1][j + 1] = 1, 1, 1, 1 ## 해당 위치를 파괴된 값으로 변경해준다
  121. fin = 1 ## fin은 해당 차수에 사각형모양이 한번이라도 완성되었다면 1로 변한다 fin이 그대로 0일 경우 루프를 종료한다
  122.  
  123. ## 파괴한 블록수 카운트
  124. for i in range(len(countBoard)):
  125. cnt += countBoard[i].count(1)
  126. #print("파괴한 블록수 : {0}".format(cnt))
  127.  
  128. ## 6-3 제거 (파괴된 블럭을 9로 변환)
  129. donelist = set(donelist) ## 해당 차수에 파괴된 인자값을 저장
  130. for i in range(m):
  131. for j in range(n):
  132. if x[i][j] in donelist and countBoard[i][j] == 1: ## 파괴가 수행된 인자이고 해당 위치가 파괴가 수행된 위치라면
  133. x[i][j] = '9' ## 빈자리로 바꿔라 (9로)
  134.  
  135. ## 6-4 정렬 (아래가 파괴되었다면 [9라면] 아래로 옮기기)
  136. for i in range(m-1): ## 맨 아래행은 수행하지 않는다!
  137. for j in range(n):
  138. if x[i+1][j] == '9': ## 아래가 빈자리이면
  139. x[i][j] , x[i+1][j] = x[i+1][j], x[i][j] ## 내려가라 인자야
  140. #print(x)
  141.  
  142. ## 출력
  143. for i in x:
  144. print(i)
  145. print("파괴한 블록수 : {0}".format(cnt))
Add Comment
Please, Sign In to add comment