Skip to content

Instantly share code, notes, and snippets.

1 80,982 163,8164 170,2620 145,648 200,8021 173,2069 92,647 26,4122 140,546 11,1913 160,6461 27,7905 40,9047 150,2183 61,9146 159,7420 198,1724 114,508 104,6647 30,4612 99,2367 138,7896 169,8700 49,2437 125,2909 117,2597 55,6399
2 42,1689 127,9365 5,8026 170,9342 131,7005 172,1438 34,315 30,2455 26,2328 6,8847 11,1873 17,5409 157,8643 159,1397 142,7731 182,7908 93,8177
3 57,1239 101,3381 43,7313 41,7212 91,2483 31,3031 167,3877 106,6521 76,7729 122,9640 144,285 44,2165 6,9006 177,7097 119,7711
4 162,3924 70,5285 195,2490 72,6508 126,2625 121,7639 31,399 118,3626 90,9446 127,6808 135,7582 159,6133 106,4769 52,9267 190,7536 78,8058 75,7044 116,6771 49,619 107,4383 89,6363 54,313
5 200,4009 112,1522 25,3496 23,9432 64,7836 56,8262 120,1862 2,8026 90,8919 142,1195 81,2469 182,8806 17,2514 83,8407 146,5308 147,1087 51,22
6 141,8200 98,5594 66,6627 159,9500 143,3110 129,8525 118,8547 88,2039 83,4949 165,6473 162,6897 184,8021 123,13 176,3512 195,2233 42,7265 47,274 132,1514 2,8847 171,3722 3,9006
7 156,7027 1
@PeterStayPool
PeterStayPool / facebook_find_permutations_of_input_words.py
Created September 3, 2019 12:03
Permutate a list of string this question is supposed permutate the characters instead of who string, as input example {"red", "fox", "super" }, the expected output is rfs, rfu, rfp, rfe, rfr, ros, rou, rop, roe, ror, rxs, rxu, rxp, rxe, rxr, efs, efu, efp, efe, efr, eos, eou, eop, eoe, eor, exs, exu, exp, exe, exr, dfs, dfu, dfp, dfe, dfr, dos, …
class PermutateWords:
def permutate(self, input):
self.result = []
self.find_permutation(input, 0, [])
return self.result
def find_permutation(self, input, next_word_pos, found):
if len(found) == len(input) or next_word_pos >= len(input):
if len(found) == len(input):
self.result.append("".join(found))
@PeterStayPool
PeterStayPool / facebook_check_if_words_matches_the_regex_pattern.py
Created August 18, 2019 02:50
Pattern Matching ---------------- Characters: a to z Operators: * + * -> matches zero or more (of the character that occurs previous to this operator) + -> matches one or more (of the character that occurs previous to this operator) Output if a given pattern matches a string.
class PatternNode:
def __init__(self, char, count, repeat):
self.value = char
self.count = count
self.repeat = repeat
class WordNode:
def __init__(self, value, count):
self.value = value
class PhpParser:
def parse(self, tokens):
self.left_bracket = 0
self.is_class = False
self.function = False
self.double_quote = False
self.single_quote = False
class_map = {}
current_class = None
@PeterStayPool
PeterStayPool / facebook_find_steps_to_m_n.py
Created August 10, 2019 05:24
A robot has to move in a grid which is in the form of a matrix. It can go to 1.) A(i,j) --> A(i+j,j) (Down) 2.) A(i,j)--> A(i,i+j) (Right) Given it starts at (1,1) and it has to go to A(m,n), find the minimum number of STEPS it has to take to get to (m,n) and write public static int minSteps(int m,int n) For instance to go from (1,1) to m=3 and …
def find_min_steps(M, N):
m = M
n = N
count = 0
while m >=1 and n>=1:
if m > n:
if m - n >= 1:
count+=1
m = m - n
else:
@PeterStayPool
PeterStayPool / find_first_missing_number.py
Created August 8, 2019 03:26
Given input [2,3,1,5,88,100], find the first missing number. All number are positive numbers. (1 - n) In the above example, 4 is the first missing number.
class FindMissingNumber:
def find_missing_number(self, list):
max_len = len(list) - 1
for i, n in enumerate(list):
if i != n-1 and n - 1 <= max_len:
self.swap(list, i, n-1)
for i, n in enumerate(list):
@PeterStayPool
PeterStayPool / facebook_make_all_balances_perfectly_balanced.py
Created August 5, 2019 05:49
You have a room-full of balances and weights. Each balance weighs ten pounds and is considered perfectly balanced when the sum of weights on its left and right sides are exactly the same. You have placed some weights on some of the balances, and you have placed some of the balances on other balances. Given a description of how the balances are a…
class Balance:
def __init__(self, id):
self.id = id
self.left = self.right = None
self.lweight = self.rweight = 0
self.visited = False
self.base = 10
def total(self):
@PeterStayPool
PeterStayPool / facebook_implement_read_with_read4.py
Created August 4, 2019 23:33
Implement read using read4 class ArbitraryIO { private: // returns bytes read or 0, sizeof(buf) < 4 // reads up to 4 bytes at a time into caller allocated buf int read4(char* buf); public: // IMPLEMENT:toRead > 4 or < 4 or = 4, buf allocated by caller, return number of bytes read int read(char* buf, size_t toRead) {} };
class ArbitraryIO:
def __init__(self, data):
self.last_read_pos = 0
self.read_pos =0
self.tmp_buf = ""
self.data = data
def read4(self):
self.tmp_buf = data[self.read_pos: self.read_pos+4]
self.read_pos += len(self.tmp_buf)
@PeterStayPool
PeterStayPool / facebook_manage_rooms_with_balanced_ratio.py
Created August 4, 2019 15:35
Given the list of conference rooms with the capacity and the target occupancy percentages, write the program that maintains the balanced occupancy percentage across the rooms. Input: list of rooms { [30, 0.6], [50, 0.7], [90, 0.4]}, number of guests Output: list of rooms with balanced occupancy percentages across the rooms
class Room:
def __init__(self, capacity, target):
self.cap = capacity
self.target = target
self.count = 0
self.target_count = int(self.cap * self.target)
def occupancy(self):
return self.count *1.0 / self.cap
@PeterStayPool
PeterStayPool / facebook_guess_dictionary_word_linear_time.py
Created August 4, 2019 06:21
Assuming you're playing one game that you need guess a word from a dictionary. You're given a machine you can try to guess the word, the machine will return how many characters has been matched by your guess. Design a system to crack the word.
class GuessWord:
def __init__(self):
self.alphabet = 'abcdefghijklmnopqrstuvwxyz'
def guess(self, target):
self.target = target
found = self.find_target_length()
length = found[0]
target_chars = found[1]