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
def find_sum_eq_k(l ,k): | |
l = sorted(l) | |
start ,end = 0 ,len(l)-1 | |
found = False | |
while(found != True and end >= 0 and start < len(l)): | |
if(l[start] + l[end] == k): | |
found=True | |
elif(l[start] + l[end] < k): | |
start += 1 | |
else: |
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,[1, 2, 3, 4, 5], the expected output would be [120, 60, 40, 30, 24]. | |
If our input was [3, 2, 1], the expected output would be [2, 3, 6]. | |
''' | |
def find_prod(l): | |
''' | |
sum = log10(A) + log10(B) + log10(C) | |
prod(A*B*C) = antilog(sum) | |
antilog = base ** exponent = 10 ** sum |
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
# Definition for a binary tree node. | |
# class TreeNode(object): | |
# def __init__(self, x): | |
# self.val = x | |
# self.left = None | |
# self.right = None | |
class Serialize_To_Deserialize: | |
def serialize(self, root): | |
"""Encodes a tree to a single string. |
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, the input [3, 4, -1, 1] should give 2. The input [1, 2, 0] should give 3. | |
''' | |
def first_missing_positive(nums: List[int]) -> int: | |
length=len(nums) | |
if length==0: | |
return 1 | |
j=0 | |
for i,val in enumerate(nums): | |
if val<=0 or val>(length): |
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, car(cons(3, 4)) returns 3, and cdr(cons(3, 4)) returns 4. | |
''' | |
class cons: | |
def __init__(self,a,b): | |
self.a=a | |
self.b=b | |
@staticmethod | |
def car(cls): | |
return cls.a |
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 _ctypes | |
def di(ptr_id): | |
return _ctypes.PyObj_FromPtr(ptr_id) | |
class Node: | |
def __init__(self,data): | |
self.data=data | |
self.xpr=0 | |
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, the message '111' would give 3, since it could be decoded as 'aaa', 'ka', and 'ak'. | |
''' | |
def decode_msg(msg): | |
n=len(msg) | |
if n==0 or msg=='0' or msg[0]=='0': | |
return 0 | |
n=len(msg) | |
count=[0]*(n+1) | |
count[0]=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
''' | |
For example, the following tree has 5 unival subtrees: | |
0 | |
/ \ | |
1 0 | |
/ \ | |
1 0 | |
/ \ | |
1 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
''' | |
For example, [2, 4, 6, 2, 5] should return 13, since we pick 2, 6, and 5. | |
[5, 1, 1, 5] should return 10, since we pick 5 and 5. | |
''' | |
def max_sum_non_adjacent(nums): | |
if len(nums)==0: | |
return 0 | |
if len(nums)==1: | |
if nums[0]<=0: | |
return 0 |
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 threading | |
import time | |
def process_1(n_sec=0): | |
print(f"Process_1 started at {time.strftime('%H-%M-%S',time.localtime())}" \ | |
f" by {threading.current_thread().name}\n") | |
print(f"Process_1 sleeps for {n_sec} seconds") | |
time.sleep(n_sec) | |
print(f"Process_1 woke up at {time.strftime('%H-%M-%S',time.localtime())}") | |
OlderNewer