Insertion sort php


<?php
/**
 * An unsorted list of integers
 * Worst case O(n^2)
 * Best case O(n)
 * @param array $unsorted
 */
function insertionSort(array &$unsorted){
    for($i = 1; $i < count($unsorted); $i++){ 
        $temp = $unsorted[$i]; $j = $i - 1; 
        while ( ($j >= 0) &&  ($unsorted[$j] < $temp)){
            $unsorted[$j + 1] = $unsorted[$j];
            $unsorted[$j--] = $temp;
        }
    }
}
Posted in algorithms, php | Leave a comment

Calculate the diagonal difference of an N x N matrix

<?php
/**
* Calculate the diagonal difference of an N x N matrix
* 
* runs in O(n)
*
* @param int   $n size of N x N array
* @param array $a N x N array
*
* @return number
*/
function diagonalDifference($n = 0, $a = array()){
    $first_d = 0;
    $second_d = 0;
    for($i = 0, $j = $n - 1; $i < $n; $i++, $j--){
        $first_d += $a[$i][$i];
        $second_d += $a[$j][$i];
    }
   return abs($first_d - $second_d);
}
Posted in algorithms | Tagged , , | Leave a comment

EBOX (formerly Electronic Box) Internet Service – A review

I am writing this to help out Canadian internet users that might be on, or thinking of signing up Electronic Box/EBOX. Do it – switch.

EBOX is an independent Internet Service Provider (ISP) that piggybacks  on the network infrastructure of BellRogersVideotron, or Cogeco. Currently they offer internet services in Quebec, and Ontario.

I have used the internet services of Bell, Rogers, Videotron, and Cogeco at one point or another. When I had move off the Island of Montreal I called them up to see who could give me the best deal. Rogers and Cogeco were not in my area. I spoke to Bell (current ISP at the time), and Videotron.

Savings

EBOX offered me big savings on their internet packages. Depending on your area – switching to EBOX can save you almost $40. That is how much I saved based on the internet package quotes that I got from Bell Canada, and Videotron.

EBOX Modem

In my experience the EBOX Modem is not that good. You can buy a DLS modem from Kijiji for $15. When I signed up for EBOX internet, I wanted to get a DSL telephone as well for my home alarm. So I ordered EBOX’s high end modem for about $160. I picked in up at Terminus Longueuil. For some reason I was getting disconnected every 25 minutes. Luckily I still had my old Bell Canada modem router. After entering my ebox.net PPPoE username and password into the Bell Canada modem router, I got a stable connection.

DNS

If you decide to keep the EBOX router update the DNS servers to Google DNS servers 8.8.8.8, and 8.8.4.4 this is much better than the EBox DNS servers.

EBOX customer service

My feeling is that EBOX tried to make sure my internet was working well, all my issues were taken care of and they were very helpful. I would point out that the long wait times are not cool. On the sales side. I suggest that they review all sales calls, and call back customers if there was a misunderstanding. During the initial sales call, I was under the impression that I was getting a DSL telephone line – whilst in reality EBOX only offers VOIP telephone.

EBOX static IP (DSL Subnet IP)

The biggest plus for me was getting a static IP for $5. To get a static IP from Bell or Videotron would have forced me to sign up for a Business Internet Package.

I host the blog from my home computer using EBOX internet service.

Overall I would give Ebox 4/5.

Posted in internet | Tagged | Leave a comment

Finding your local African Business in Montreal / Toronto / Calgary – Try Afroll.com

Finding local ethnic establishment is not an easy task. What do I google to find an African business closest to me? Who do I ask? What are the first steps?

Non of these are easy answers. You find that most African or ethnic stores are unwilling, undervalue, or unaware of the power of the web. They rely on advertising on traditional channels, like local community newspapers, holding events, or by word of mouth. While these forms are still relevant – the new age is on the web. Consumers are doing more online shopping by the second – and it is about time that ethnic businesses invest on their online presence.

How much should your business spend on the web? What are the running cost? Do you need to hire extra stuff to maintain and upgrade your online presence? — This is were www.chiramu.com comes in. Free of cost we will give you an online presence.

You can start by adding you business here https://afroll.com/businesses/add, all we need is your business’ address, description, hours, and contact details.

Posted in Businesses, Tutorials | Leave a comment

