## Friday, September 28, 2012

### Formatting blog code snippets

I use http://codeformatter.blogspot.com/ to format the code snippets. It's brilliant, do give it a try if you are posting code to your blog.

### How to determine the next occurrence of a day of the week in Java

I recently ran into a question around how to determine the next occurrence of a particular day of the week in Java. For example, if today is Tuesday, how do I figure out when the next Saturday is? Or, if it's Saturday, when is the next Wednesday?

Sounds simple enough, right? Turns out, it's not that easy in Java! Here's my solution:

Turns out that the % (mod) in Java doesn't deal with negative numbers that well, but by using the trick of doing a double-mod, you get the right answer!

You simply call this method with the starting date, and then tell it to target, for example, Calendar. SATURDAY, and it will give you the date of the next Saturday!

Sounds simple enough, right? Turns out, it's not that easy in Java! Here's my solution:

```
public static Date getNextOccurenceOfDay(Date today, int dayOfWeek) {
Calendar cal = Calendar.getInstance();
cal.setTime(today);
int dow = cal.get(Calendar.DAY_OF_WEEK);
int numDays = 7 - ((dow - dayOfWeek) % 7 + 7) % 7;
cal.add(Calendar.DAY_OF_YEAR, numDays);
return cal.getTime();
}
```

Turns out that the % (mod) in Java doesn't deal with negative numbers that well, but by using the trick of doing a double-mod, you get the right answer!

You simply call this method with the starting date, and then tell it to target, for example, Calendar. SATURDAY, and it will give you the date of the next Saturday!

## Friday, September 21, 2012

### Finding duplicate numbers with Binary Math in Java

I went for an interview once, and was asked the following question:

Given a sequence of numbers, from 1 to 1000, where only one number is duplicated, how would I proceed to find the duplicate number?

After I solved the problem using a basic loop that just checks if it's seen this number before using a hash table, the interviewer asked if I could improve my answer using XOR math. I didn't quite get it right, this is the solution they showed me :

int numbers[] = {4,2,3,4,5,6,7,8,1,10,11,12,13,14,9};

for (int pos = 1; pos < numbers.length; pos++)

{

numbers[pos] = numbers[pos] ^ numbers[pos-1] ^ pos;

}

System.out.println("Duplicate is : " + numbers[numbers.length-1]);

This bit of Java code loops through the array and finds the duplicate number. Of course, this only works for positive integers, I've not tested it with negative numbers and I know it doesn't work with floats.

So, there you go, a good use for binary math in Java!

Given a sequence of numbers, from 1 to 1000, where only one number is duplicated, how would I proceed to find the duplicate number?

After I solved the problem using a basic loop that just checks if it's seen this number before using a hash table, the interviewer asked if I could improve my answer using XOR math. I didn't quite get it right, this is the solution they showed me :

int numbers[] = {4,2,3,4,5,6,7,8,1,10,11,12,13,14,9};

for (int pos = 1; pos < numbers.length; pos++)

{

numbers[pos] = numbers[pos] ^ numbers[pos-1] ^ pos;

}

System.out.println("Duplicate is : " + numbers[numbers.length-1]);

This bit of Java code loops through the array and finds the duplicate number. Of course, this only works for positive integers, I've not tested it with negative numbers and I know it doesn't work with floats.

So, there you go, a good use for binary math in Java!

## Tuesday, September 18, 2012

### Learning Python with Umonya

It's time for some more Python fun! Umonya is busy gearing up for another basic Python course aimed at high school pupils.

I quote from their website :

"Umonya will be having a course on 12-14 October 2012 where we will teach 100 High School children how to program in Python. It will be taking place during Cape Town's first ever Software week."

If you are interested, please visit their website at http://www.umonya.org/ to learn more.

I quote from their website :

"Umonya will be having a course on 12-14 October 2012 where we will teach 100 High School children how to program in Python. It will be taking place during Cape Town's first ever Software week."

If you are interested, please visit their website at http://www.umonya.org/ to learn more.

Subscribe to:
Posts (Atom)