Python
-
프로그래머스(Programmers)_Level2_올바른괄호Python/Coding_Problems 2022. 12. 10. 13:05
첫번째시도 def solution(s): stack = [] for i in s: if len(stack) > 0 and stack[-1] != i: stack.pop() else: stack.append(i) return True if len(stack) == 0 else False 정확성: 50.2 효율성: 30.5 합계: 80.7 / 100.0 3,4,5,11,18 틀림 두번째시도 def solution(s): stack = [] if s[0] == ")": return False else: for i in s: if len(stack) > 0 and stack[-1] != i: stack.pop() else: stack.append(i) return True if len(stack) == 0 el..
-
프로그래머스(Programmers)_Level1_같은숫자는싫어Python/Coding_Problems 2022. 12. 10. 11:02
# 처음에 생각한것 s = list("baabaa") for i in range(len(s)-1): if s[i] == s[i+1]: wanted_remove_index = [i, i+1] while len(wanted_remove_index): s = [v for idx_s, v in enumerate(s) if idx_s not in wanted_remove_index] wanted_remove_index.clear() 위에처럼 하면은.. wanted_remove_ndex = [1,2] 라고 할 때 i는 1인 상태에서 위의 인덱스를 뽑아낸것.. 무튼.. 저걸 다 처리하고나면은 1,2의 인덱스가 없어진 [b,b,a,a] 가 되는데! for문의 i는 1에서 2로 넘어간다. 따랏 [b,b,a,a]에서 a부..
-
Python에서 가상환경 만들기Python/Basic 2022. 11. 30. 22:38
1. venv : Python 3.3 버전 이후 부터 기본모듈에 포함됨 # 가상환경 만들기 python3 -m venv 가상환경이름 # 가상환경 실행 가상환경이름\Scripts\activate 2. virtualenv: Python 2 버전부터 사용해오던 가상환경 라이브러리, Python 3에서도 사용가능 # pip 업그레이드 하기 python -m pip install --upgrade pip # virtualenv 설치하기 python3 -m pip install --user -U virtualenv # 가상환경 만들기 virtualenv 가상환경이름 3. conda: Anaconda Python을 설치했을 시 사용할 수 있는 모듈 머신러닝, 데이터과학 분야의 다양한 라이브러리들이 설치된 런타임인 아..
-
__str__ vs __repr__Python/OOP 2022. 11. 27. 20:42
__str__ To customize the string representation of a class instance, the class needs to implement the __str__ magic method. Internally, Python will call the __str__ method automatically when an instance calls the str() method. class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age= age def __str__(self): return f'이름: {self.fi..
-
프로그래머스(Programmers) LEVEL2 멀리뛰기Python 2022. 10. 26. 22:50
from itertools import permutations def func2(*argv): result = [] perm = permutations(*argv) for i in list(perm): result.append(i) return list(set(result)) def func(n): lis = [1] lis2 = [2] answer =0 for i in range(1,n): res = n-i if res % 2 == 0: # 1을 i개 만큼 쓸 수 있다는 이야기 result = lis*i counted_two = (n-sum(result)) // 2 result2 = counted_two * lis2 final = result2 + result answer += len(func2(fina..
-
__getitem__Python/Basic 2022. 10. 13. 16:26
# __getitem__은 클래스의 인덱스에 접근할 때 자동으로 호출되는 메서드 class 클래스이름: def __getitem__(self, 인덱스): 코드 class test: def __init__(self): print("test클래스의 생성자입니다") self.numbers = [i for i in range(1,11)] def __getitem__(self, index): print("__getitem__메서드 호출") return self.numbers obj = test() print(obj[0]) # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] class test: def __init__(self): print("test클래스의 생성자입니다") self.numbers = [i..
-
데코레이터(Decorator)Python/Basic 2022. 10. 12. 10:07
# 데코레이터 함수, 꾸며주는 함수 # when to use?! - 함수를 수정하지 않은 상태에서 추가 기능을 구현할 때 사용 =================== def print_hello(): print("안녕하세요") def add_print_to(original): def wrapper(): print("함수 시작") original() print("함수끝") return wrapper # add_print_to함수가 print_hello함수를 꾸며준다 print_hello = add_print_to(print_hello) print_hello() ================================ 데코레이터를 써보자 def add_print_to(original): def wrapper(..
-
Django로 이메일을 보내보자Python/Django 2022. 10. 11. 09:06
안정적으로 수많은 유저에게 이메일을 도달시키기 위해서는 전문적인 이메일 발송 서비스를 사용해야 한다 네이버 로그인 후 아래처럼 해준다 사진3 # 만약 네이버 2단계 인증이 설정되어 있다면 를 설정해야 한다 https://nid.naver.com/ 일반적으로 계정정보들은 소스코드에 하드코딩하지 않는다 환경변수에 저장하고 이를 로딩하여 활용 .env 파일을 생성하고 계정정보를 적어보자 # .env 파일 포맷의 주의점! EMAIL_HOST=smtp.naver.com 할때 " = " 앞뒤로 띄어쓰기를 하면 안된다 .env파일들(.env.development, .env.production 등 ) 은 절대 버전관리에 넣지 않는다 왜냐하면 env파일들에 있는 계정정보들이 Github공계계정에 올라가면 악용할 수 있기..