Advertisement
Guest User

Untitled

a guest
May 27th, 2015
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. scores = "Manchester United 1 Chelsea 0, Arsenal 1 Manchester United 1, Manchester United 3 Fulham 1, Liverpool 2 Manchester United 1, Swansea 2 Manchester United 4"
  2.  
  3. games_won = 0
  4. games_lost = 0
  5. games_drawn = 0
  6.  
  7. goals_scored = 0
  8. goals_conceded = 0
  9.  
  10. scores_array = scores.split(",")
  11.  
  12. #usually in ruby you use the bracket-notation for blocks that are one-line long, and the do/end notation for blocks
  13. #that span multiple lines. For example:
  14. #
  15. #scores_array = scores_array.collect do |string|
  16. # string.split(" ")
  17. #end
  18.  
  19. scores_array = scores_array.collect { |string|
  20. string.split(" ")
  21. }
  22.  
  23.  
  24. scores_array.each { |array|
  25.  
  26. #what you've done here isn't bad, and it works(!)
  27.  
  28. #The problems are as follows:
  29.  
  30. #1) You had to account for the fact that man united isn't always listed first in the input.
  31. #so basically you made an if/else to determine which score belonged to which team. The problem
  32. #with that is that now you have all of this duplicate logic in both the if and the else blocks.
  33. #Ideally, you only want to do all your tallying of scores in ONE place. Ideally, any logic patterns
  34. #that appear in your code should only ever appear ONCE, and then be reused.
  35.  
  36. #2) The real problem here though is the readability of your code. All that array[2] > array[4] crap is
  37. #pretty hard to interpret. Its certainly not intuitive what this code is doing unless you go back and
  38. #read the input. The most important facet of good code is **READABILITY**. It should be self-evident
  39. #what every line is doing. Comments should be almost unnecessary.
  40.  
  41. #Imagine coming back to this in a year and having to fix some part of it (or being another person
  42. #who'se never seen this code before). You would have to re-read the entire thing to begin to get
  43. #your footing with what this code does.
  44.  
  45.  
  46. if array[0] == "Manchester"
  47. goals_scored += array[2].to_i
  48. goals_conceded += array[4].to_i
  49. if array[2] > array[4]
  50. games_won += 1
  51. elsif array[2] == array[4]
  52. games_drawn += 1
  53. else
  54. games_lost += 1
  55. end
  56. else
  57. goals_scored += array[4].to_i
  58. goals_conceded += array[1].to_i
  59. if array[4] > array[1]
  60. games_won += 1
  61. elsif array[4] == array[1]
  62. games_drawn += 1
  63. else
  64. games_lost += 1
  65. end
  66. end
  67. }
  68.  
  69. points = games_won * 3 + games_drawn
  70.  
  71. puts "number of wins = #{games_won}"
  72. puts "number of draws = #{games_drawn}"
  73. puts "number of defeats = #{games_lost}"
  74. puts "goals scored = #{goals_scored}"
  75. puts "goals conceded = #{goals_conceded}"
  76. puts "number of points = #{points}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement