Skip to content

Instantly share code, notes, and snippets.

View santhalakshminarayana's full-sized avatar

Santha Lakshmi Narayana santhalakshminarayana

View GitHub Profile
@santhalakshminarayana
santhalakshminarayana / Daily_Coding_Problem_1.py
Created August 31, 2019 14:28
Given a list of numbers and a number k, return whether any two numbers from the list add up to k.
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:
@santhalakshminarayana
santhalakshminarayana / Daily_Coding_Problem_2.py
Last active August 31, 2019 14:53
Given an array of integers, return a new array such that each element at index i of the new array is the product of all the numbers in the original array except the one at i.
'''
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
@santhalakshminarayana
santhalakshminarayana / Daily_Coding_Problem_3.py
Created September 1, 2019 06:38
Given the root to a binary tree, implement serialize(root), which serializes the tree into a string, and deserialize(s), which deserializes the string back into the tree.
# 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.
@santhalakshminarayana
santhalakshminarayana / Daily_Coding_Problem_4.py
Created September 1, 2019 08:56
Given an array of integers, find the first missing positive integer in linear time and constant space. In other words, find the lowest positive integer that does not exist in the array. The array can contain duplicates and negative numbers as well.
'''
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):
@santhalakshminarayana
santhalakshminarayana / Daily_Coding_Problem_5.py
Created September 1, 2019 09:08
cons(a, b) constructs a pair, and car(pair) and cdr(pair) returns the first and last element of that pair.
'''
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
@santhalakshminarayana
santhalakshminarayana / Daily_coding_Problem_6.py
Created September 4, 2019 15:58
An XOR linked list is a more memory efficient doubly linked list. Instead of each node holding next and prev fields, it holds a field named both, which is an XOR of the next node and the previous node. Implement an XOR linked list; it has an add(element) which adds the element to the end, and a get(index) which returns the node at index.
import _ctypes
def di(ptr_id):
return _ctypes.PyObj_FromPtr(ptr_id)
class Node:
def __init__(self,data):
self.data=data
self.xpr=0
@santhalakshminarayana
santhalakshminarayana / Daily_coding_Problem_7.py
Created September 4, 2019 18:08
Given the mapping a = 1, b = 2, ... z = 26, and an encoded message, count the number of ways it can be decoded.
'''
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
@santhalakshminarayana
santhalakshminarayana / Daily_coding_Problem_8.py
Created September 4, 2019 18:52
Given the root to a binary tree, count the number of unival subtrees.A unival tree (which stands for "universal value") is a tree where all nodes under it have the same value.
'''
For example, the following tree has 5 unival subtrees:
0
/ \
1 0
/ \
1 0
/ \
1 1
@santhalakshminarayana
santhalakshminarayana / Daily_coding_Problem_9.py
Created September 5, 2019 13:53
Given a list of integers, write a function that returns the largest sum of non-adjacent numbers. Numbers can be 0 or negative.
'''
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
@santhalakshminarayana
santhalakshminarayana / Daily_coding_Problem_10.py
Created September 5, 2019 18:16
Implement a job scheduler which takes in a function f and an integer n, and calls f after n milliseconds.
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())}")