Advertisement
Guest User

Twitter Interview 4 Perry

a guest
Mar 4th, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.51 KB | None | 0 0
  1. // This is the text editor interface.
  2. // Anything you type or change here will be seen by the other person in real time.
  3.  
  4. // 9x9 grid with integers (-inf to inf)
  5. // return if it's a valid sudoku solution
  6.  
  7. bool checkNumbers(vector<int> &num){
  8.     vector<bool> found(9,false);
  9.    
  10.     for(int i=0; i<num.size(); i++){
  11.         if(num[i] < 1 || num[i] > 9)
  12.             return false;
  13.         found[num[i]-1] = true;
  14.     }
  15.    
  16.     for(int i=0; i<found.size(); i++){
  17.         if(found[i]==false)
  18.             return false;
  19.     }
  20.     return true;
  21. }
  22.  
  23. bool isValid(vector<vector<int> > &grid){
  24.     if(grid.size()!=9)
  25.         return false;
  26.     for(int i=0; i<9; i++){
  27.         if(grid[i].size() != 9)
  28.             return false;
  29.     }
  30.    
  31.     // check each row
  32.     for(int i=0; i<grid.size(); i++){
  33.         vector<int>tmp;
  34.         for(int j=0; j<grid[i].size(); j++){
  35.             tmp.push_back(grid[i][j]);
  36.         }
  37.         if(!checkNumbers(tmp))
  38.             return false;
  39.     }
  40.    
  41.     // check each column
  42.     for(int j=0; j<grid[0].size(); j++){
  43.         vector<int> tmp;
  44.         for(int i=0; i<grid.size(); i++){
  45.             tmp.push_back(grid[i][j]);
  46.         }
  47.         if(!checkNumbers(tmp))
  48.             return false;
  49.     }
  50.    
  51.     // check each 3x3 box
  52.     for(int i=0; i<grid.size(); i+=3){
  53.         for(int j=0; j<grid[i].size(); j+=3){
  54.             vector<int>tmp;
  55.             for(int m=0; m<3; m++){
  56.                 for(int n=0; n<3; n++){
  57.                     tmp.push_back(grid[i+m][j+n]);
  58.                     if(!checkNumbers(tmp))
  59.                         return false;
  60.                 }
  61.             }
  62.         }
  63.     }
  64.    
  65.     return true;
  66. }
  67.  
  68. ///
  69. ///          
  70.  
  71.                   |
  72.              |          |
  73.                
  74.                 |    |
  75.                |      |
  76.  
  77.  
  78. struct node{
  79.     int x;
  80.     struct node *left;
  81.     struct node *right;
  82. };
  83.  
  84. bool checkLeftRight(node *left, node *right){
  85.     if(left == NULL && right == NULL)
  86.         return true;
  87.     if(left == NULL || right == NULL)
  88.         return false;
  89.     if(left->x != right->x)
  90.         return false;
  91.     return checkLeftRight(left->left, right->right) && checkLeftRight(left->right, right->left);
  92. }
  93.  
  94. bool isSymetric(node *root){
  95.     if(root == NULL)
  96.         return true;
  97.    return checkLeftRight(root->left, root->right);
  98. }
  99.  
  100.  
  101.  
  102. Scala course on coursera
  103. reactive programming course on coursera (followup to the scala)
  104. scalding on github (cascading + scala)
  105. summingbird on github
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement