Saturday, February 16, 2013

Project Euler - Problem 005

So I just finished problem 5 and it was actually quite easy. I was able to just put a while loop around a simple function call to see if all numbers 1 through 20 evenly divided into a number:

 def isEvenlyDivisible(n: Int) = (1 until 20) forall (n % _ == 0)  

Though, my program did take 12 seconds to run so I still think there are some optimizations that could be done to get it to run a little faster.

Actually, I just made a few enhancements and got it to finish in 7 seconds. I'd like to see if this can be done faster but for now, it's time to take on problem 6.

Saturday, February 9, 2013

Project Euler - Problem 002

I just finished up the second project euler problem and it definitely took me a littler longer than expected to figure out. If you should happen to try this one, make sure to read the directions carefully because I feel I would've had the answer several minutes earlier than I did had I paid a little more attention.

On to problem 3.

Gson and Date Formatting

Gson is a really cool tool to quickly convert regular POJOs to json strings and back. It's super simple to setup and even easier to use:

 final Gson gson = new GsonBuilder().create();  
 final String jsonString = gson.toJson(myPojo);  
 final Pojo myPojo = gson.fromJson(jsonString, Class);  

However, by default it appears that Gson uses a fairly simple date formatter that seems to be locale specific. In most cases, this should be alright unless you need to parse a more fine grained date, such as something like "2013/02/09 15:07:50 +0000". When using gson to parse a date like it does fail, and you get exceptions:

 Caused by: java.text.ParseException: Unparseable date: "2013/02/09 15:07:50 +0000"  

Obviously this is not wanted and knowing that this is a valid date makes it even stranger that gson couldn't handle it. The answer? You can override the date parsing adapter that gson uses by default with your own version. For my example, I had to override it with the following:

final GsonBuilder builder = new GsonBuilder();  
builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {  
    // Year in 4, month in 2, day in 2, hour in 24, minutes in hour, seconds in minute, timezone in 4  
    final DateFormat df = new SimpleDateFormat("yyyy/MM/dd kk:mm:ss Z");  
    public Date deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {  
        try {  
            return df.parse(json.getAsString());  
        } catch (final java.text.ParseException e) {  
            return null;  
return builder.create();  

So, this is a really simple yet powerful way to use gson even when you have difficult dates that need to parsed.

Friday, February 8, 2013

Project Euler - Problem 001

I haven't been working with scala too much lately because I really haven't found any projects that I feel I can accomplish yet with a new language. Though, I heard about project euler the other day and noticed that each problem is a simple math calculation to solve. I thought this would be the perfect opportunity to start learning a new language and take advantage of some of scala's functional programming aspects.

So I took on problem1 during lunch today and found it to be not so terrible:

 package scala  
 import java.util.concurrent.TimeUnit  
  * Problem 001 in scala.  
 object Problem001 extends App {  
  val timeUnit = TimeUnit.MILLISECONDS  
  val startTime = System.currentTimeMillis()  
  // Create a list of 1000 numbers  
  val buffer = for (i <- List.range(0, 1000) if (i % 3 == 0) || (i % 5 == 0)) yield i  
  val sum = buffer.toList.foldLeft(0)(_ + _)  
  val endTime = System.currentTimeMillis()  
  println("Time elapsed in seconds: " + timeUnit.toSeconds(endTime - startTime))  

I definitely see this as a way to keep learning a new language and to keep my mind sharp with these kinds of problems. Hopefully problem 2 goes as well as the first one did.

A New Language

So lately I’ve been wanting to learn a new language and get out of my java home. I’ve been working with java all throughout college and currently use it at work. I heard of the upcoming language scala and found out it is compatible with java which was a total plus for me (seeing that I’m so used to java technologies and libraries). I do have a mathematical background so some of the more advanced collection operations seem a bit more logical to me and really seem help spruce up the code. I haven’t played around with the language too much as of now, but I definitely think this language looks incredible and can’t wait to see what all I can do with it.
I’m really looking forward to this new adventure of teaching myself a new language. Come on scala.