subtree(users/wpcarro): docking briefcase at '24f5a642'
				
					
				
			git-subtree-dir: users/wpcarro git-subtree-mainline:464bbcb15cgit-subtree-split:24f5a642afChange-Id: I6105b3762b79126b3488359c95978cadb3efa789
This commit is contained in:
		
						commit
						019f8fd211
					
				
					 766 changed files with 175420 additions and 0 deletions
				
			
		|  | @ -0,0 +1,41 @@ | |||
| # take-aways: | ||||
| #   - Use integers as lists of boolean values | ||||
| #   - Use 1 << n to compute 2^n where n = len(xs) | ||||
| 
 | ||||
| def set_from_int(xs, n): | ||||
|     result = [] | ||||
|     for i in range(len(xs)): | ||||
|         if n & (1 << i) != 0: | ||||
|             result.append(xs[i]) | ||||
|     return result | ||||
| 
 | ||||
| # subsets :: Set a -> List (Set a) | ||||
| def subsets(xs): | ||||
|     n = len(xs) | ||||
|     return [set_from_int(xs, i) for i in range(1 << n)] | ||||
| 
 | ||||
| #   0 1 2 | ||||
| # 0 N Y Y | ||||
| # 1 _ N Y | ||||
| # 2 _ _ N | ||||
| 
 | ||||
| # For my interview, be able to compute *permutations* and *combinations* | ||||
| 
 | ||||
| # This differs from permutations because this is about finding combinations... | ||||
| # | ||||
| # bottom-up | ||||
| # 0 =>        { } | ||||
| # 1 =>  {3}   {4}   {3} | ||||
| # 2 => {5,4} {5,3} {4,3} | ||||
| 
 | ||||
| xs = [ | ||||
|     ([], [[]]), | ||||
|     ([5], [[], [5]]), | ||||
|     ([5,4], [[],[5],[4],[5,4]]), | ||||
| ] | ||||
| 
 | ||||
| for x, expected in xs: | ||||
|     result = subsets(x) | ||||
|     print("subsets({}) => {} == {}".format(x, result, expected)) | ||||
|     assert result == expected | ||||
|     print("Success!") | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue