Friday, December 3, 2010

This is not Complicated :: I am an Interviewer.

After some months of the post I am an Interviewer, this is the follow up article.

Problem reminder:

We were ask to write an algorithm to to get two random between 1 and 1000000  so that the difference between them is always 355.
You can use what you like but if you write some php then use mt_rand() inbuilt function to generate.

The posts had pre-assumed that, you know basic php, set up you server and  your 'hello world'  at least has been tested:)

No stress :).

Reasoning: If I have one number, then I have the second number by simple arithmethic.

Analysis:

Let,
 $z=355;// it is the answer of our problem and it was given to us.


Lets have the two numbers, call them $x and $y.
Now lets generate,
$x = mt_rand(1, 1000000); //Generate $x
$y = $x + $z;//  we got $y. We are done :) 

Where are we?
First, In 3 lines we got the answer to the interview question!  Hurray!
Secondly,we simply got the second number by adding 355 to the first random number :), arithmetic.
Finally, we use php and we use mt_rand();
Congrats, we may process to phase two of the interview, that is not my job anymore :),but you are assured for your supposed Google Job :)!.


Claims: The Second Number is not random?!
I push that it is random because we don't know the first and , the second depends on the first.It is random because I can't predict it before the Algorithm starts executing .The Algorithm determines what the second number is :).
So, always,
$y-$x = 355;
This simple thinking has helped me in implementing several random effects I had been working on with apps and products I have been building for the past months.I have actually found it's mutations really handy in several locations.

Think starting from the simplest you can think of, build, then go to which ever complicated level you want.
Remember "Everything should be made as simple as possible, not simpler"-Albert Einstein.
Wasamundi,  which will be  a venue makes use of such simple algorithms in it's mutatory forms.
By the way, we are still on private beta testing and soon going to beta test.We have spend some excellent time developing, business modeling and future proofing the core.
I'll see you soon :).
End of Blog post.
For Really Enthusiasts, like Some of my friends :)
After some fierce argument with some friends before this publishing, claiming the second number is not actually random.
I actually spend some few minutes to leave the machine do some work.They where happy with something as this but I was not :).
<?php

/**
 * Description : Function to generate two random numbers so that the difference is always 355.
 *
 * @author Njie Litumbe .L. Nara <njielitumbe@gmail.com><njielitumbe.blogspot.com>
 * @Under Nason Systems, Inc. <www.nasaonsystems.com>
 *
 * All rights Open.
 */

class TwoRand{


 var $a ;
 var $b ;

function tworand(){
$this->a =1;//just the variables, making it clean
$this->b = 1000000;

$x=mt_rand($this->a,$this->b);//Generate $x
$y= mt_rand($this->a,$this->b);//Generate $y (Please don't use arithmetic)

//Logic

//proceed, it can be $y-$x, we just want to make sure it is positive before they do any thing
    if(($x -$y >0))
    {
    
    //lets go recursive

   while(($x - $y)!== 355)
   {  
          $this->tworand();
          exit;
      }
          echo "Helloo, We got it! ";
          $diff= $x-$y;
          echo 'X = '.$x.' Y= '.$y;  
          echo "Z(Difference)". $diff;    
          return 0;
    
    
   }
   else
   {
         $this->tworand();
   }

}

}//end of class tworand

$tworand = new TwoRand();

Our 3 lines of code has ended up as  30 lines including undetermined recursive-ness.

Character of the Above code;

  • Inefficient:
After some undetermined number of refresh before you can actually get the results spitting to the browser, for the other times you get:
Fatal error: Allowed memory size of 134217728 bytes exhausted
Fatal error: Allowed memory size of 134217728 bytes exhausted

  • Not an Algorithm
Undetermined growth rate.
Hardly satisfy any of the conditions for an algorithm

  • Just thought up on impulse to satisfy my Arithmetic hater friends :) they know themselves :).
I may not find time to figure out the actual algorithm to to satisfy my Arithmetic hater friends :).
It is open for they or anyone to suggest something different.

Sunday, November 21, 2010

My dream of One African One Computer; delivering technology to Cameroon.


As the story goes, On Saturday November 13, Possibilities Without Borders held its third annual silent auction and gala event on the College of Charleston’s campus.  The event was in conjunction with Technology for Africa, whose goal was to raise $20,000 to be put toward a humanitarian Spring Break trip to Cameroon.


Four students and one professor  where chosen to make the trip.
The students must raise a minimum of $60,000 by March in order to purchase computers, smart boards, and other technological equipment they will be installing during their stay in Cameroon.


The journey of students of Charleston is not the first to Cameroon.
In March 2009, seven College of Charleston SIFE( Students in Free Enterprise ) students, a faculty advisor and one board member, traveled to Cameroon to set up three computer labs and teach entrepreneurship after successfully raising over $68,000 for the project.
This particular one will be of greater weight.
One source is quoted to have said this journey shall not only be a privilege to travel abroad, but being able to change the lives of countless youths is an even greater one.That   this is a unique experience that will change the lives of everyone involved.


Foumban has been chosen as the area of interest.



I hail this initiative as it comes along to support my dream of One African One Computer.
An Africa, with computers twice the number of phones presently in the continent.
Time will tell,I know, time will tell.

Thursday, November 11, 2010

Nature of Objects In OOP

WARNING!
READING THIS POST MAY LEAVE YOU MORE CONFUSED.THE WRITER DOES NOT CLAIM TO HAVE MASTERED THE SUBJECT NOR ADDRESSES THE WHOLE SUBJECT IN QUESTION.HE TRIES TO SHARE WHAT HE UNDERSTANDS IN THE ‘CONFUSED’ NATURE OF OBJECTS IN OBJECT ORIENTED PROGRAMMING – with case php.We shall stick with Php5>.

I was Introduced to an Object Oriented mindset when I started using Kohana-php as a tool in my development. It turns out that, you may use the tools and the methods, but the thought process is a completely different aspect which few will come to appreciate.
My former boss once asked me; Nara what is an Object?. I could hardly give a clear answer.
This answer becomes a victim of  “I know what the thing is but I can’t explain what it is!”. My adventures have led me to take up the thought activity of OOP.

The Aim of this post:
1. It is intended to familiarize the reader about what OOP is really about, and possibly have him/her taking advantage of it, while keeping it fun.
2. It is intended to be direct. It is intended to make the idea of OOP unobstructed as much as I can go.
3. This article is supposed to be short and introduce the reader to the concept.

Assumption:
The article assumes you have a basic knowledge of php.

Let’s Go!...

What is Object Oriented Programming?
We say it is a type of programming in which programmers define not only the data type of a data structure, but also the types of operations that can be applied to the data structure. In this way, the data structure becomes an object that includes both data and functions. In addition, programmers can create relationships between one object and another. For example, objects can inherit characteristics from other objects.
It is by far not only a matter of syntax; it is a way of thinking, a way of practicing and a way of applying.

Lets Recognize Objects.
It is essential we use real life analogies to bring familiarity to OOP.
Let’s use a car.
What a car has (Properties):
Windows, Doors, An Engine, Wheels.
These objects can further have their own properties and behaviour. This is called Composition in OOP terminology. Just focus on the large object in development, the car. Try to implement only what you need.

What a car can do (Behaviour)
Accelerate,Brake,Open doors

Lets Define an object.
We should say an object is an entity that encapsulates properties and behaviour that is specific to that entity. Programming, an object should be seen as an Implementation of a class,the class is a blue print;because there is a class, we can talk of an object.

Lets try to sort Something :
I see it like: classes are made so that they can become objects at use time. So saying and object extends another object means a class extends a class. If we talk of an object then there is a class around.
This part always gets me lost :).

Lets Relate around us.
  • The magazine you are holding is an object.
  • The articles in it are objects.
  • The window in your room is an object.
  • Your room is an object.
  • Everything you can see is an object.
But there is a lot more to OOP as a concept.

Things to keep in mind.
Functions in a class are called Methods.
Variables in a Class are called Properties.

So far So fine :).

Things to keep in mind (Nature of Objects).
There are three pillars of OOP.

Encapsulation, Inheritance, Polymorphism.
Lets take one at a time we stick with PHP.

Encapsulation:
The object should keep its details to itself, and only expose its interface (or: behaviour) to the outside.

KEYWORDS:
Private, Public, Protected
Private, Protected & Public, called specifiers. They are mainly here for data security. The use of them in programming is a separate, process on its own and has generated several arguments.
Lets demonstrate:
<?php
class Member 
{
       public $name;//the name of a member is declared public
       private $age;//the age is declared public
       protected $activities; //the activities of the member is declared protected


 //we declare a constructor which is loaded when the class is Instantiated
public function  __construct($name,$age,$activities)
{
$this->name = $name;
$this->age = $age;
$this->activities = $activities;
}

//Lets us this accessor to access the activities from when out of this class.
//it has been declared protected
public   function getAct($activities){
//this works as $activity is protected in Member
>return $this->activities ;
}

}
//Use:
$name= 'Sunil';
$age = '28';
$activities = 'Has not been paying their dues.';
//Create a new instance of the member class
$mem = new Member($name,$age,$activities);
//Prints this since this is public
echo " Name : " . $mem->name;
//Prints this since we use an accessor
echo " Activities :" .$mem->getAct($activities);
//Fatal error: Cannot access protected property Member::$activities
//comment it out
echo "Activities : " .$mem->activities;
//this is not possible.We can't access this directly, it’s protected//Fatal error: Cannot access private property Member::$age

