본문 바로가기

전체 글94

프로그래머스: 단어변환 (python, 파이썬 ) 1. 문제 https://programmers.co.kr/learn/courses/30/lessons/43163# 2. 풀이 BFS 3. 구현 1. dictionary에 append할 때 if item1 in dic: dic[item1].append(item2) else: dic[item1]=[item2] 2. AttributeError: 'str' object has no attribute 'append' if item1 in dic: dic[item1].append(item2) else: dic[item1]=item2 4와 같이 쓰면 dictionary가 key:str, value:str로 연결되서 dict[item1]에 str를 반환해 나는 에러 if item1 in dic: dic[item1].a.. 2021. 7. 2.
프로그래머스 네트워크 python 1. 문제 https://programmers.co.kr/learn/courses/30/lessons/43162?language=python3 코딩테스트 연습 - 네트워크 네트워크란 컴퓨터 상호 간에 정보를 교환할 수 있도록 연결된 형태를 의미합니다. 예를 들어, 컴퓨터 A와 컴퓨터 B가 직접적으로 연결되어있고, 컴퓨터 B와 컴퓨터 C가 직접적으로 연결되어 있 programmers.co.kr 2. 풀이 하나로 합쳐져있는 집합을 구하는 방법은 Union-Find이다. 3. 구현 def find(x, parent): if parent[x]==x: return x parent[x]=find(parent[x], parent) return parent[x] def uni(x, y, parent): x = find.. 2021. 6. 29.
softmax가 세상을 분류하는 방법 (왜 linear와 non-linear가 반복적으로 사용될까?) 목차: (1) 궁금증: 소프트맥스?? (2) 아날로그와 디지털의 차이 (3) 선형모델 vs 비선형모델: 로지스틱 함수부터 시작해보자면... (4) 로짓이란?? (5) 선형과 비선형을 결합시키는 딥러닝 (6) 로지스틱 -> 시그모이드 -> 소프트맥스 함수, 다시 소프트맥스 함수로 (1) 궁금증: 소프트맥스?? 수업을 듣는 도중에 선생님께서 이런 말씀을 하신다. " 신경망 모델은 기본적으로 비선형 모델이지만 수학적으로 분해를 해보면 선형모델이 그 안에 숨겨져 있고, 비선형 모델과 선형 모델의 결합으로 이뤄져있습니다. " 이 말이 너무 추상적이어서 softmax에 대해 공부해보게 되었다. (2) 아날로그와 디지털의 차이 아날로그와 디지털의 차이가 무엇이냐하면, 아날로그는 연속적인 값들을 말하고 디지털은 분리된.. 2021. 6. 28.
[그리디] 프로그래머스 조이스틱 python 1. 문제 https://programmers.co.kr/learn/courses/30/lessons/42860 2. 풀이 1) 'A'부터 올라가는 방향으로도 움직일 수 있고 'Z'에서 낮아지는 방향으로 움직일 수 있다. 2) 그 때 그때의 idx에 따라 왼쪽으로 가는 게 나을 수도 있고 오른쪽으로 가는 게 나을 수도 있다. 예: AZABAAAAAAAAAAAAAAAAAAXA 오른쪽으로 가서 Z B를 바꾸고 다시 왼쪽으로 와서 X를 고쳐야함. 파이썬 아스키코드: ord print(chr(65)) print(ord('A')) print(ord('d')) A #65를 chr로 만듦 65 #'A'를 숫자로 만듦 65~90 100 #'d'를 숫자로 만듦 97~122 삼항연산자: answer += le if le 2021. 6. 27.
[BFS] 프로그래머스 게임 맵 최단거리 python 1. 문제 https://programmers.co.kr/learn/courses/30/lessons/1844 코딩테스트 연습 - 게임 맵 최단거리 [[1,0,1,1,1],[1,0,1,0,1],[1,0,1,1,1],[1,1,1,0,1],[0,0,0,0,1]] 11 [[1,0,1,1,1],[1,0,1,0,1],[1,0,1,1,1],[1,1,1,0,0],[0,0,0,0,1]] -1 programmers.co.kr 2. 풀이 전형적인 BFS map의 row col이 다를 수 있다는 점을 빼먹어서 틀렸다. 3. 구현 from collections import deque # deque import d = [[1,0], [-1, 0], [0,1], [0,-1]] def isValid(r,c, row, col): r.. 2021. 6. 18.
[분할정복]프로그래머스 [카카오 인턴] 수식 최대화 python 1. 문제 2. 풀이 분할 정복 분할정복은 dfs처럼 그 아래가 완성될 때, 라는 가정을 하고 푼다. return을 잘 생각해야한다. 3. 구현 NameError: name 'ops' is not defined ops를 함수 인자로 안보내줘서 난 에러 TypeError: unsupported operand type(s) for -: 'str' and 'int' string 형태를 - 연산하려고 해서 난 에러, str을 cal로 보내 int형으로 바꾼 후 - 연산을 하도록 바꿨다. def plus(ops,s): li = s.split('+') res = 0 for item in li: res=res + int(cal(ops, item)) # print("plus: ", res) return res def .. 2021. 6. 17.
프로그래머스 예상대진표 python: Queue 1. 문제 2. 풀이 3. 구현 def solution(n,a,b): answer = 0 q = deque([i for i in range(1,n+1)]) while(a!=b): a = (a+1)//2 b = (b+1)//2 answer+=1 return answer 예시 테스트케이스만 맞고 나머지는 틀린다. from collections import deque def solution(n,a,b): answer = 0 q = deque([i for i in range(1,n+1)]) while(q): x = q.popleft() y = q.popleft() if [x,y] == [a,b] or [x,y] == [b,a]: answer+=1 break if a in [x, y]: # list를 쓰면 한 줄.. 2021. 6. 16.
프로그래머스 전화번호 목록도움말 python 1. 문제 2. 풀이 sort 하면 길이대로 정렬되는 것 str.startswith(str2) 시작하는 것 확인하는 라이브러리 3. 구현 def solution(phone_book): answer = True phone_book.sort() for num1, num2 in zip(phone_book, phone_book[1:]): if num2.startswith(num1): return False return True 2021. 6. 14.
프로그래머스 짝지어 제거하기 python 1. 문제 2. 풀이 앞뒤를 비교하므로 stack 자료구조 이용! 2. 구현 정답코드 def solution(s): answer = -1 stk = [] for c in s: if stk: if stk[-1]==c: stk.pop() else: stk.append(c) else: stk.append(c) if stk: answer=0 else: answer=1 return answer 답은 나오는 것 같은데 시간초과. 사실 그럴법도 하다. def solution(s): answer = -1 li = [ 'aa', 'bb', 'cc', 'dd', 'ee', 'ff', 'gg', 'hh', 'ii', 'gg', 'kk', 'll', 'mm', 'nn', 'oo', 'pp', 'qq', 'rr', 'ss', .. 2021. 6. 14.
프로그래머스 오픈채팅방 python 1. 문제 2. 풀이 dictionary 형태 사용하기 3. 구현 def solution(record): answer = [] dic={} for item in record: item = item.split(' ') # item.split(' ') 하면 달라지는 것이 없다. if item[0] != 'Leave': dic[item[1]] = item[2] for item in record: item = item.split(' ') if item[0]=='Enter': answer.append(f"{dic[item[1]]}님이 들어왔습니다.") if item[0]=='Leave': answer.append(f"{dic[item[1]]}님이 나갔습니다.") return answer 2021. 6. 14.
프로그래머스 124나라의 숫자 python 1.문제 2.풀이 q,r = divmod(N, Q) 단점은 조금 느리다는 것. 3.구현 def solution(n): answer = '' if n 2021. 6. 14.
프로그래머스 튜플 python 1. 문제 2. 풀이 1) string을 list 형태로 만든다 2) 원소를 크기 순서대로 정렬해서 앞의 집합에 없는 원소를 추가한다. 3. 구현 1) .split을 잘 써서 string을 분리하면 훨씬 깔끔해짐. 2) 원소의 크기로 정렬할 때는 .sort(key=len) 3) 원소가 있는 지 아닌 지 확인할 때 for item (not) in list: 하면 된다. 내풀이 def solution(s): answer = [] tmp=[] tmpList =[] tmpNum=0 for ch in s: if ch.isdigit(): tmpNum=int(ch)+tmpNum*10 if ch==',': if tmpNum == 0: continue else: tmp.append(tmpNum) tmpNum=0 if c.. 2021. 6. 13.
반응형