brewersfan1976

pair_of_shoes.rb

Oct 15th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.07 KB | None | 0 0
  1. def pairOfShoes(shoes)
  2.     shoes = shoes.sort
  3.  
  4.     # check for equal number of left and right shoes
  5.    
  6.     x = 0
  7.     left = 0
  8.     right = 0
  9.    
  10.     while (x < shoes.length)
  11.           if shoes[x][0] == 0 then
  12.              left = left + 1
  13.           end
  14.              
  15.           if shoes[x][0] == 1 then
  16.              right = right + 1
  17.           end
  18.              
  19.           x = x + 1
  20.     end
  21.              
  22.     if left != right then
  23.        return false
  24.     end
  25.        
  26.     # check to see if shoe size has a matching size for left and right
  27.        
  28.     x = 0
  29.        
  30.     while (shoes[x][0] == 0)
  31.           value = shoes[x][1]
  32.           y = 0
  33.           found = false
  34.        
  35.           while (y < shoes.length)
  36.                 if shoes[y][0] == 1 and shoes[y][1] == value then
  37.                    found = true
  38.                 end
  39.                    
  40.                 y = y + 1
  41.           end
  42.              
  43.           if found == false then
  44.              return false
  45.           end
  46.          
  47.           x = x + 1
  48.     end
  49.        
  50.     return true
  51. end
Advertisement
Add Comment
Please, Sign In to add comment