This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
For example, given the query string 'de' and | |
the set of strings [dog, deer, deal], | |
return [deer, deal]. | |
''' | |
class TrieNode: | |
def __init__(self): | |
self.next_chars=[None for i in range(26)] | |
self.is_end=False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
For example, if N is 4, then there are 5 unique ways: | |
1, 1, 1, 1 | |
2, 1, 1 | |
1, 2, 1 | |
1, 1, 2 | |
2, 2 | |
''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
For example, given s = "abcba" and k = 2, the longest substring with k distinct characters is "bcb". | |
''' | |
def long_substr(s,k): | |
long_st=0 | |
has_found=0 | |
dq=[] | |
mapp=dict() | |
for i,ch in enumerate(s): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
def dst_to_origin(x,y): | |
return np.sqrt(x*x + y*y) | |
def monte_carlo_pi(): | |
''' | |
sq_area=(2*r)**2 | |
cir_area=pi*(r**2) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
def pick_random(l): | |
''' | |
l(list) : imagine stream is passed as list | |
in loop | |
''' | |
count=1 | |
x=l[0] | |
print(x) | |
for i in l[1:]: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Record_Last_N: | |
def __init__(self,last_n): | |
self.ord_list=[] | |
self.last_n=last_n | |
def record(self,order_id): | |
self.ord_list.append(r_id) | |
if len(self.ord_list) > self.last_n: | |
self.ord_list.pop(0) | |
def get_last(self,i): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Suppose we represent our file system by a string in the following manner: | |
The string "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext" represents: | |
dir | |
subdir1 | |
subdir2 | |
file.ext | |
The directory dir contains an empty sub-directory subdir1 and a sub-directory subdir2 containing a file file.ext. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
For example, given array = [10, 5, 2, 7, 8, 7] and k = 3, we should get: [10, 7, 8, 8], since: | |
10 = max(10, 5, 2) | |
7 = max(5, 2, 7) | |
8 = max(2, 7, 8) | |
8 = max(7, 8, 7) | |
''' | |
from collections import deque | |
def largest_subarray(arr,n,k): | |
''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
import cv2 | |
from PIL import Image | |
from skimage.filters import threshold_local | |
import os | |
def plot_img(img,is_gray=True): | |
fig=plt.figure(figsize=(10,15)) | |
ax=fig.add_subplot(111) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
import cv2 | |
from PIL import Image | |
from skimage.filters import threshold_local | |
import os |