Skip to content
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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/main/scala/DataMunging.scala
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(
Copy link
Contributor

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

@idarlington idarlington Mar 6, 2017

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.

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]) = {
Copy link
Contributor

Choose a reason for hiding this comment

The 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 **/
Copy link
Contributor

Choose a reason for hiding this comment

The 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))

}
}