We Recommend

Beginning Ruby: From Novice to Professional Beginning Ruby: From Novice to Professional
Beginning Ruby is a thoroughly contemporary guide for every type of reader wanting to learn Ruby, from novice programmers to web developers to Ruby newcomers. It starts by explaining the principles behind object-oriented programming and within a few chapters builds toward creating a genuine Ruby application.


Posted By

ebukva on 11/21/07


Tagged


Versions (?)


Counting Hours


Published in: Ruby 


  1. #!/usr/bin/env ruby
  2. text = STDIN.read
  3. lines = text.split("\n")
  4. totalHr = 0
  5. totalMin = 0
  6.  
  7. i = 1
  8. for line in lines do
  9. begHr = line[0,2].to_i
  10. begMin = line[3,2].to_i
  11. endHr = line[8,2].to_i
  12. endMin = line[11,2].to_i
  13. hourage = line[0,13]
  14. desc = line[14, (line.length - 14)]
  15.  
  16. if line != ""
  17.  
  18. if desc == nil
  19. desc = " "
  20. end
  21.  
  22. if endHr < begHr
  23. endHr = endHr + 24
  24. end
  25.  
  26. hoursSpent = endHr - begHr
  27.  
  28. if endMin < begMin
  29. endMin = endMin + 60
  30. hoursSpent = hoursSpent - 1
  31. end
  32.  
  33. minSpent = endMin - begMin
  34.  
  35. puts hourage + " (" + hoursSpent.to_s.rjust(2,'0') + "." + minSpent.to_s.rjust(2,'0') + ") " + desc
  36.  
  37. totalHr = totalHr + hoursSpent
  38. totalMin = totalMin + minSpent
  39.  
  40. i += 1
  41. end
  42.  
  43. end
  44.  
  45. while totalMin >= 60
  46. totalMin = totalMin - 60
  47. totalHr = totalHr + 1
  48. end
  49.  
  50. puts "\n" + " TOTAL: " + totalHr.to_s.rjust(2,'0') + "." + totalMin.to_s.rjust(2,'0')

Report this snippet 

You need to login to post a comment.