일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- kaggle
- 30. Substring with Concatenation of All Words
- concurrency
- Class
- Regular Expression
- 43. Multiply Strings
- Decorator
- LeetCode
- 109. Convert Sorted List to Binary Search Tree
- 시바견
- Python
- 715. Range Module
- Python Code
- Python Implementation
- 프로그래머스
- 밴픽
- 315. Count of Smaller Numbers After Self
- Protocol
- t1
- DWG
- Substring with Concatenation of All Words
- 운영체제
- Generator
- 컴퓨터의 구조
- Convert Sorted List to Binary Search Tree
- data science
- 파이썬
- attribute
- shiba
- iterator
- Today
- Total
목록shiba (41)
Scribbling
Web downloads in three different styles - sequential download - concurrent.futures - asyncio With heavy input/output workload, we can make the best out of concurrency. Below is the code that downloads flag gif files sequentially. import os import time import sys import requests POP20_CC = ('CN IN US ID BR PK NK BD RU JP' 'MX PH VN ET EG DE IR TR CD FR').split() BASE_URL = 'http://flupy.org/data/..
Basic behavior of Coroutines def coroutine(): print('started') x = yield print('received: ', x) c = coroutine() next(c) c.send(3) 1) "next(c)" or "c.send(None)" primes the coroutine -> coroutine now waits at 'yield' expression 2) c.send(3) sets x to 3, re-executing the coroutine 3) At the end, coroutine raises StopIteration Example: coroutine to compute a running average def averager(): total, c..
For a given recipe, we need to check whether all the necessary elements can be supplied. If an element belongs to supplies, it's a piece of cake. Now the question is to deal with elements that belong to recipes. We can think of recipes as a graph with cycles. Plus, if there's a cycle among certain recipes, they can never be cooked. class Solution: def findAllRecipes(self, recipes: List[str], ing..
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..
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..
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..
In this post, I am dealing with 'magic methods' to create more pythonic data models. First Example: Card Deck Card = namedtuple('Card', ['rank', 'suit']) class FrenchDeck: ranks = [str(n) for n in range(2, 11)] + list('JQKA') suits = ['Spades', 'Diamonds', 'Hearts', 'Clubs'] def __init__(self): self._cards = [Card(rank, suit) for suit in FrenchDeck.suits for rank in FrenchDeck.ranks] def __len__..
The very first thing to do is looking for matches. We don't need to use any fancy algorithms when looking for matches in this problem because it is guranteed that replacements will not overlap. Now we have matched list in the form of [index, source, target]. If you are planning to replace s directly, it will lead to bad time complexity as you will have to copy all the characters of s each time y..
class Solution: def calculate(self, s: str) -> int: # ' ' is intentionally added, # to mark the end of the given string s # encountering ' ' will add the last number to the stack self.s = s + ' ' self.ptr = 0 return self.calc() def calc(self): num, stack, sign = 0, [], '+' while self.ptr < len(self.s): c = self.s[self.ptr] # if met with '(', # get the evaluated number by recursion if c == '(': s..
Using double trie structures for prefix and suffix. Somewhat straightforward if one already knows about trie. If not, refer to "LeetCode prob 208. Implement Trie (Prefix Tree)". class WordFilter: def __init__(self, words: List[str]): wordDict = {} for idx, word in enumerate(words): wordDict[word] = idx self.PrefixTree = {} self.SuffixTree = {} for word, idx in wordDict.items(): node = self.Prefi..