Thursday, October 11, 2012

Android presentation

Download the android presentation (08/10/2012- Man-UP) from here.

Sunday, January 8, 2012

jlab : Your own Java Testing Lab by VasLabs for you!

What is the way of programming big programs? You create object classes and test them individually. You configure them to work as you want and then you put them together to form a nice new shiny program.
Most of the programmers use IDE's to do that. However, if you are a hardcore of the kind, you may wish to create your own environment just as how you like it to be.
VasLabs has created a small script to give you ideas and/or help you start building your own lab environment to do what everyone does in labs; tests!
The philosophy behind it is that for every object class you get a test class. And for every test you do, you can view a log file containing the results. Then you can compare it with the expected results by reading it or using cmp, or even better, you could initially create a file that contains the expected results and then use jlab to run the test by comparing for you both results.
The program works on your current directory. I.e. you should change directory to the directory you want to build your lab and then run jlab.
Here is the manual of the program (you can also view it by running jlab without arguments):

To use this program use the format jlab --argument [file]
-----------------------------------
The list of available arguments are:
--initialise
Initialises your lab environment in your current directory creating the appropriate folders

--create [file1 ... fileN]
Creates the given class names and puts them in the src directory. It also opens them using gedit

--create_tests
Creates a test class for each class in the src directory. It then opens them with gedit

--compile
Compiles every src file in all directories and puts the .class files in the bin directory

--run_tests
Runs all tests and puts the results in log files. Then it opens the log files with gedit

--test file expectedResults
Runs the given test file and compares its results with the given expectedResults file. If there is a difference it opens the results with gedit

Requires: gedit text editor

Author: Vasilis Nicolaou
Distributed by: vaslabs
User Agreement:

This script shall be used with care by people who know what they are doing. This program intends to help building a java lab. By java lab
we mean a virtual environment that behaves like IDE with the difference that offers simple functionalities for testing java object classes.
The author has no responsibility for any loss of data or damage caused by using this script.
You can edit and re-distribute this script as you wish. If you do not agree delete this script file from you computer.

Tip: You can create aliases for every different functionality and put it in .my_bashrc
08/01/2012



Using this idea you can build a similar lab for every language or facility you want.
Download it from here.
This is written in bash, and therefore can be run only on Unix based OS which have bash installed.
By VasLabs


Tuesday, December 13, 2011

The great convert tool of ImageMagick


It's of those days where you are just wondering why you are failing to program as usual. Your mind is sleepy, your thinking slow. While drinking a cup of coffee in order to awake your brain (even if you know that in such situations coffee just fails) you listen something not so important (as most things sound at the beginning). "I have a large pdf file how do I reduce its size?".
(I want to inform you now that this article is not about pdf resizing).
Then, as the coffee sinks in, along with this words, there is another question on the horizon. Can you reduce anything without losing something? Of course not. Anyway, that's not my point.

I decided to try and find how to do that, with linux, using scripts. After a couple of google searches I found ghostscript. It was quite a fail, since the pdf was at its minimum size that ghostscript could convert, so it made it bigger.

Then, I looked at my pictures. I always do this when I fail to find a solution for something. Great images, with high resolution. Hang on! What about making every pdf page as a jpg file and then reducing the resolution of those images, and after that putting all these images back as pdf pages?

If you have the knowledge of a normal user this could mean hours of work, especially if pdf has 100 pages. Imagine having at the end another bigger pdf file.

So, I realised that it was a lot of work. What about using a script to do that? Here it comes the convert of ImageMagick!

I might failed to find an optimised solution for the pdf problem, but I've found a great tool! Convert is a terminal program created by ImageMagick that can convert any image file into another by user's will. In addition, it is open source, written in C.

You can reduce your size of your images, transform them, skew them, flip them, change colours with mapping and more more advance image things! It's like having photoshop in a small program, without the heavy brashes and tools. Type the commands, you have your image converted. Just visit man pages on your linux. Or visit  http://www.imagemagick.org/script/convert.php to download a version with GUI or for a different operating system.

