일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
- LeetCode
- Convert Sorted List to Binary Search Tree
- Regular Expression
- 30. Substring with Concatenation of All Words
- 109. Convert Sorted List to Binary Search Tree
- data science
- 컴퓨터의 구조
- DWG
- Decorator
- 프로그래머스
- concurrency
- 715. Range Module
- 운영체제
- Substring with Concatenation of All Words
- shiba
- Class
- 파이썬
- 시바견
- Python Implementation
- Python
- attribute
- kaggle
- 43. Multiply Strings
- Python Code
- 밴픽
- Protocol
- Generator
- 315. Count of Smaller Numbers After Self
- t1
- iterator
- Today
- Total
목록Computer Science (392)
Scribbling
What is a "Pythonic Object"? An object with many different magic methods seems to be more pythonic. However, it doesn't necessarily refer to a class with a lot of magic methods. That is, we don't have to implement all the magic methods in every class. Instead, implement them if only needed, because 'being simple' is pythonic. Below is Vector2D class example. from array import array import math c..
ID operator returns the object's memory address. We use it to compare objects' identities. morgan = {'name': 'hoesu', 'age':29} hoesu = morgan print(hoesu is morgan) print(id(hoesu), id(morgan)) "==" operator calls __eq__() method in dict class and checks whether the objects have the same value or not. morgan = {'name': 'hoesu', 'age':29} anonymous = {'name': 'hoesu', 'age':29} print(morgan == a..
Decorator is a callable that takes another function as its parameter. Decorator is executed during 'import time'. The key idea about closure you must understand is that it can access to 'nonlocal' variables outside the function. Take a look at the below exmaple. def make_averager(): series = [] def averager(new_val): series.append(new_val) return sum(series) / len(series) return averager avg = m..

Some traditional design patterns can be simplified by making good use of first-order functions in python. Let's see the below example. 1. Goal: Calculate total price and discount for each order 2. There are three discount strategies; - fidelitypromo: if the customer's fidelity >= 1000, then 5% of the total - bulkitempromo: if q >= 20 for an item, then 10% of the item - largeorderpromo: if type >..
First thing I came out with was to use a trie data structure, as it helps us deal with characters efficiently. class StreamChecker: def __init__(self, words: List[str]): self.trie = {} for word in words: node = self.trie for char in word: if char not in node: node[char] = {} node = node[char] node['#'] = '#' self.candidates = [] def query(self, letter: str) -> bool: self.candidates.append(self.t..
Python memoryview function returns memory view objects. So why use it? Memory view is a safe way to expose 'buffer protocol' in python. Buffer protocol allows an object to expose its inner data without copying. As buffer protocol is only available at the C-API level, memoryview is present to expose the same protocol at Python level. Example1: how memoryview works arr = bytearray('XYZ', 'utf-8') ..
1. Using heaps with lazy removal Below code uses 'MyHeap' data structure, a heap with lazy removal. Lazy removal here means, if we have to remove an element, we handle it by using another heap as a trash can. import heapq class MyHeap: def __init__(self, type='min'): self.heap = [] self.removals = [] self.sign = 1 if type == 'min' else -1 def insert(self, elem): heapq.heappush(self.heap, self.si..
Immutable dictionary: MappingProxyType Immutable Set: Frozenset from types import MappingProxyType d = {'a': 'apple', 'b': 'banana', 'c': 'carrot'} d_fixed = MappingProxyType(d) print(d_fixed['a']) s = frozenset([1, 2, 3]) print(s)
1. List / Generator Comprehension Using list / generator comprehension makes code more readable while enhancing efficiency. suits = ['Spades', 'Diamonds', 'Hearts', 'Clubs'] ranks = [str(i) for i in range(2, 11)] + list('JQKA') for card in (f"{suit} {rank}" for suit in suits for rank in ranks): print(card) 2. Tuple Unpacking a, b, *rest = range(1, 5) print(a, b, rest) 3. NamedTuple Namedtuple is..
We need to deal with ranges to solve this problem. Below is the link of a wonderful solution. https://leetcode.com/problems/range-module/discuss/244194/Python-solution-using-bisect_left-bisect_right-with-explanation Python solution using bisect_left, bisect_right with explanation - LeetCode Discuss Level up your coding skills and quickly land a job. This is the best place to expand your knowledg..