Monday, March 28, 2011

Kata 2: Sorting a list of integers

C (http://ideone.com/Zf6rB)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <stdio.h>
#include <stdlib.h>

/*
Order an Array of integers, coding kata.
In C we will have to program our own sorting
algorithm. For simplicity our own very inefficient
sorting algorithm is used
*/

void printArray(int* list, int size);
void sortAscending(int* list, int size);


int main()
{
int size = 50;
int list[size];
// Initialize the array in reverse order
int i = 0;
for(i = size ; i > 0 ; i--)
{
list[size-i] = i;
}
printArray(list,size);
sortAscending(list,size);
printArray(list,size);

}

void printArray(int* list, int size)
{
int i = 0;
for(i = 0 ; i < size ; i++)
{
printf("list[%d] = %d\n",i,list[i]);
}
}

void sortAscending(int* list, int size){
// Variable to swap values
int aux;
// Variable to knwo when we are done ordering
int comparisonCounter = 0;
int index = 0;
while(index < size -1)
{
comparisonCounter++;
if( list[index] > list[index+1] )
{
aux = list[index];
list[index] = list[index+1];
list[index+1] = aux;
index = (--index >= 0)?index:0;
}
else
{
index++;
}
}
printf("Comparisons done = %d\n",comparisonCounter);
}

Sunday, March 27, 2011

Greater Common Divisor (GCD), the Euclidean way

GDC using the Euclidean algorithm and different programming languages (as a means of a very simple coding kata):


1
2
3
4
5
6
7
8
9
10
11
12
13
14
def gcd(num,den):
rem = abs(num%den)
print "num: %d, den: %d, rem: %d"%(num,den,rem)
if (rem == 0):
return den
else:
return gcd(den,rem)


# Result should be 11 for all of the following
print gcd(110,33)
print gcd(-110,33)
print gcd(33,-110)
print gcd(33,110)

C

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdio.h>
#include <stdlib.h>

// Function prototype
int gcd(int a,int b);

int main()
{
printf("GCD(%d,%d) = %d\n",110,33,gcd(110,33));
printf("GCD(%d,%d) = %d\n",110,-33,gcd(110,-33));
printf("GCD(%d,%d) = %d\n",33,110,gcd(33,110));
printf("GCD(%d,%d) = %d\n",33,-110,gcd(33,-110));
exit(0);
}

int gcd(int a, int b)
{
int rem = abs(a%b);
if(rem == 0)
{
return b;
}
else
{
return gcd(b,rem);
}
}

C++


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <cstdlib>
using namespace std;

class GDC
{
public:
int calculateGDC(int a, int b);
};

int main()
{
GDC * calculator = new GDC();
int a = 110;
int b = 33;
cout << "GDC(" << a << "," << b << ") = " << calculator->calculateGDC(a,b) << endl;
cout << "GDC(" << a << "," << -b << ") = " << calculator->calculateGDC(a,-b) << endl;
cout << "GDC(" << -a << "," << b << ") = " << calculator->calculateGDC(-a,b) << endl;
cout << "GDC(" << -a << "," << b << ") = " << calculator->calculateGDC(-a,b) << endl;
delete calculator;
exit(0);
}



int GDC::calculateGDC(int a, int b)
{
int rem = abs(a%b);
if (rem == 0)
{
return b;
}
else
{
return this->calculateGDC(b,rem);
}
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.math.*;
public class Main
{
public static void main(String args[])
{
int a = 110;
int b = 33;
Main calc = new Main();
System.out.println("GDC("+a+","+b+") = "+ calc.calculateGDC(a,b));
System.out.println("GDC("+a+","+-b+") = "+ calc.calculateGDC(a,-b));
System.out.println("GDC("+-a+","+b+") = "+ calc.calculateGDC(-a,b));
System.out.println("GDC("+-a+","+-b+") = "+ calc.calculateGDC(-a,-b));
}

int calculateGDC(int a, int b){
int rem = Math.abs(a%b);
if (rem == 0)
{
return b;
}
else
{
return this.calculateGDC(b,rem);
}
}
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
def mcd(a,b)
rem = (a%b).abs
if rem == 0 then
return b
else
return mcd(b,rem)
end
end

puts mcd(110,44)
puts mcd(110,33)
puts mcd(110,-33)
puts mcd(33,-110)



Saturday, March 26, 2011

Java Regular Expressions

Today I want to provide some useful information about the use of regular expressions in Java.

Regular expressions handling is part of the Java library, adding to the vast functionality already offered by it. In short, we could say that regular expressions represent string patterns

They have may uses. For instance, they can be used to filter the files in a directory whose name follows a particular pattern.

The good news about Java and regular expressions, is that the API is pretty simple (something that can not be said about other APIs in the standard library). It is based in the use of two classes, the Pattern and the Matcher. Obviously the Pattern class represents the regular expression pattern, while the Matcher applies the regular expression to a particular String.

There are three ways to use a Matcher:
  1. To find substrings matching the pattern
  2. To check in the whole input string matches the pattern
  3. To check if a defined region within the inputs string matches the pattern

More information can be found in the Java Regular Expressions Trail.

The following code snippet shows how to use a Pattern and a Matcher to filter Strings in an array. It uses the Matcher to check if the whole input matches the pattern. The source code can be edited and downloaded from IDEONE.COM:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.util.*;
import java.lang.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

class Main
{
public static void main (String[] args) throws java.lang.Exception
{
System.out.println("Initializing");
String input[] = {"pattern0_1","pattern1_2","pattern_d"};
Pattern p = Pattern.compile("(pattern)(\\d+)(_)(\\d)+");
for(int i = 0 ; i < input.length ; i++){
Matcher m = p.matcher(input[i]);
if(m.matches()){
System.out.println(m.group());
}
}
}
}

Enjoy!!!