For the story, I used this to convert my pdf:
convert input.pdf -scale 500 output.pdf
And got a smaller file with a terrible resolution.
Anyone knows anything about it?

That's a story about how something irrelevant results to an exploration of another irrelevant thing (but useful).


So, even if you are going to fail, never mind, explore! Even if it's pangolins! :)

Monday, November 14, 2011

Computer Synesthesia

Synesthesia (also spelled synæsthesia or synaesthesia, plural synesthesiae or synaesthesiae), from the ancient Greek σύν (syn), "together," and αἴσθησις (aisthēsis), "sensation," is a neurologically based condition in which stimulation of one sensory or cognitive pathway leads to automatic, involuntary experiences in a second sensory or cognitive pathway. People who report such experiences are known as synesthetes. (http://en.wikipedia.org/wiki/Synesthesia)

Here, we are going to illustrate Synesthesia as a situation where a person (in this case the computer) hears images. I.e. instead of seeing them, the pathway goes from the eyes to the part of the brain that understands sounds (or something like that :))

How is it like to hear images? Let's hear one.

You have to have this tools:
A linux distribution
The pacat program (check if you have it, otherwise install it)
A little C (so we need gcc compiler)

Here is the code for reading a file. We are going to use it to read image files:

#include
int main(int argc, char *argv[])
{
int t = 0;
FILE *input;
input = fopen(argv[1], "r");
while (t != -1)
{t = getc(input); putchar(t);}
fclose(input);
}

Save it as sounds.c and compile it using gcc sounds.c -o sounds
Then use
sounds mypicture.png | pacat --format u8 --rate 8000

And you can hear your pictures and images :P Don't expect the sweetest melody :P


Saturday, June 11, 2011

C : thank you for the memories

C is in general a quite free language, in a sense that it usually compiles! However, when it comes to running, even the most experienced programmers get errors or values that they wouldn't expect.
The most common error is a segmentation fault. This happens when your program tries to write or read a memory location out of its bounds, so the operating system does not allow it.
In addition, sometimes, you get some unexpected values, because you accidentally read a wrong memory location that belongs to your program, so the operating system does not block it. Furthermore,somebody can write accidentally a piece of code that might cause the program to freeze:
Imagine you want to create an array of length 10 and initialise it with 0 values.
What would happen if you write this (by mistake of course)

int main()
{
   
    int a[10];
    int i = 0;
    for (;i<=10; i++)
            a[i] = 0;
   
}



Why your program freezes? What would happen if you read the array without putting initial values in it if you print it out?
Can you guess what would happen if you read a[10]?
Try it! It's really fun!
by VasLabs

Wednesday, June 8, 2011

Text File Encryptor

Here is the idea. Create a simple program that changes the bytes of a file using a passphrase in a such way that they can be revered to their initial state.
Of course this program is not suitable for commercial use because the encryption is very simple. But you get the idea.
The program is written in java and you can download the source code and the compiled program from there:

ByteReader source code
ByteReader byte code
ByteEncryptor source code
ByteEncryptor byte code
DataEncryptor source code
DataEncryptor byte code


The process is:
We have an object ByteReader that reads the contents of a text file using a BufferedReader which wraps a FileReader. It reads it line by line to avoid reading dumb null bytes (it happened when we used InputStreamReader, no idea why!
After that we convert each character of the line to an int! And then we store it in a variable such that b = object ByteReader and b.bytes[][] = thearray of ByteReader that keeps the bytes read.

Then we create another object, a ByteEncryptor which takes as one of its arguments the byte reader and according to another argument password and whether to subtract from the value of the bytes or add(i.e. 0 or 1) as another argument. It also takes the number of lines and the destination to store the encrypted data.

It encrypts each byte with the simplest calculation that exists!
for each byte
byte = byte + (passphrase.hashCode() % 91)
and decrypts
byte = byte - (passphrase.hashCode() % 91)

Some notes to have in mind!
1) Never use this program on important data! They might be corrupt! Use it only on test data that you don't mind to lose!
2) It fails to decrypt images, videos, pdf and similar formats. Do you suspect why?
3) Never use this program to encrypt data and feel safe! Can you think of a simple program that it can decrypt these data without knowing the password? If yes, let others know! (We know :) )