echo "Age  : " .$mem->age;//this is private.


Lets Explain:
A public access specifier allows the data members and methods to be access from anywhere in the script.
A method or data member declared as private can only be accessed by the class itself and neither the outside program nor the derived class can have access to it.
A protected access specifier allows the derived class to access the data member or member functions of the base class, but does not allow global access to other objects and functions.
We see this as we use the getAct() function to access the activities which was declared protected.Notice that we could not access $activities property directly.

Inheritance:
This is mainly inheriting the properties of a Superclass or Parent class to another class which is called the child class. As a child inherits the properties from a parent. This property is of great significance, because we don't have to declare the same properties in different classes again and again. We just declare the significant properties to a class and go on inheriting these classes to other classes, which needs to implement or reflect such properties in addition to its own properties.
This approach naturally saves a lot of complexity.

KEYWORD:
Extends (a child extends properties of parent).A class can extend several classes.

Lets Demonstrate:
A car is a parent and a Sports car is a derivative of a Car. That is a sports car Inherits all the properties of a Car.

<?php   
class Car {         
      public $description;     
      public $cost;         

    public   function __construct($d, $c) {
            $this->set($d, $c);
            }

    public        function set($d,$c) {
                $this->description = $d;
                $this->cost = $c;
            }

    public     function display() {
            echo "Description : ".$this->description.'<br/>Cost : '. number_format($this->cost) ."FCFA";
            }



    public    function setDescription($d) {
                $this->description = $d;
            }

    public     function getDescription() {
                return $this->description;
            }
        }

class Sportcar extends Car {
                      public $tyres;

    public      function __construct($d, $c, $t)
                    {
//we use the parent method set.Sport car inherits
                                 $this->set($d, $c);
                                 $this->tyres = $t;
                    }


//This is a function to display the number of tyres
    public    function displays()
                   {
         echo "I am a Sport Car.";
          $this->display();  //this is the inherited method from the parent
     // Here is to display the number of tyres
                           echo "<br/> Number of Tyres : $this->tyres";
                   
                   }
}

//Use:

$description = "Toyota";
$cost = 2000000;
//Instantiate the parent class,create a new object $ordinary
$ordinary = new Car($description, $cost);
$description = "Porsche";
$cost = 5000000;
$tyres = 4;

// Instantiate the sportscar class,create a new object $spcar

            $spcar = new Sportcar($description,$cost,$tyres);

//Lets use all the instantiated material
                        $ordinary->display(); //use the parent
            $spcar->displays(); //use the sports car
/*
 *Output
Description : Toyota
Cost : 2,000,000FCFA
I am a Sport Car.

Description : Porsche
Cost : 5,000,000FCFA
Number of Tyres : 4
*/

?>

Lets Explain:
We go further to create a new method displays() which is peculiar to the sports car only.
A Sport car is a Car it inherits all its variables and functions from Car.We see this as the display() method which we created in the Parent Class (Car), we use it(inherit ), in the displays() method of the sports car.

The variable $ordinary is an Object.
The variable $spcar is an Object

Polymorphism:
This concept simply means one face and multiple interfaces.
This concept of Polymorphism is achieved through functions having the same name, but their execution depending on the context.
PHP has no concept of strong typing so a lot of this is automatic, since it has no concept of strong typing, thus all objects will be treated equally, anyway.

We shall dedicate a different post to this pillar (Lets stick to our objective KIS-Short). 

These basic concepts have encouraged the evolution of a rich programming style.

Things to keep in mind (Nature of objects):
Three advantages of OOP

Reusability:
Objects can stand on their own. They are abstracted; they represent one thing. This means that they can be combined in many ways, which makes for (and encourages) reusability. Reusing objects rather than having to reinvent the wheel over and over again can save a lot of time.

Extendibility:
Instead of writing a completely new object every time you need one (which often takes quite some time), you can often extend one. It is in the nature of objects that they are extendable. One can derive an object from another object, and thus extend its functionality without having to rewrite the whole object and add the required functionality.

Maintainability:
Because of the very natural way, objects (and their hierarchies) can be designed, they are easy to read, which makes it easier to analyse, and thus extend already existing applications. Because of the "pluggable" nature of objects, less code modification is needed to integrate new features into an application.

Conclusion
Object Oriented Programming is a powerful and often elegant approach to analyze, design, and implement a solution to a problem, but not something you master in days, weeks, or even years. It is certainly not the golden key to solve every problem, that will be too bad to be true :). It will not be realistic to apply OOP to that small script which is to take charge of emailing a form.But it should come handy in the design of average to large scale applications.

Othello: A minute to learn, A lifetime to master.

Resources:
http://php.net/manual/en/language.oop5.php

My other Posts Here:
njielitumbe.livejournal.com