Skip to content

Instantly share code, notes, and snippets.

View gideondsouza's full-sized avatar

Gideon Israel Dsouza gideondsouza

  • Dransfeld, Germany
View GitHub Profile
IUserRepository repo;
//this magically gets the right instance based on some config somewhere.
class UserDisplay : UserControl
{
UserDisplay(IUserRepository repo)
{//display the username or something here..
}
}
class FakeUserRepo : IUserRepository
{
public string GetUserName(int id)
{
return "FakeUser";
}
}
var fakeUserRepo = new Mock<IUserRepository>();
fakeUserRepo.Setup(f => f.GetUserName(It.IsAny<int>)).Returns("FakeUser");
//does the same thing as the class declaration
fakeUserRepo.Object;//this returns fake object of type IUserRepository
@gideondsouza
gideondsouza / IsPrime.cs
Created March 5, 2012 15:34
normal (brute force) method to check for primes
bool IsPrime(long n)
{
int c = 0;
for (long i = 1; i < n; i++)
{//loop up-till the number
if (n % i == 0)//check if its divisible by i
{
c++;
if (c > 1) { return false; }//short-circuit..
} //^^if it has more than one divisor it can't be prime
@gideondsouza
gideondsouza / SieveofEratosthenes.cs
Created March 5, 2012 15:49
Sieve of Eratosthenes C# implementation
long sum = 0;
long n = 2000000;
bool[] e = new bool[n];//by default they're all false
for (int i = 2; i < n; i++)
{
e[i] = true;//set all numbers to true
}
//weed out the non primes by finding mutiples
for (int j = 2; j < n; j++)
{
@gideondsouza
gideondsouza / gist:2864370
Created June 3, 2012 18:03
Getting users location via network
Button btnLocation = (Button)findViewById(R.id.btnLocation);
btnLocation.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// Acquire a reference to the system Location Manager
LocationManager locationManager =
(LocationManager) AddressPOCActivity.this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
@gideondsouza
gideondsouza / gist:2864432
Created June 3, 2012 18:13
Get an address from a latitude and longitude
public String GetAddress(String lat, String lon)
{
Geocoder geocoder = new Geocoder(this, Locale.ENGLISH);
String ret = "";
try {
List<Address> addresses = geocoder.getFromLocation(Double.parseDouble(lat), Double.parseDouble(lon), 1);
if(addresses != null) {
Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder("Address:\n");
for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
@gideondsouza
gideondsouza / gist:2864460
Created June 3, 2012 18:18
Send a GET request to a server.
public void postData(String la, String lo) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpGet htget = new HttpGet("http://<your_app_url>/Home/Book/"+la+"/"+lo);
try {
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(htget);
String resp = response.getStatusLine().toString();
Toast.makeText(this, resp, 5000).show();
@gideondsouza
gideondsouza / question3.sql
Created August 31, 2012 11:00
MoreJoinOperations question 3 on sqlzoo.net
SELECT id,title, yr
FROM movie
WHERE title LIKE '%Star%Trek%'
ORDER BY yr ASC