-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Done with all the task #3
base: master
Are you sure you want to change the base?
Changes from all commits
05eadac
caad7c7
880f71f
31826d6
590ed5d
bcf3508
1ceadb8
b64b118
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package DataMunging | ||
import scala.io.Source | ||
|
||
object DataMunging { | ||
|
||
def main(args: Array[String]) ={ | ||
getMinSpread() | ||
getMinGoalDiff() | ||
} | ||
def getMinSpread() = { | ||
val source = Source.fromFile("weather.dat") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could also factor out
|
||
val spread = source.getLines.toList | ||
.drop(2) //the first line is a header and second line is a blank space so skip those lines here. | ||
.map(_.split("\\s+")) // split on regex white space | ||
.map(x => (x(1), x(2).stripSuffix("*").toDouble-x(3).stripSuffix("*").toDouble)) //clean the recourds as somelines have * and aslo conver to double | ||
val min_spread = find_min(spread) // get minimum spread | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Scala style guide usually goes with camelCase variable names like minSpread, rather than having underscores like min_spread |
||
println(s"1: the day with the smallest temparature spread is day ${min_spread._1} with a spread of ${min_spread._2} degrees") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. println at this point is okay. However your getMinSpread() function can be more testable with a unit test if you make it return the actual spread. In other words, making it 'a pure function without side-effects' .
|
||
source.close() | ||
} | ||
|
||
def getMinGoalDiff() = { | ||
val source = Source.fromFile("football.dat") //get file access object | ||
val teams = source.getLines.toList | ||
.drop(1) //drop the header | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you implement getLines the way I suggest, getMinGoalDiff() becomes |
||
.filter(x=> !x.endsWith("-")) // filter out line with ---- | ||
.map(_.split("\\s+")) // split each line on white space | ||
.map(x =>(x(2), x(7).toDouble - x(9).toDouble)) // select team and different between f and a | ||
val team = find_min(teams) | ||
println(s"2: the team with the smallest difference for and against goal is ${team._1} with a difference of ${team._2} degrees") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice touch with the string interpolation 👍 |
||
source.close() | ||
} | ||
// | ||
// def getSourceData(filename: String): BufferedSource = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can delete commented out code to keep your code clean |
||
// return Source.fromFile(filename) | ||
// } | ||
|
||
def find_min(data: List[(String, Double)]): (String, Double)= { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice job factoring out this common code 👍 |
||
return data.reduceLeft((x,y) => if (x._2 < y._2) x else y) | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Overall, very very good effort 👍 👍 . For someone new to Scala, you have done a great job! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can remove
def main(args: Array[String])
by just extending App, that is,object DataMunging extends App