This file contains hidden or 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 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() |
This file contains hidden or 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 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 |
This file contains hidden or 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
| tic | |
| for i = 1:10000 | |
| for j = 1:10000 | |
| z = i + j; | |
| end | |
| end | |
| toc |
This file contains hidden or 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 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) |
This file contains hidden or 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 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: |
This file contains hidden or 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 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 |
This file contains hidden or 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 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: |
This file contains hidden or 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 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: |
This file contains hidden or 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
| # 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 |
This file contains hidden or 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 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): |
NewerOlder