Skip to content

Instantly share code, notes, and snippets.

View wenweixu's full-sized avatar

Wenwei Xu wenweixu

  • Pacific Northwest National Laboratory
  • Seattle WA
View GitHub Profile
@wenweixu
wenweixu / Sherlock_valid_string.py
Created August 25, 2019 18:49
Hackerrank Sherlock and the Valid String Python solution
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the isValid function below.
def isValid(s):
@wenweixu
wenweixu / min_abs_distance_in_array.py
Created August 27, 2019 00:51
Minimum Absolute Difference in an Array Hackerrank Python solution
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the minimumAbsoluteDifference function below.
def minimumAbsoluteDifference(arr):
@wenweixu
wenweixu / luck_balance.py
Created August 27, 2019 05:13
Hackerrank Luck Balance Python Solution
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the luckBalance function below.
def luckBalance(k, contests):
@wenweixu
wenweixu / greedy_florist.py
Last active June 12, 2020 06:21
Hackerrank Greedy Florist Python solution
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the getMinimumCost function below.
def getMinimumCost(k, c):
@wenweixu
wenweixu / max_min.py
Created August 27, 2019 20:42
Hackerrank Max Min python solution
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the maxMin function below.
def maxMin(k, arr):
@wenweixu
wenweixu / height_binary_tree.py
Last active November 11, 2020 09:30
Hackerrank Height of a Binary Tree Python solution
def height(root):
# condition to stop recursion
if root == None:
return -1
# divide and conquer
depth_left = height(root.left)
depth_right = height(root.right)
depth = max(depth_left, depth_right) + 1
# return the final result
return depth
@wenweixu
wenweixu / binary_search_tree_lca.py
Created September 1, 2019 23:47
Hackerrank Binary Search Tree Lowest Common Ancestor Python solution
def lca(root, v1, v2):
# compare v1 v2 and get the big and small value
if v1 > v2:
v_big, v_small = v1, v2
else:
v_big, v_small = v2, v1
current = root
while True:
if current.info > v_big:
@wenweixu
wenweixu / Huffman_Decoding.py
Created September 2, 2019 04:33
Hackerrank Huffman Decoding Python solution
def decodeHuff(root, s):
current = root
result = ''
for code in s:
if int(code) == 0:
current = current.left
else:
current = current.right
if current.left == None and current.right == None:
result += current.data
@wenweixu
wenweixu / check_binary_search_tree.py
Created September 2, 2019 06:02
Hackerrank Is This a Binary Search Tree Python solution
def checkBST(root):
def is_BST(root):
if root == None:
return True, None, None
bool_left, min_left, max_left = is_BST(root.left)
bool_right, min_right, max_right = is_BST(root.right)
if bool_left and bool_right:
if root.left==None and root.right==None:
return True, root.data, root.data
elif root.left==None and not root.right==None:
@wenweixu
wenweixu / ice_cream_parlor.py
Created September 2, 2019 18:32
Hackerrank Hash Tables - Ice Cream Parlor Python solution
def whatFlavors(cost, money):
cost_dict = {}
for i,icost in enumerate(cost):
if money-icost in cost_dict:
print(str(cost_dict[money-icost]+1) + ' ' + str(i+1))
return
else:
cost_dict[icost] = i