The Book Of Not by Tsitsi Dangarembwa – a review

I bought The Book Of Not in 2010. I waited 3 years to read it. The first time I opened the book, I felt overwhelmed with anticipation. I was not sure that if I read the book in between my busy schedule I would be able to savor every word. So, I waited 3 years to read it. After finishing my studies, on my long commute to and from work, and night classes I found the perfect space to read The Book Of Not.

The prequel to The Book Of Not, Nervous Conditions was and still is the book that drew me in with the first sentence, “I was not sorry when my brother died.” I did not expect to read those words, even in a fictional novel. With that in mind I dug through the whole book, Nervous Conditions, to find out why someone would utter such callous a statement. With that first sentence, I had already had an unfavourable opinion of Tambudzai Sigauke, the protagonist.

As I read through Nervous Conditions I found out that Tambudzai had very much the same experience as women in African society, being overlooked because of their gender. She was very much capable as her brother. I could see my mother, my sisters in her fortitude. More so I saw the pressure that most Zimbabwean children are put through. The pressure to succeed in school. The pressure to be that beacon of hope that comes out of a village. The pressure to be educated and still keep the humility, the language and culture of your people.

In The Book Of Not, the story takes place during Zimbabwe’s Second Chimurenga (War of Independence). For those who are born after 1980, the ‘Born Frees’, the war is just a story. We did not see it for ourselves the struggles that everyone had to go through. Those who participated in the war only speak of the ideology of the war, not the experience. The last time I encountered a novel set during the war was in the book, White Men, Black War.

Tambudzai Sigauke’s experiences during the Second Chimurenga are through her school mates, her sister, and her uncle. They paint the personal loses of war; death, maiming, and physical abuse. One could say, the author decided to balance the effects of war on “both sides” of the conflict. For me the war theme was one I had to talk about first, because it is not a happy one.

In Zimbabwe in particular and Southern Africa in general we like to pullout the big English words. One word I heard a lot growing up was perseverance. I am sure a lot of Zimbabweans are named perseverance.  I took perseverance to mean the fortitude to pursue one’s goals and dreams. Tambudzai wanted to be the best at Sacred Heart, and do right by her uncle. To be brilliant it requires a bit of sacrifice, you have to sacrifice relationships, you have to build new ones. Lastly, not everyone is going to like the choices you make.

Tambu found herself a nemesis in her dorm mate, Ntombi. Ntombi, who seemed not to feel the need to curry favour with people. Tambu seemed the need to be cordial. Ntombi and Tambu’s opposite personalities clash through out the book.

Posted in Uncategorized | Leave a comment

Double Ended Queue implementation in Java, and Operations run in O(1) time

public class Main {



    /**

     * @param args

     */

    public static void main(String[] args) {

    Deque<Integer> Q = new Deque<Integer>(6);

    try {

        Q.addFirst(3);

        System.out.println(Q);

        Q.addFirst(5);

        System.out.println(Q);

        Q.removeFirst();

        System.out.println(Q);

        Q.addLast(7);

        System.out.println(Q);

        Q.removeFirst();

        System.out.println(Q);

        Q.removeLast();

        System.out.println(Q);

        System.out.println(Q.isEmpty());

        Q.removeFirst();

        

    } catch (FullStackException e) {

        // TODO Auto-generated catch block

        System.out.println(e.getMessage());

    }catch (EmptyStackException e) {

        // TODO Auto-generated catch block

        System.out.println(e.getMessage());

    }

    

    }



}

/**

* Double queue class

*/

class Deque<E>

{

//f is front index

//r is rear index

//N is the size of the queue

//A is the queue

//E is a generic type 

    private int f,r,N;

    private E[] A;

    

    public Deque(int s)

    {

        A = (E[]) new Object[s];

        f = 0;

        r = 0;

        N = s;

    }

    

    public void addFirst(E e) throws FullStackException

    {

        if (size() == N - 1) throw new FullStackException();

        /**

         * Would work in Python

         * f = (f - 1 ) % N;

         */

        f = (f == 0) ? N - 1 : (f - 1 ) % N;

        A[f] = e;

    }

    

    public void addLast(E e) throws FullStackException

    {

        if (size() == N - 1) throw new FullStackException();

        A[r] = e;

        r = (r + 1 ) % N;

    }

    

    public E removeFirst() throws EmptyStackException

    {

        if (isEmpty()) throw new EmptyStackException();

        E temp = A[f];

        A[f] = null;

        f = (f + 1 ) % N;

        return temp;

    }

    

    public E removeLast() throws EmptyStackException

    {

        if (isEmpty()) throw new EmptyStackException();

        /**

         * Would work in Python

         * r = (r - 1 ) % N;

         */

        r = (r == 0) ? N - 1 : (r - 1 ) % N;

        E temp = A[r];

        A[r] = null;

        return temp;

    }

    

    public E getFirst() throws EmptyStackException

    {

        if (isEmpty()) throw new EmptyStackException();

        return A[f];

    }

    

    public E getLast() throws EmptyStackException

    {

        if (isEmpty()) throw new EmptyStackException();

        int temp = (r - 1 ) % N;

        return A[temp];

    }

    

    public int size()

    {

        return (N - f + r) % N;

    }

    

    public boolean isEmpty()

    {

        return f == r;

    }

    

    public String toString()

    {

        String str = "(";

        for(E e:A) str += e + ",";

        str += ")";

        return str;

    }

}



class FullStackException extends Exception

{

    public FullStackException()

    {

        super("Stack is full!");

    }

}



class EmptyStackException extends Exception

{

    public EmptyStackException()

    {

        super("Stack is empty!");

    }

}
Posted in Code samples | Tagged | Leave a comment

Resursion to add n by n elements in an array

Algorithm

 

Algorithm sum(A,n)

    Input: A, an n by n array

    Output: The sum of elements in A

 

    if n=1 then return A[0][0]

 

    s  ← 0

    while i < n -1 do

        s ← s + A[i][n-1] + A[n-1][i]

     return  A[n-1][n-1] + s  + sum(A,n-1)

 

Comments

Runs in O(n) time.

We work the array from the outside to the inside until n = 1

 

Implementation Java

 

 

 

 

 

public static int sum(int A[][], int n)

{

 

    if(n == 1) return A[0][0];

 

    int s = 0;

 

    for(int i = 0; i < n – 1 ; i++)   s+= A[i][n-1] + A[n-1][i];

 

    return A[n-1][n-1] + s  + sum(A,n-1);

 

}

Posted in Code samples | Leave a comment

power function in O(n) time, algorithm plus implementation in python

Algorithm number 1.

 

Algorithm powerOf(x,n)

        Input: integer n, non negative integer x

        Output : x to the power of n

        if n = 0

             return 1

        m ← 0

        while n > 0 do

             m ← m × n

             n ← n – 1

         return m

 

Implementation.

 

def powerOf(x,n):

    if n == 0 : return 1

    m = 0

    while n > 0:

       m *= x

       n -= 1

     return m

 

 

 

Algorithm (recursive) number 2.

 

Algorithm p(x,n)

     Input: integer n, non negative integer x

     Output : x to the power of n

     if n = 0

         return 1

     return x × p(x,n-1)

 

Implementation.

 

def p(x,n):

    if n == 0: return 1

    return x * p(x,n-1)

 

Posted in Code samples | Tagged | Leave a comment

Nonrecursive Factorial Algorithm and Implementation in python

Algorithm fact(n)
     input: Nonegative integer n
     output: Fibonacci value of n
     if n = 0
         return 1
     m ← 1
     k ← 1
     while n ≥ k do
           m ←  m * k
           k ← k + 1
      return m

Finally the Python implementation

>>> def fact(n):
…     if(n==0): return 1;
…     m = 1;
…     k = 1;
…     while(n >= k):
…             m = m * k;
…             k = k + 1;
…     return m;

>>> fact(1)
1
>>> fact(2)
2
>>> fact(3)
6
>>> fact(4)
24
>>>

Posted in Uncategorized | Leave a comment

How to translate Shona to English.

Shona to English translation is not easy. So is any translation, because no language is one to one with another language no matter how unrelated or related they are.

Shona for one, does not share any roots with English, other than that people of English decent ‘visited’ Shona lands for 90 years and then left.

Coming back to Translating Shona to English, consider VaShona.com’s Shona to English Translator. The translator seems to be in the developmental stage, however for simple translations it is sufficient.

I have not found any other (mechanical) Shona to English  translators that exist.

Posted in Uncategorized | Leave a comment