brewersfan1976

lineup.rb

Sep 24th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.57 KB | None | 0 0
  1. def lineUp(commands)
  2.     number = 0
  3.     lr_combinations = 0
  4.     x = 1
  5.    
  6.     if commands.length == 0 then
  7.        return 0
  8.     end
  9.        
  10.     if commands.length == 1 and commands == 'A' then
  11.        return 1
  12.     end
  13.        
  14.     if commands.length == 1 and (commands == 'L' or commands == 'R') then
  15.        return 0
  16.     end
  17.        
  18.     if commands[0] == 'A' then
  19.        number = number + 1
  20.     end
  21.        
  22.     if commands[0] == 'L' or commands[0] == 'R' then
  23.        lr_combinations = lr_combinations + 1
  24.     end
  25.        
  26.     while (x < commands.length)
  27.           if (commands[x] == 'L' or commands[x] == 'R') and (commands[x - 1] == 'L' or commands[x - 1] == 'R') then
  28.              lr_combinations = lr_combinations + 1
  29.              
  30.              if lr_combinations % 2 == 0 then
  31.                 number = number + 1
  32.              end  
  33.           end
  34.              
  35.           if (commands[x] == 'L' or commands[x] == 'R') and (commands[x - 1] == 'A') then
  36.               lr_combinations = lr_combinations + 1
  37.              
  38.               if lr_combinations % 2 == 0 then
  39.                  number = number + 1
  40.               end  
  41.           end
  42.                  
  43.           if commands[x] == 'A' and lr_combinations == 0 then
  44.              number = number + 1
  45.           end
  46.                  
  47.           if lr_combinations != 0 then
  48.              if commands[x] == 'A' and lr_combinations % 2 == 0 then
  49.                 number = number + 1
  50.              end
  51.           end
  52.              
  53.           x = x + 1
  54.     end
  55.              
  56.     return number
  57. end
Advertisement
Add Comment
Please, Sign In to add comment