To run the program open a terminal the directory you saved the files and then run
java DataEncryptor yourFileYouWantToEncrypt destinationFile password 1/0

1 is for adding, ie encryption, 0 is for subtracting, ie decryption.
Do not run it the first time with the value 0, you may not be able to recover it, and also remember the notes above.
If you put less arguments you will get an ArrayIndexOutOfBoundsException, i.e. the program will crash.
You can make whatever changes you like on this program since it's just a simple silly one that aims to excite your imagination for building similar things, more sophisticated. So, it is not licensed and there is no copyright.

Enjoy!!!

Tuesday, June 7, 2011

The MontyHallProblem

The Monty Hall problem is as follows(stated in wikipedia):
The Monty Hall problem is a probability puzzle based on the American television game show Let's Make a Deal that was originally hosted by Monty Hall. The problem, also called the Monty Hall paradox, is a veridical paradox because the result appears odd but is demonstrably true. The Monty Hall problem, in its usual interpretation, is mathematically equivalent to the earlier Three Prisoners problem, and both bear some similarity to the much older Bertrand's box paradox.

In a few words
Suppose you're on a game show, and you're given the choice of three doors: Behind one door is a car; behind the others, goats. You pick a door, say No. 1, and the host, who knows what's behind the doors, opens another door, say No. 3, which has a goat. He then says to you, "Do you want to pick door No. 2?" Is it to your advantage to switch your choice?
 Furthermore the Monty Hall problem is briefly discussed in the movie "21 Blackjack".
The answer in the question switch or not is given but without a mathematical analysis.
The mathematical solution for this problem is based on Bayes' Theorem. This article, however, does not aim to explain Bayes' Theorem, but how we use it to create a program in java that will give us the best solution for N doors.
The steps is that you choose a door, and Monty Hall opens a door other that yours and the winning door. In each stage you choose to switch or stick to your choice. This process is until there's nothing left but two closed doors, yours and another one.

So let's say we have N doors. The prior probability of each door to have the car behind it is obviously 1/N

So, let's say D is the set of containing all closed doors, such that its initial value is
P(D) = 1
(That means that the car is behind of a closed door)
and card(D) = N
(that means that D has N elements)


∀L∊D
         P(L) = 1/N
(this means that each door that is a closed door has probability be the right door of 1/N)

So now Monty Hall opens a door b other than your door you chose.
D = D-{b}
P(D) = 1

Now for every door that is closed the probability changes.
Let Oi be the event that Monty Hall opens a door i, and P(Oi | L) be the probability that
he opens door I by knowing that the right door is L.
so if L = "your door" ⇒ i is a door other than L


so P(Oi | L) = 1/(N-1)
Wonder why? think about it. He knows that you choosed the right door, so he has a range of N-1 doors to choose.
He wouldn't open the right door (that's the rules) and so if L = i ⇒ P(Oi | L) = 0
Now what happens if the door is not the door you chose?
if (L ≠ your door) and (L ≠ i) then
  P(L) = 1/(N-2)

If you still wonder why count how many doors left for Monty Hall to choose.

Now, we have to update our probabilities of belief for each door. We must forget the prior probabilities we begun because they have no meaning any more.

So we've found those probabilities of opening, how do we find the new probabilities for each door?

Using Bayes' theorem to find the probability that L is the right door under the event that Moonty Hall opened door i
I.e
P(L | Oi) = P(Oi | L)p(L) / (∑P(Oi | d)P(d))
                                          d∊D

So for each step you must do the same thing, and in the end you will have your probability.




You can ask for questions on facebook or below this article

If you find the solution in a language other than Java we would be glad to share it with us along with your name!

Solution is uploaded, you can find the source code here and the byte code there!