Archive for category Uncategorized

Simple Java Combinations Algorithm Code - Calculate Combinations of Elements in an Array

Below is some code I wrote recently to calculate the different combinations of elements in an Array. It is a simplified and straightforward recursive solution.

A recursive solution has 2 important piecies: a base case, and a recursive step. Here is an example of how the code below executes:

given: [1,2,3,4]

Recursive Step - Iterate through each element and break it out, leaving the rest of the elements
(first iteration)
1 [2,3,4]
2 [1,3,4]
3 [1,2,4]
4 [1,2,3]

(second iteration)

1 2 [3,4]
1 3 [2,4]
1 4 [2,3]

…. etc

The recursive step is defined in plain english as just remove a value, then call the function again on the rest of the list (with this element removed).

In the base case, once we have an array the size of length 1, we just simply return the array - so simple!

There is a key piece of code that, given an element and an array of arrays, prepends the element to each array. i.e.

1, [[5,6], [7,8], [3,4]] –> [ [1,5,6], [1,7,8], [1,3,4] ]

Pretty simple.

import java.util.ArrayList;

public class Combination {
public ArrayList<ArrayList<Object>> compute(ArrayList<Object> restOfVals) {
if(restOfVals.size() < 2) {
ArrayList<ArrayList<Object>> c = new ArrayList<ArrayList<Object>>();
c.add(restOfVals);
return c;
}
else {
ArrayList<ArrayList<Object>> newList = new ArrayList<ArrayList<Object>>();
for(Object o: restOfVals) {
//make a copy of the array
ArrayList<Object> rest = new ArrayList<Object>(restOfVals);
//remove the object
rest.remove(o);
newList.addAll(prependToEach(o, compute(rest)));
}
return newList;
}

}
private ArrayList<ArrayList<Object>> prependToEach(Object v, ArrayList<ArrayList<Object>> vals){
for(ArrayList<Object> o : vals) {
o.add(0, v);
}
return vals;
}
public static void main(String args[]) {
ArrayList<Object> i = new ArrayList<Object>();
i.add("1");
i.add("2");
i.add("3");
i.add("4");
Combination c = new Combination();
ArrayList<ArrayList<Object>> ff = new ArrayList<ArrayList<Object>>();
ff = c.compute(i);
System.out.println(ff.size());
System.out.println(ff);
}
}

Tags: , , , , ,

The Internet Trends - Niching, Layering, Hierarchy.

Isn’t it amazing how much knowledge is actually on the internet? You can google just about anything these days - often I have what I think is a semi-original thought, and can usually find someone thinking the same thing in a matter of minutes.

The fact is, we are quickly reaching the point where those quick ‘first layer’ thoughts or questions that all of us have are, or can be answered by, the internet. By first layer, I mean quick thoughts or questions that pop up in our minds on a daily basis, as opposed to questions that come when we are intensely focused on a particular area or in a particular field of expertise.

What is the impact of this? Well, what it means is that to get noticed, you need to start ‘niching’. Yep - this is talked about all the time in online marketing - targeting a specific niche to talk about, filling in the gaps of the knowledge of the internet on particular focused topics.

The second trend, which is a more predictable one due to natural heirarchy, is the layering that is being built on top of the internet. First there is the information. Then there are information aggregators - then there are aggregators of aggregators, and search of search engines. There is infrastructure being built on top of the cloud to manage it all. We built servers and browsers to handle the responses and the requests, and now we have networks and clouds of servers that can easily pass the data back and forth, and scale on and off demand flexibly.

The question is, what is the next layer that is being built? How will the browser / request / server model change? What is the next layer that will be built on top of the internet’s vast amounts of aggregated information?

Quick Reflections on building ZetaPage

The first thing I am obligated - by pure appreciativeness and nothing else - to thank every one of the Shotput Partners. They have created an absolutely amazing program that has helped us immeasureably to get our startup off the ground and into existence.

Alas, the hour is late, and because this warrants much more, here I will outline some brief thoughts to preceed the upcoming lengthy post.

0. There is no substitute for energy.

Everyday I’m faced with hundreds of decisions, from tiny ones like “what is the proper way to word a sentence on the about page”, to how the database models should change to allow better normalization and ensure easier scalability. Every day, you need to be making those decisions and moving forward. Whatever it is, you need to just do it and move forward - sure you may make some typos or screw up the models and have to refactor a lot of code later - but you can’t stop. Of course, I have always been a huge fan of reasoning through things, and doing things in the best way possible; no doubt you can still do this, but the caveat is that you should do things as best as possible (within the given time period).

1. Meeting once a week and speaking with people

In the Shotput program, once a week we would meet with really awesome entrepreneurs, technology lawyers, professors, marketing experts, and a whole host of really cool and smart people. Similarly, working with the other groups to provide feedback and just being surrounded with other entrepreneurs somehow adds a certain vibe and energy that really makes you feel awesome - it is palpable and apparrent, that each of us knew we were buidling something new and novel.

2. Who you gonna call? (Hopefully Ghostbusters isn’t the answer)

Sometimes you have hard decisions to make, such as changing the focus or direction of the company; sure you need to make a decision and go with it, but, like I said earlier, I’m definitely a fan of doing things the best way possible (and not having to redo things) - thus getting expert outside advice is the best thing possible. That is where key advisors come in.

3. Solving hard problems on a daily basis

Like I said earlier, you will be faced with hundreds of questions a day. Solving problems on a daily basis turns into knowing when it is worth it to sit down and think about something, and when it is completely fine to use a straight-out-of-the-box approach.

4. Adaptation on a daily basis

Now, when making decisions you need to be inundated with the latest <insert statement here>; by this, I mean that each day you need to be in two frames of mind; the first frame of mind is the one you ended yesterday on; essentially, this is the plan going ahead. The second frame of mind is the changeling mind, what it does is test the waters of each decision and assumption you make - it doesn’t prevent you from making decisions, but it makes you aware of all the avenues you could be taking in case you get tripped up or decide you need to go in a different direction. Its like you’re navigating through a neighborhood, following the main road but being aware of all the side streets - you do it because the house you are looking for could be on any street.