fork download
  1. hash = {}
  2. alpha = 'A'
  3. for number in 1.upto(26)
  4. hash[alpha] = number
  5. alpha = alpha.succ
  6. end
  7.  
  8. MapOfAlphaToNumber = hash
  9.  
  10.  
  11. def parse_excel_row_id(string)
  12. row_num = 0
  13. for alpha in string.split(//)
  14. raise 'Invalid Row ID' unless MapOfAlphaToNumber.has_key?(alpha)
  15.  
  16. row_num = row_num * 26 + MapOfAlphaToNumber[alpha]
  17. end
  18.  
  19. return row_num
  20. end
  21.  
  22.  
  23. printf "'A' --> %d", parse_excel_row_id('A')
  24. puts
  25. printf "'AA' --> %d", parse_excel_row_id('AA')
  26. puts
  27. printf "'ZZZ' --> %d", parse_excel_row_id('ZZZ')
  28. puts
  29.  
Success #stdin #stdout 0.05s 9760KB
stdin
Standard input is empty
stdout
'A'   --> 1
'AA'  --> 27
'ZZZ' --> 18278