목록2022/01 (70)
Developer_Neo
네스티드 함수(Nested Function) - 중첩 함수로써 함수 안에 정의된 함수를 의미한다. 예로 살펴보자 def maker(m): def inner(n): return m*n return inner fl=maker(2) f1(7) # 14 위에서 inner함수가 네스티드 함수가 된다. 이게 왜 가능하냐? 파이썬의 함수는 객체이다 이렇기 때문에 다른함수를 호출할때 인자로도 전달이 가능하기 때문이다. 위의 코드에서 maker함수에서 m이 벗어나면 m은 사라지게 되는데 어떻게 inner함수가 알고선 계산을 올바르게 하는가? 클로저로 알아보자 클로저 위의 질문에 답변을 해보자면 정의한 inner함수가 변수 m의 값을 어딘가에 살짝 저장해두고 쓴다는 것이다. 따라서 안쪽에 위치한 네스티드 함수가 자신이 필..
[python] 연산자 오버로딩, 정보은닉과 , __dict__ [python] 연산자 오버로딩, 정보은닉과 , __dict__ 연산자 오버로딩 __add__(self, other) : + 연산자(A + B또는 A += B) __iadd__(self, other) : + 연산자(A += B) __sub__(self, other) : - 연산자(A - B, A -= B) __isub__(self, other) : - 연산자(A -= B) __.. devloper-dreaming.tistory.com 에서 보았듯이 정보은닉관점에서 객체가 갖는 값에 직접 접근하는 것은 오류의 확률을 높일 수 있으므로 메소드를 통해 접근하는 것이 안전하다고 했었다. 그래서 나온 메소드들은 getter와 setter인데 메소드 ..
연산자 오버로딩 __add__(self, other) : + 연산자(A + B또는 A += B) __iadd__(self, other) : + 연산자(A += B) __sub__(self, other) : - 연산자(A - B, A -= B) __isub__(self, other) : - 연산자(A -= B) __mul__(self, other) : * 연산자(A * B, A *= B) __truediv__(self, other) : / 연산자 __floordiv__(self, other) : // 연산자 __mod__(self, other) : % 연산자 __pow__(self, other) : ** 연산자(pow(A, B)) __and__(self, other) : and 연산자 __not__(sel..
파이썬의 클래스 역시 상속이라는 것을 할 수 있다. class Father: def run(self): print("so fast!!!") class Son(Father): def jump(self): print("so high!!!") def main(): s = Son() s.run() s.jump() main() ''' so fast!!! so high!!! ''' 위의 코드를 보면 클래스가 Father와 Son이 있는 것을 볼 수 있다. 여기에서 상속이라는 것을 했는데 바로 class Son(Father): 이 문장이었다. 이렇게 상속을 하게 되면 Son은 자식클래스이자 서브, 하위클래스가 되고 Father는 부모클래스이자 슈퍼, 상위 클래스가 된다. 따라서 Son클래스를 이용해서 만든 객체는 F..
https://www.acmicpc.net/problem/1152 1152번: 단어의 개수 첫 줄에 영어 대소문자와 공백으로 이루어진 문자열이 주어진다. 이 문자열의 길이는 1,000,000을 넘지 않는다. 단어는 공백 한 개로 구분되며, 공백이 연속해서 나오는 경우는 없다. 또한 문자열 www.acmicpc.net 문제 영어 대소문자와 공백으로 이루어진 문자열이 주어진다. 이 문자열에는 몇 개의 단어가 있을까? 이를 구하는 프로그램을 작성하시오. 단, 한 단어가 여러 번 등장하면 등장한 횟수만큼 모두 세어야 한다. 입력 첫 줄에 영어 대소문자와 공백으로 이루어진 문자열이 주어진다. 이 문자열의 길이는 1,000,000을 넘지 않는다. 단어는 공백 한 개로 구분되며, 공백이 연속해서 나오는 경우는 없다...
https://www.acmicpc.net/problem/10809 10809번: 알파벳 찾기 각각의 알파벳에 대해서, a가 처음 등장하는 위치, b가 처음 등장하는 위치, ... z가 처음 등장하는 위치를 공백으로 구분해서 출력한다. 만약, 어떤 알파벳이 단어에 포함되어 있지 않다면 -1을 출 www.acmicpc.net 문제 알파벳 소문자로만 이루어진 단어 S가 주어진다. 각각의 알파벳에 대해서, 단어에 포함되어 있는 경우에는 처음 등장하는 위치를, 포함되어 있지 않은 경우에는 -1을 출력하는 프로그램을 작성하시오. 입력 첫째 줄에 단어 S가 주어진다. 단어의 길이는 100을 넘지 않으며, 알파벳 소문자로만 이루어져 있다. 출력 각각의 알파벳에 대해서, a가 처음 등장하는 위치, b가 처음 등장하는 ..
https://www.acmicpc.net/problem/3052 3052번: 나머지 각 수를 42로 나눈 나머지는 39, 40, 41, 0, 1, 2, 40, 41, 0, 1이다. 서로 다른 값은 6개가 있다. www.acmicpc.net 문제 두 자연수 A와 B가 있을 때, A%B는 A를 B로 나눈 나머지 이다. 예를 들어, 7, 14, 27, 38을 3으로 나눈 나머지는 1, 2, 0, 2이다. 수 10개를 입력받은 뒤, 이를 42로 나눈 나머지를 구한다. 그 다음 서로 다른 값이 몇 개 있는지 출력하는 프로그램을 작성하시오. 입력 첫째 줄부터 열번째 줄 까지 숫자가 한 줄에 하나씩 주어진다. 이 숫자는 1,000보다 작거나 같고, 음이 아닌 정수이다. 출력 첫째 줄에, 42로 나누었을 때, 서로 ..
https://www.acmicpc.net/problem/2675 2675번: 문자열 반복 문자열 S를 입력받은 후에, 각 문자를 R번 반복해 새 문자열 P를 만든 후 출력하는 프로그램을 작성하시오. 즉, 첫 번째 문자를 R번 반복하고, 두 번째 문자를 R번 반복하는 식으로 P를 만들면 된다 www.acmicpc.net 문제 문자열 S를 입력받은 후에, 각 문자를 R번 반복해 새 문자열 P를 만든 후 출력하는 프로그램을 작성하시오. 즉, 첫 번째 문자를 R번 반복하고, 두 번째 문자를 R번 반복하는 식으로 P를 만들면 된다. S에는 QR Code "alphanumeric" 문자만 들어있다. QR Code "alphanumeric" 문자는 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\..