Tekpub’s Mastering Linq Challenge

9. January 2010

Update 6: When you think you’re done, think twice. The code should be valid even when the range contains some negative value

var primes =
    Enumerable.Range(-10, 34).Aggregate<int , IEnumerable<int>>(new int[] {},
                         (list, x) =>
                         x >1 && list.All(y => x%y != 0)
                             ? list.Union(new[] {x})
                             : list);

End update

Update 5: Long story short, Linq is not the type of thing you’d use when finding primes.

Update 4: Removing “if” in expense of reduced speed

Enumerable.Range(1, 34).Aggregate<int , IEnumerable><int>>(new int[] {},
                         (list, x) =>
                         x != 1 && list.All(y => x%y != 0)
                             ? list.Union(new[] {x})
                             : list);

End update

Update 3: Making the code a bit faster by dividing only primes found so far

var primes =
    Enumerable.Range(1, 34).Aggregate(new List<int>(), (list, x) =>
{
    if(x!=1&&list.All(y=>x%y!=0))
    {
        list.Add(x);
    }
    return list;
});

End update

Update 2: Using all, one can have one step cleaner code.

var primes =
        Enumerable.Range(1, 34).Where(x =>
                         x == 2 || (x != 1 &&
                                    Enumerable.Range(2, (int) (Math.Sqrt(x)))
                                                           .All(y=>x%y!=0)));

End update

Update: Using any, one can make a bit faster implementation of it

var primes =
                Enumerable.Range(1, 34).Where(x =>
                                              x == 2 || (x != 1 &&
                                                         !Enumerable.Range(2, (int) (Math.Sqrt(x)))
                                                             .Any(y=>x%y==0)));

End update

Here is my solution to TekPub’s Mastering Linq Challenge appeared on Justin Etheredge’s blog

            var primes =
                Enumerable.Range(1, 34).Where(x =>
                                              x == 2 || (x != 1 &&
                                                         Enumerable.Range(2, (int) (Math.Sqrt(x)))
                                                             .Where(y => (x%y) == 0).Count() == 0));
Not very efficient, but simple :)

Testing on Android

6. January 2010

androidFor the last couple of months, I have been working on Android platform as part of Software Engineering course(more on that later, i hope). Even though there are a lot of things I don’t like about android (like XML layouts and the ids of widgets being held in some other class etc but perhaps this is just me), I overall find it a good platform to work with.

What I really liked in Android is that it provides an Out-of-the-box environment for easy testing android applications. I don’t know how Windows Mobile projects handle this situation, but what android does is that it executes the test in the context of virtual machine. Android integrates nicely with JUnit framework, which is the java brother of NUnit.

What is more interesting than this is that Android also has UI testing framework. Being a person who hasn’t written UI tests much (as in none), I can’t say if it is a good one or not. I just liked the feature that came out of the box.

I have created a simple Activity (you can think of it as a window, but not exactly)

public class SampleActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final Button b=(Button)findViewById(R.id.Button01);
        final EditText t=(EditText)findViewById(R.id.EditText01);
        b.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                t.setText(t.getText().toString()+"hello");
            }
        });
    }
}

And created the following test.

public class SampleActivityInstrumentation extends
		android.test.ActivityInstrumentationTestCase2 {

	public SampleActivityInstrumentation() {
		super("com.myandroid", SampleActivity.class);
		// TODO Auto-generated constructor stub
	}

	public void test_OK_button_appends_hello() throws Throwable {
		SampleActivity tne = (SampleActivity) getActivity();

		final Button btn=(Button) tne.findViewById(com.myandroid.R.id.Button01);
		final EditText titleEdit = (EditText) tne.findViewById(com.myandroid.R.id.EditText01);
		runTestOnUiThread(new Runnable() {
			public void run() {
				titleEdit.performClick();
			}
		});
		sendKeys("H E L L O");
		runTestOnUiThread(new Runnable() {
			public void run() {
				btn.performClick();
			}
		});
		assertEquals("hellohello",titleEdit.getText().toString());
		
	}

}

I found it very cool to have such a nice integration. More than that is that they use proven testing framework out of the box.