-
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
My Solution to the weather problem #2
base: master
Are you sure you want to change the base?
Changes from all commits
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 @@ | ||
/** | ||
* Created by idarlington on 2/26/17. | ||
*/ | ||
object DataMunging { | ||
|
||
case class weatherLine( | ||
dayNumber : Int, | ||
maxTemperature : Double, | ||
minTemperature : Double | ||
) | ||
|
||
def temperatureDiff (a : weatherLine, b : weatherLine) = { | ||
(a.maxTemperature - a.minTemperature) > (b.maxTemperature - b.minTemperature) | ||
} | ||
|
||
def main(args: Array[String]) = { | ||
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. I suppose you could extend App instead of having a main method, but thats just a style thing. Whichever style you like is good. |
||
/** Read Contents of file **/ | ||
val filename = "weather.dat" | ||
val fileLines = io.Source.fromFile(filename).getLines().toList | ||
|
||
/** filter empty lines **/ | ||
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. Your solution is nice, simple, elegant. 👍 The comments would be really helpful for guys in our Meetup who are beginners. Though more experienced Scala devs may not need them, as the code itself is quite clear. 👍 |
||
val filteredFileLines = fileLines.filter(l => l != "") | ||
|
||
/** remove header and footer from data and split lines into Array of Strings **/ | ||
val lines = filteredFileLines.tail.init.map(l => l.split(" ")) | ||
|
||
/** filter out empty indices from array and remove asterisks from data **/ | ||
val filteredLines = lines.map(l => l.filter(_ != "").map(s => s.replaceAll("[*]", ""))) | ||
|
||
/** store required data in weatherline instances **/ | ||
val weatherLines = filteredLines.map(l => weatherLine(l(0).toInt,l(1).toDouble,l(2).toDouble)) | ||
|
||
/** sort with temperature difference **/ | ||
val sortedData = weatherLines.sortWith(temperatureDiff) | ||
val maxTempSpread = sortedData.head | ||
|
||
println("Day " + maxTempSpread.dayNumber + " has Max Temperature spread of " + (maxTempSpread.maxTemperature - maxTempSpread.minTemperature)) | ||
|
||
} | ||
} |
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.
Nice touch adding a weatherLine class data type! 👍 .
One tiny thing - Scala Style guide would probably suggest the CamelCase name for weatherLine i.e WeatherLine.
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.
http://docs.scala-lang.org/style/naming-conventions.html#classestraits
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.
Thanks, I decided earlier to I need to put in more effort to using a style guide in coding. Thanks for the correction.