Skip to content

Instantly share code, notes, and snippets.

View sheldonrobinson's full-sized avatar

Sheldon Robinson sheldonrobinson

View GitHub Profile
@sheldonrobinson
sheldonrobinson / isSubtree.py
Created October 6, 2019 01:02
CodeSignal Solution isSubtree
#
# Binary trees are already defined with this interface:
# class Tree(object):
# def __init__(self, x):
# self.value = x
# self.left = None
# self.right = None
def isSubtree(t1, t2):
def isEqual(left, right):
if left == None and right == None:
@sheldonrobinson
sheldonrobinson / hasPathWithGivenSum.py
Created October 6, 2019 00:53
CodeSignal Solution hasPathWithGivenSum
#
# Binary trees are already defined with this interface:
# class Tree(object):
# def __init__(self, x):
# self.value = x
# self.left = None
# self.right = None
def hasPathWithGivenSum(t, s):
if t is None:
if s==0:
@sheldonrobinson
sheldonrobinson / findProfession.py
Created October 6, 2019 00:47
CodeSignal Solution findProfession
def findProfession(level, pos):
# Base case
if (level == 1):
return 'Engineer'
# Recursively find parent's profession. If parent
# is a doctar, this node will be a doctal if it is
# at odd position and an engineer if at even position
if (findProfession(level-1, (pos+1)//2) == 'Doctor'):
if (pos%2):
@sheldonrobinson
sheldonrobinson / kthSmallestInBST.py
Created October 6, 2019 00:26
CodeSignal Solution kthSmallestInBST
#
# Binary trees are already defined with this interface:
# class Tree(object):
# def __init__(self, x):
# self.value = x
# self.left = None
# self.right = None
def kthSmallestInBST(t, k):
def inorder(r):
return inorder(r.left) + [r.value] + inorder(r.right) if r else []
@sheldonrobinson
sheldonrobinson / isTreeSymmetric.py
Created October 6, 2019 00:14
CodeSignal Solution isTreeSymmetric
#
# Binary trees are already defined with this interface:
# class Tree(object):
# def __init__(self, x):
# self.value = x
# self.left = None
# self.right = None
def isEqual(left, right):
if left == None and right == None:
@sheldonrobinson
sheldonrobinson / reverseNodesInKGroups.py
Created October 5, 2019 07:17
CodeSignal solution reverseNodesInKGroups
def reverseList(head, tail):
prev = None
while prev != tail:
prev, prev.next, head = head, prev, head.next
return prev
def reverseNodesInKGroups(l, k):
if k < 2:
return l
@sheldonrobinson
sheldonrobinson / rearrangeLastN
Created October 5, 2019 07:15
CodeSignal Solution rearrangeLastN
# Definition for singly-linked list:
# class ListNode(object):
# def __init__(self, x):
# self.value = x
# self.next = None
#
def rearrangeLastN(l, n):
if n == 0:
return l
front, back = l, l
@sheldonrobinson
sheldonrobinson / mergeTwoLinkedLists.py
Created October 5, 2019 07:03
CideSignal Solution mergeTwoLinkedLists
# Singly-linked lists are already defined with this interface:
# class ListNode(object):
# def __init__(self, x):
# self.value = x
# self.next = None
#
def mergeTwoLinkedLists(l1, l2):
if l1 == None and l2 == None:
return None
@sheldonrobinson
sheldonrobinson / addTwoHugeNumbers.py
Created October 5, 2019 04:06
CodeSignal Solution addTwoHugeNumbers
# Singly-linked lists are already defined with this interface:
# class ListNode(object):
# def __init__(self, x):
# self.value = x
# self.next = None
#
def addTwoHugeNumbers(a, b):
lst_a = {}
lst_b = {}
i=0
@sheldonrobinson
sheldonrobinson / removeKFromList.py
Created October 5, 2019 02:35
CodeSignal Solution removeKFromList
# Singly-linked lists are already defined with this interface:
# class ListNode(object):
# def __init__(self, x):
# self.value = x
# self.next = None
#
def removeKFromList(l, k):
if l == None:
return l
while l != None and l.value == k: