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
def myf(r,c):
return r+c
f = np.vectorize(myf)
r_arr = np.meshgrid(range(10000),range(10000))
c_arr = np.meshgrid(range(10000),range(10000))
start = time.time()
import time
import numpy as np
r_arr = np.meshgrid(range(10000),range(10000))
c_arr = np.meshgrid(range(10000),range(10000))
start = time.time()
z_arr = r_arr + c_arr
tic
for i = 1:10000
for j = 1:10000
z = i + j;
end
end
toc
import time
import numpy as np
start = time.time()
for r in range(10000):
for c in range(10000):
z = r + c
print(time.time() - start)
def mergeLists(head1, head2):
llist3 = SinglyLinkedList()
while head1 or head2:
if head1 == None:
llist3.insert_node(head2.data)
head2 = head2.next
elif head2 == None:
llist3.insert_node(head1.data)
head1 = head1.next
elif head1.data >= head2.data:
@wenweixu
wenweixu / Flipping_bits.py
Created September 3, 2019 03:29
Hackerrank Flipping_bits python solution
def flippingBits(n):
result = 0
for i in range(31,-1,-1):
if n//(2**i) == 1:
n = n - 2**i
x = 0
else:
x = 1
result += 2**i * x
@wenweixu
wenweixu / min_time_required.py
Created September 3, 2019 01:34
Minimum Time Required hackerrank python solution
def minTime(machines, goal):
# make a modest guess of what the days may be, and use it as a starting point
efficiency = [1.0/x for x in machines]
lower_bound = int(goal / sum(efficiency)) - 1
upper_bound = lower_bound + max(machines) + 1
while lower_bound < upper_bound -1:
days = (lower_bound + upper_bound)//2
produce = sum([days//x for x in machines])
if produce >= goal:
@wenweixu
wenweixu / pairs2.py
Created September 2, 2019 23:22
Pairs Hackerrank python solution 2: two pointers
def pairs(k, arr):
result = 0
arr = sorted(arr)
j = 1
for i in range(len(arr)-1):
while j<len(arr):
if arr[j] - arr[i] == k:
result += 1
j += 1
elif arr[j] - arr[i] > k:
@wenweixu
wenweixu / pairs.py
Created September 2, 2019 23:07
Hackerrank Pairs Python solution
# Complete the pairs function below.
def pairs(k, arr):
my_dict = {}
result = 0
for ele in arr:
my_dict[ele] = 1
if ele + k in my_dict:
result += 1
if ele -k in my_dict:
result += 1
@wenweixu
wenweixu / triple_sum.py
Created September 2, 2019 22:37
Triple Sum Hackerrank Python solution
def triplets(a, b, c):
# use set to get unique values, and sort the list
a = sorted(list(set(a)))
b = sorted(list(set(b)))
c = sorted(list(set(c)))
#initiate the number of options in a and c
ia, ic = 0, 0
result = 0
for ib in range(len(b)):
while ia < len(a):