본문 바로가기

Python7

04. 컬렉션(Collection) 컬렉션(Collection) 컬렉션 자료구조는 데이터를 서로 연관시키지 않고 모아두는 컨테이너(container) 이다. 맨버십 연산자 : in 크기 함수 : len 반복성 set and dictionary Set 반복 가능하다. 가변적이며 중복 요소가 없다. 정렬되지 않는 데이터 타입이다. 즉 인덱스 연산을 할 수 없다. 중복 요소를 제거할 수 있다. 시간복잡도 O(1) 이다. 딕셔너리(dictionary) hash table로 구현되어 있다. 특정 객체에 해당하는 임의의 정수 값을 상수 시간 내에 계산한다. 연습문제 단어 횟수 세기 애너그램 문장 또는 단어의 철자 순서를 바꾸는 놀이 주사위 합계 경로 주사위를 두 번 던져서 합계가 특정 수가 나오는 경우의 수와 경로를 구해보자 단어의 중복 문자 제거 .. 2020. 6. 1.
03. 스택(Stack) & 큐(Queue) 스택(Stack) LIFO(Last in, first out)구조이다. 즉, 배열의 끝에서만 데이터에 접근할 수 있다. 배열 인덱스 접근이 불가하다. 시간복잡도 : O(1)이다. stack의 동작 push : 스택 맨 끝에 항목을 삽입 pop : 스택 맨 끝 항목 반환 및 제거 top/peek : 스택 맨 끝 항목 반환 empty : 비어있는지 확인 size : 스택 크기 확인 스택 구현 1. python list를 활용한 stack 구현 2. Node(객체)의 컨테이너로 Stack 구현 2020. 5. 26.
[HackerRank] 20. Birthday chocolate Birthday Chocolate 초콜렛 바는 연속되어야 한다. list 의 끝 인덱스가 넘어가면 마지막 인덱스까지 추출한다.\ sum, len을 반환하여 로직을 세운다. import math import os # Complete the birthday function below. def birthday(s, d, m): count = 0 for i in range(len(s)): temp = s[i:i+m] if sum(temp) == d: if len(temp) == m: count += 1 return count if __name__ == '__main__': n = int(input().strip()) s = list(map(int, input().rstrip().split())) dm = inp.. 2020. 4. 1.
[HackerRank] 19. Bon Appétit Bon Appétit import math # Complete the bonAppetit function below. def bonAppetit(bill, k, b): half_total = sum(bill) / 2 bill.pop(k) anna_actual = sum(bill) / 2 if b == anna_actual: print('Bon Appetit') else: print(int(half_total - anna_actual)) if __name__ == '__main__': nk = input().rstrip().split() n = int(nk[0]) k = int(nk[1]) bill = list(map(int, input().rstrip().split())) b = int(input().s.. 2020. 4. 1.
[HackerRank] 18. Breaking the Records Breaking the Records import math import os import random import re import sys # Complete the breakingRecords function below. def breakingRecords(scores): max_score = scores[0] min_score = scores[0] max_count = 0 min_count = 0 for score in scores: if max_score score: min_score = score min_count += 1 return max_count, min_count if __name__.. 2020. 4. 1.
[HackerRank] 17. Counting Valleys Counting Valleys def countingValleys(n, s): height = 0 pre_height = 0 result = 0 for i in s: # 이전 높이 계산 pre_height = height # 현재 높이 계산 if i =='D': height -= 1 else: height += 1 if height == 0 and pre_height < 0: result += 1 return result def main(): n = int(input()) s = input() result = countingValleys(n, s) print(result) if __name__ == '__main__': main() 2020. 4. 1.
[HackerRank] 16. Day of the programmer Day of the Programmer #!/bin/python3 import math import os import random import re import sys # Complete the dayOfProgrammer function below. def solve(year): if (year == 1918): return '26.09.1918' elif ((year 1918) & (year%400 == 0 or ((year%4 == 0) & (year%100 != 0)))): return '12.09.%s' %year else: return '13.09.%s' %year if __name__ == '__main__': year = int(input().strip()) result = dayOfPro.. 2020. 4. 1.