본문 바로가기

CodingTest/Programmers

(66)
[ 프로그래머스 ] 거리두기 / 제출 1 / def check_if_partitions(place, a, b): #there is partitions if abs(a[0] - b[0]) == 2: if place[max(a[0],b[0])-1][a[1]] == ('X' or 'P'): return True elif abs(a[1]-b[1]) == 2: if place[a[0]][max(a[1],b[1])-1] == ('X' or 'P'): return True else: # 1, 1 씩 차이 if place[max(a[0],b[0])][min(a[1],b[1])] == ('X' or 'P') and place[min(a[0], b[0])][max(a[1],b[1])] == ('X' or 'P'): return True return ..
[ 프로그래머스 / 파이썬 ] 프렌즈 4블록 / 구현 1 / def solution(m, n, board): answer = 0 # 각 값 쉽게 접근 위해 배열화 for i in range(m): board[i] = list(board[i].strip()) # popBlock 시작위해 무작위값 대입 popBlocks = ['a'] while popBlocks: # 전체 값 돌며 있나 체크 좌상이 기준 popBlocks = [] visited = [[False]*n * for _ in range(m)] for i in range(m-1): for j in range(n-1): ij = check(i, j, board) if ij: popBlocks.extend(check(i, j, board, visited))# 지워질 아이템 인덱스 배열 # 아래 ..
[ 프로그래머스 ] 후보키 분명 데이터베이스 열심히 공부했었는데, 이렇게 문제로 만나니까 갑자기 개념이 헷갈렸습니다. 후보키에서 최소성이 "있어야"한다는 것인데, 꼬아서 생각되버렸기 때문입니다. 최소성 : 키를 구성하는 속성 하나를 제거하면 유일성이 깨져야한다. 즉, 어떤 속성이 제거되어도 여전히 유일성을 유지하면 안 되는 것입니다. 유일성을 유지한다는 이야기는, 위의 제거 되어도 무방한 속성 외에 유일성을 갖는 키 자체를 포함하고 있다는 이야기입니다. 즉, 최소성을 가진다는 이야기는 다른 후보키를 포함하지 않는다는 말이 됩니다. = A가 후보키일 때, B가 A를 포함하면 B는 최소성 위반으로 후보키가 되지 못하는 겁니다. B = A + b 인데, b가 사라져도 여전히 유일성을 유지하니까요! https://studyandwrite..
[ 프로그래머스 ] 순위 검색 / 제출 1 / def solution(info, query): answer = [] for q in query: lan, pos, lev, food = q.split(' and ') food, score = food.split(' ') score = int(score) count = check(lan, pos, lev, food, score, info) answer.append(count) return answer def check(lan, pos, lev, food, score, information): count = 0 for info in information: lan_i, pos_i, lev_i, food_i, score_i = info.split(" ") score_i = int(score_i..
[ 프로그래머스 ] 수식 최대화 / 구현 1 / from itertools import permutations def calculate(a, b, oper): if oper == '*': return a*b elif oper == '+': return a+b elif oper == '-': return a-b def solution(expression): answer = 0 nums = [] opers = [] num = '' for ex in expression: if ex == "*" or ex == "+" or ex == "-": opers.append(ex) nums.append(num) num = '' else: num += ex oper_order = list(permutations(['-','*','+'],3)) print..
[ 프로그래머스 ] 다리를 지나는 트럭 / 코드 1 / def solution(bridge_length, weight, truck_weights): q=[0]*bridge_length sec=0 while q: sec+=1 q.pop(0) if truck_weights: if sum(q)+truck_weights[0] weight: bridge.append(0) else: truck = truck_weights.pop() bridge.append(truck) total_weight += truck step += 1 step += bridge_length return step
[ 프로그래머스 ] 124 나라의 숫자 def solution(n): answer = '' while n: if n%3: answer += str(n%3) n = n//3 else: answer += '4' n = n//3-1 answer = answer[::-1] return answer 으아 진법 개념 어렵다 역시 그래도 3진법 개념이라는게 빨리 들어와서 아이디어는 잡을 수 있었다. 여기서 체크할 것은 1. 문자열에 값 더할 때 str으로 변환해서 바로 더하는 것 2. if문에서 값이 1이면 true, 0이면 false처럼 바로 써도 되는 것. 3. 문자열 순서 뒤집기는 문자열[::-1]로 구현
[ 프로그래머스 ] 타켓 넘버 < 다시 풀기 > / 가져온 풀이 / def solution(numbers, target): answer = DFS(numbers, target, 0) return answer def DFS(numbers, target, depth): answer = 0 if depth == len(numbers): if sum(numbers) == target: return 1 else:return 0 else: answer += DFS(numbers, target, depth+1) numbers[depth] *= -1 answer += DFS(numbers, target, depth+1) return answer 너무 오랜만에 DFS/BFS문제를 풀려고 하니 머릿 속에 잘 안 들어왔습니다. 해서 다른 분 풀이를 이용해 공부를 하는 식..
[ 프로그래머스 ] 구명보트 / 제출 1 / def solution(people, limit): answer = 0 boat_weight = 0 people.sort() for person in people: if boat_weight + person
[ 프로그래머스 ] 오픈채팅방 / 제출 1 / def change(array, uid): return updated_array def solution(records): answer = [] name_dict = {} for record in records: record = record.split() if len(record) == 3: oper, uid, name = record else: # leave인 경우 oper, uid = record if oper == 'Enter': answer.append(uid+"님이 들어왔습니다.") if uid not in name_dict: name_dict[uid] = name elif uid in name_dict and name_dict[uid] != name: name_dict[uid] ..