brewersfan1976

catalog_update.rb

May 31st, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.97 KB | None | 0 0
  1. def catalogUpdate(catalog, updates)
  2.     updates_length = updates.length
  3.     catalog_length = catalog.length
  4.     x = 0
  5.     y = 0
  6.     found = false
  7.    
  8.     while (x < updates_length)
  9.           while (y < catalog_length)
  10.                 if updates[x][0] == catalog[y][0] then
  11.                    catalog[y].push(updates[x][1])
  12.                    found = true
  13.                    value = catalog[y][0]
  14.                    catalog[y] = catalog[y] - [value]
  15.                    catalog[y] = catalog[y].sort
  16.                    catalog[y] = catalog[y].unshift(value)
  17.                 end
  18.                    
  19.                 y = y + 1
  20.            end
  21.            
  22.            if found == false then
  23.               catalog.push(updates[x])
  24.               catalog_length = catalog.length
  25.            end
  26.                
  27.            y = 0
  28.            x = x + 1
  29.            found = false
  30.     end
  31.    
  32.     catalog = catalog.sort {|a, b| a[0] <=> b[0]}
  33.  
  34.     return catalog
  35.          
  36. end
Advertisement
Add Comment
Please, Sign In to add comment