Add coding exercises for Facebook interviews
Add attempts at solving coding problems to Briefcase.
This commit is contained in:
		
							parent
							
								
									d2d772e43e
								
							
						
					
					
						commit
						aa66d9b83d
					
				
					 66 changed files with 2994 additions and 0 deletions
				
			
		
							
								
								
									
										71
									
								
								scratch/facebook/mst.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										71
									
								
								scratch/facebook/mst.py
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,71 @@ | |||
| from heapq import heappush, heappop | ||||
| import random | ||||
| 
 | ||||
| def to_vertex_list(graph): | ||||
|     result = {} | ||||
|     for a, b, kg in graph: | ||||
|         if a in result: | ||||
|             result[a].append((b, kg)) | ||||
|         else: | ||||
|             result[a] = [(b, kg)] | ||||
|         if b in result: | ||||
|             result[b].append((a, kg)) | ||||
|         else: | ||||
|             result[b] = [(a, kg)] | ||||
|     return result | ||||
| 
 | ||||
| def mst(graph): | ||||
|     graph = to_vertex_list(graph) | ||||
|     beg = random.choice(list(graph.keys())) | ||||
|     h = [] | ||||
|     result = [] | ||||
|     seen = set() | ||||
|     for c, kg in graph[beg]: | ||||
|         heappush(h, (kg, beg, c)) | ||||
|     while h: | ||||
|         kg, beg, end = heappop(h) | ||||
|         # detect cycles | ||||
|         if end in seen: | ||||
|             continue | ||||
|         # use the edge | ||||
|         seen.add(beg) | ||||
|         seen.add(end) | ||||
|         result.append((beg, end)) | ||||
|         for c, kg in graph[end]: | ||||
|             heappush(h, (kg, end, c)) | ||||
|     return result | ||||
| 
 | ||||
| graphs = [ | ||||
|     [ | ||||
|         ('A', 'B', 7), | ||||
|         ('A', 'D', 5), | ||||
|         ('B', 'D', 9), | ||||
|         ('E', 'D', 15), | ||||
|         ('F', 'D', 6), | ||||
|         ('F', 'G', 11), | ||||
|         ('F', 'E', 8), | ||||
|         ('G', 'E', 9), | ||||
|         ('C', 'E', 5), | ||||
|         ('B', 'E', 7), | ||||
|         ('B', 'C', 8), | ||||
|     ], | ||||
|     [ | ||||
|         ('A', 'B', 4), | ||||
|         ('A', 'C', 8), | ||||
|         ('B', 'C', 11), | ||||
|         ('B', 'E', 8), | ||||
|         ('C', 'D', 7), | ||||
|         ('C', 'F', 1), | ||||
|         ('D', 'E', 2), | ||||
|         ('D', 'F', 6), | ||||
|         ('E', 'G', 7), | ||||
|         ('E', 'H', 4), | ||||
|         ('F', 'H', 2), | ||||
|         ('G', 'H', 14), | ||||
|         ('G', 'I', 9), | ||||
|         ('H', 'I', 10), | ||||
|     ], | ||||
| ] | ||||
| 
 | ||||
| for graph in graphs: | ||||
|     print(mst(graph)) | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue