목록2022/01/18 (16)
Developer_Neo
문제 동혁이는 오래된 창고를 뒤지다가 낡은 체스판과 피스를 발견했다. 체스판의 먼지를 털어내고 걸레로 닦으니 그럭저럭 쓸만한 체스판이 되었다. 하지만, 검정색 피스는 모두 있었으나, 흰색 피스는 개수가 올바르지 않았다. 체스는 총 16개의 피스를 사용하며, 킹 1개, 퀸 1개, 룩 2개, 비숍 2개, 나이트 2개, 폰 8개로 구성되어 있다. 동혁이가 발견한 흰색 피스의 개수가 주어졌을 때, 몇 개를 더하거나 빼야 올바른 세트가 되는지 구하는 프로그램을 작성하시오. 입력 첫째 줄에 동혁이가 찾은 흰색 킹, 퀸, 룩, 비숍, 나이트, 폰의 개수가 주어진다. 이 값은 0보다 크거나 같고 10보다 작거나 같은 정수이다. 출력 첫째 줄에 입력에서 주어진 순서대로 몇 개의 피스를 더하거나 빼야 되는지를 출력한다. ..
문제 파티가 끝나고 나면, 사람들은 누가 파티에 왔는지와 얼마나 많은 사람들이 왔는지를 궁금해한다. 보통 파티는 매우 크게 열리기 때문에, 정확하게 몇 명이 참가했는지 알 수가 없다. 지난주 토요일에 상근이는 자신의 3학년 진학을 기념하면서 매우 성대한 파티를 열었다. 그리고, 상근이는 1m2당 몇 명의 사람이 있었는지 알고있다. 상근이의 파티는 정말 엄청난 규모였기 때문에, 대부분의 신문에도 기사가 실렸다. 상근이는 서로 다른 5개의 신문을 보면서 그 기사에 적혀져있는 참가자의 수를 적었다. 상근이는 자신이 알고있는 참가자의 수가 정확하다고 생각한다. 각 신문 기사에 실려있는 참가자의 수가 몇 명 만큼 잘못되어있는지 구하는 프로그램을 작성하시오. 입력 첫째 줄에 1m2당 사람의 수 L (1 ≤ L ≤ ..
문제 16진수 수를 입력받아서 10진수로 출력하는 프로그램을 작성하시오. 입력 첫째 줄에 16진수 수가 주어진다. 이 수의 최대 길이는 6글자이다. 16진수 수는 0~9와 A~F로 이루어져 있고, A~F는 10~15를 뜻한다. 또, 이 수는 음이 아닌 정수이다. 출력 첫째 줄에 입력으로 주어진 16진수 수를 10진수로 변환해 출력한다. 생각 -> bin, hex와 oct에 해당하는 10진수를 2진수, 16진수, 8진수로 변경해주는 것이 있는데 반대로 하는 것이 있는가 생각 했더니 int()함수가 생각이 났고 int( 1 , 2 ) 에서 1번째로 들어오는 인수는 입력한 값, 2번째로 들어가는 인수는 16진수에 대해 변환을 해야하니 16이 들어가야한다. num=input() print(int(num,16))
문제 창영이는 노래 여러 개를 이어서 부르는 가수이다. 유명한 노래의 비슷한 멜로디를 이어서 부르면서 언제 곡이 넘어갔는지 모르게 만드는 것이 창영이 노래의 특징이다. 이런 노래로 상업적으로 엄청난 성공을 거둔 창영이에게 큰 시련이 찾아왔다. 그것은 바로 저작권이었다. 창영이의 노래에 포함되어 있는 멜로디는 모두 저작권이 다른 사람에게 있는 노래이다. 따라서, 이 음악으로 상업적인 활동을 했기 때문에, 저작권 협회에 저작권료를 내야한다. 창영이는 자신의 앨범에 포함되어있는 저작권이 있는 멜로디의 평균값을 구해보기로 했다. 이 값은 아래와 같이 구할 수 있다. (창영이 앨범에 수록된 곡에 포함되어 있는 저작권이 있는 멜로디의 개수) / (앨범에 수록된 곡의 개수) 이때, 평균값은 항상 올림을 해서 정수로 ..
제너레이터 - iterator객체의 한 종류로 next함수를 호출하면 값을 하나씩 얻을 수 있다. 제너레이터를 함수기반으로 만드는 법 yield가 들어갈것! yield가 하나라도 들어가면 제너레이터가 된다. def num_generator(): print('first') yield 0 print('second') yield 1 print('third') yield 2 >>> def num_generator(): print('first') yield 0 print('second') yield 1 print('third') yield 2 >>> gen=num_generator() >>> next(gen) first 0 >>> next(gen) second 1 >>> next(gen) third 2 >>> n..
처음에 Iterable객체와 Iterator객체는 무슨차이가 있는지 iter()와 next()함수는 언제 쓰는지에 대해 많이 혼동되었다. 그래서 정리하게 되었다. Iterable객체(자료형) 컨테이너 자료형 - 문자열, 튜플, 집합, 사전 내장함수 - range(), reversed(), enumerate(), filter(), map(), zip() Iterator객체 iter함수를 거쳐서 나온 것으로 iter함수가 생성해서 반환하는 객체이다. next()함수로 객체 안의 다음 값을 쉽게 얻을 수 있다. ir = iter( iterable객체 ) 로써 선언을 하면 ir은 Iterator객체된 것이고 next()함수를 사용할 수 있따는 것이다. next( iterator객체 ) 따라서 계속해서 next함..
Comprehension의 사전적 의미 - 이해, 이해력, 포용, 포용력, 포함, 압축 파이썬에서의 Comprehesion - 리스트(list), 집합(set), 딕셔너리(dictionary) 자료형에 대해 사용될 수 있다 방법 [ 표현식 for 변수 in iterable객체 ( if 조건식 ) ] { 표현식 for 변수 in iterable객체 ( if 조건식 ) } { 표현식(인데 key : value로) for 변수 in iterable객체 ( if 조건식 ) } 2. List Comprehension [ 표현식 for 변수 in iterable객체 ( if 조건식 ) ] A = [ ] for x in range(1,11): A.append(x*x) print(A) ''' [결과] [1, 4, 9,..
기존 메모리 관리의 문제점 필요 없는 메모리를 비우지 않았을 때 메모리 사용을 마쳤을 때 비우지 않을 경우 메모리 누수가 발생 장기적인 관점에서 심각한 문제가 발생 존재하지 않는 메모리에 접근하려고 하면 프로그램이 중단되거나 메모리 데이터 값이 손상될 수 있다 이러한 문제를 해결하기 위해 현대적인 언어는 자동 메모리 관리(Automatic Memory Management)를 갖추게 되었다. 파이썬에선 기본적으로 Garbage Collection(가비지 컬렉션)과 reference counting(레퍼런스 카운팅)을 통해 할당된 메모리를 관리한다 가비지컬렉션(Garbage Collection) 소멸 규칙 및 과정을 이야기하는 것으로 메모리를 자동으로 관리해주는 과정이다 레퍼런스 카운트 참조 횟수(refer..