Created
April 18, 2015 02:11
-
-
Save sikanrong/ed9cd0ebe0458a89ece8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | |
| games_won = 0 | |
| games_lost = 0 | |
| games_drawn = 0 | |
| goals_scored = 0 | |
| goals_conceded = 0 | |
| scores_array = scores.split(",") | |
| #usually in ruby you use the bracket-notation for blocks that are one-line long, and the do/end notation for blocks | |
| #that span multiple lines. For example: | |
| # | |
| #scores_array = scores_array.collect do |string| | |
| # string.split(" ") | |
| #end | |
| scores_array = scores_array.collect { |string| | |
| string.split(" ") | |
| } | |
| scores_array.each { |array| | |
| #what you've done here isn't bad, and it works(!) | |
| #The problems are as follows: | |
| #1) You had to account for the fact that man united isn't always listed first in the input. | |
| #so basically you made an if/else to determine which score belonged to which team. The problem | |
| #with that is that now you have all of this duplicate logic in both the if and the else blocks. | |
| #Ideally, you only want to do all your tallying of scores in ONE place. Ideally, any logic patterns | |
| #that appear in your code should only ever appear ONCE, and then be reused. | |
| #2) The real problem here though is the readability of your code. All that array[2] > array[4] crap is | |
| #pretty hard to interpret. Its certainly not intuitive what this code is doing unless you go back and | |
| #read the input. The most important facet of good code is **READABILITY**. It should be self-evident | |
| #what every line is doing. Comments should be almost unnecessary. | |
| #Imagine coming back to this in a year and having to fix some part of it (or being another person | |
| #who'se never seen this code before). You would have to re-read the entire thing to begin to get | |
| #your footing with what this code does. | |
| if array[0] == "Manchester" | |
| goals_scored += array[2].to_i | |
| goals_conceded += array[4].to_i | |
| if array[2] > array[4] | |
| games_won += 1 | |
| elsif array[2] == array[4] | |
| games_drawn += 1 | |
| else | |
| games_lost += 1 | |
| end | |
| else | |
| goals_scored += array[4].to_i | |
| goals_conceded += array[1].to_i | |
| if array[4] > array[1] | |
| games_won += 1 | |
| elsif array[4] == array[1] | |
| games_drawn += 1 | |
| else | |
| games_lost += 1 | |
| end | |
| end | |
| } | |
| points = games_won * 3 + games_drawn | |
| puts "number of wins = #{games_won}" | |
| puts "number of draws = #{games_drawn}" | |
| puts "number of defeats = #{games_lost}" | |
| puts "goals scored = #{goals_scored}" | |
| puts "goals conceded = #{goals_conceded}" | |
| puts "number of points = #{points}" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment