Skip to content

Instantly share code, notes, and snippets.

@GuillaumeTong
Last active March 5, 2020 10:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GuillaumeTong/062966b64f7c94a55ae1d5e4d8fa83ad to your computer and use it in GitHub Desktop.
Save GuillaumeTong/062966b64f7c94a55ae1d5e4d8fa83ad to your computer and use it in GitHub Desktop.
Oursky Developer Pre-Test 2020
# this function expects array1 and array2 to be iterables containing capital letter characters
def isSubset(array1, array2):
hashtable = dict.fromkeys(array1, True)
for x in array2:
if not hashtable.__contains__(x):
return False
return True
# # By using dict to store array1, we can get hashtable key lookup performance, which is O(1). Based on this,
# # the complexity of the function is overall O(n), as both array1 and array2 are only traversed once, and only an O(1)
# # action is performed on each array traversals
# # It is however notable that with only 26 possible symbols,
# # the complexity of this particular operation should bot be very important
# # Test code
# print(isSubset(['J', 'H', 'G', 'R'], ['J', 'R', 'G']))
# print(isSubset(['J', 'H', 'G', 'R'], ['J']))
# print(isSubset(['J', 'H', 'G', 'R'], ['R', 'J', 'G']))
# print(isSubset(['J', 'H', 'G', 'R'], ['L', 'J', 'G']))
# print(isSubset(['J', 'H', 'G', 'R'], ['L']))
#
# # Expected:
# # True
# # True
# # True
# # False
# # False
import time
import math
class Entry:
def __init__(self, value, weight, last_access_time):
self.value = value
self.weight = weight
self.last_access_time = last_access_time
class CacheStorage:
storage = {}
capacity = 10
def put(self, key, value, weight):
current_time = time.time()
self.storage[key] = Entry(value, weight, current_time)
if len(self.storage) > self.capacity:
min_score = math.inf
for i in self.storage:
e = self.storage[i]
if e.last_access_time == current_time:
score = e.weight / -100
else:
score = e.weight / math.log(current_time - e.last_access_time)
if min_score > score:
min_score = score
min_key = i
self.storage.pop(min_key)
def get(self, key):
return self.storage[key].value
# c = CacheStorage()
# c.put(1, 2, 3000)
# c.put(11, 2, 3)
# c.put(3, 2, 3)
# c.put(7, 2, 3)
# c.put(4, 2, 3)
# c.put(6, 2, 3)
# c.put(99, 2, 3)
# c.put(1000, 2, 3)
# c.put(78, 2, 3)
# c.put(2, 2, 3)
# c.put(90, 2, 1)
# c.put(20, 2, 3000000000000000)
# for i in c.storage:
# print(i, ": ", c.storage[i].value)

Answer 3

The bug is on line 3 of the code, where i is declared as var. This gives i too wide of a scope and causes the function definitions to interpret it as a dynamic variable instead of resolving it. The solution is to declare it as let, which gives:

function createArrayOfFunctions(y) {
    var arr = [];
    for(let i = 0; i < y; i++) {
        arr[i] = function(x) { return x + i; }
    }
    return arr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment