본문 바로가기

Python345

Delete occurrences of an element if it occurs more than n times Description: Enough is enough! Alice and Bob were on a holiday. Both of them took many pictures of the places they've been, and now they want to show Charlie their entire collection. However, Charlie doesn't like these sessions, since the motive usually repeats. He isn't fond of seeing the Eiffel tower 40 times. He tells them that he will only sit during the session if they show the same motive .. 2022. 4. 2.
Jaden Casing Strings Description: Jaden Smith, the son of Will Smith, is the star of films such as The Karate Kid (2010) and After Earth (2013). Jaden is also known for some of his philosophy that he delivers via Twitter. When writing on Twitter, he is known for almost always capitalizing every word. For simplicity, you'll have to capitalize each word, check out how contractions are expected to be in the example bel.. 2022. 4. 1.
Python _ @classmethod, @staticmethod 란 무엇인가? 파이썬에서 클래스들을 살펴보면 가끔 뜬금없이 데코레이터가 등장하곤 합니다. 바로 @classmethod, @staticmethod 데코레이터입니다. 이 두 데코레이터를 왜 사용하는지 같이 살펴보겠습니다. 우선 다음처럼 클래스 코드를 작성하고 인스턴스를 만들겠습니다. class Robot: number = '0001' def __init__(self, name): self.name = name def 인스턴스메서드(self): print(f'인스턴스메서드 호출 {self.name}') @classmethod def 클래스메서드(cls): print(f'클래스메서드 호출 {cls.number}') @staticmethod def 스태틱메서드(): print('스태틱메서드 호출') robot = Robot('.. 2022. 3. 31.
Complementary DNA Description: Deoxyribonucleic acid (DNA) is a chemical found in the nucleus of cells and carries the "instructions" for the development and functioning of living organisms. If you want to know more: http://en.wikipedia.org/wiki/DNA In DNA strings, symbols "A" and "T" are complements of each other, as "C" and "G". You function receives one side of the DNA (string, except for Haskell); you need to.. 2022. 3. 31.
Python _ magic method를 사용하여 객체 커스텀하기 파이썬에는 magic method 라는 것이 있습니다. 흔히 __000__ 형태로 되어 있는 것을 의미하며 이는 파이썬 자체에 내장되어 있는 메서드들입니다. # magic method 예시 '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeo.. 2022. 3. 31.
Python _ isinstance로 타입을 체크하자. 어떤 객체가 무슨 타입인지를 알려면 type() 메서드를 활용하면 됩니다. 하지만 어떤 타입이 맞는지 참/거짓으로 체크만 하고 싶다면 isinstance()를 활용할 수 있습니다. 사용법은 간단합니다. isinstance 메서드에 첫 번째 인자로 해당 객체를, 두번째 인자로 체크하고 싶은 타입을 넣어주면 됩니다. def 체크_문자열(객체): return isinstance(객체, str) print(체크_문자열('문자')) # >>> True print(체크_문자열(123)) # >>> False 문자열을 넣었을 때는 True, 숫자를 넣었을 때는 False를 반환합니다. def 체크_리스트(객체): return isinstance(객체, list) print(체크_리스트('문자')) # >>> Fals.. 2022. 3. 30.