Saturday, April 28, 2012

Phonegap plugin for Ads on iPhone

I suppose you know how to create a Phonegap project and also how to add a plugin into the project.

First download the required code from here.
Add the "Ads" folder to your plugins list.

To do this, right-click on the Plugins folder in Xcode and select to “Add Files to…” the folder.


Navigate to the Ads folder, and select the folder. Check “Copy items

into. . .” and “Create groups for. . .” A ChildBrowser folder is added to the Plugins folder. Copy the content of "js" folder to desired location.

Then update the plist to use the plugin
In Supporting Files > PhoneGap.plist, add under Plugins

(click the down arrow to the left of Plugins, then click on the + symbol to open a new row):

Key: SAiOSAdPlugin

Type: String (default)

Value: SAiOSAdPlugin

See the "index.html" for initialization of the code.
Now, to display the Ad, call "showTheAd(true)"

and whenever you want to hide the Ad call "showTheAd(false)"

Make sure you link in the iAd framework to your project. Do this by going to your project settings in Xcode. Then select your target and in the right pane go to "Build Phases." Then in the "Link Binary with Libraries" section hit the "+" button to add more frameworks. Select iAd.framework click "Add" and then rebuild.

Friday, April 27, 2012

Google Plus +1 and share

This a html code that can be added to website to share some content on  GooglePlus

You can use any image(but select a related one). By clicking on the image, content will be posted

on the users G+ account. Consider the following samople code
<a href="https://plusone.google.com/_/+1/confirm?hl=en&url=<LINK TO BE SHARED>"

<img src="ANY IMAGE" /></a>

Also the proper description will be posted, along with the link.

Monday, April 16, 2012

Shortest Job First Scheduling using HEAP







// Input is from the file "input.txt" in the form (a1,t1),(a2,t2),(a3,t3)  (arrival time, time to complete ie, burst time)




import java.io.*;
import java.util.*;

public class SJFHeap {
    static class point {
        double time;
        double run;
        int no;
        double total;

        point(int i, double a, double b) {
            no = i;
            time = a;
            run = b;
            total = 0;
        }
    }

    static class MHeap {
        private point[] Heap;
        private int maxsize;
        private int size;
        public MHeap(int max) {
            maxsize = max;
            Heap = new point[maxsize];
            size = 0;
        }

        private int leftchild(int pos) {
            return 2 * pos;
        }

        private int rightchild(int pos) {
            return 2 * pos + 1;
        }

        private int parent(int pos) {
            return pos / 2;
        }

        private boolean isleaf(int pos) {
            return ((pos > size / 2) && (pos <= size));
        }

        private void swap(int pos1, int pos2) {
            point tmp;
            tmp = Heap[pos1];
            Heap[pos1] = Heap[pos2];
            Heap[pos2] = tmp;
        }

        public void insert(point elem) {
            size++;
            Heap[size] = elem;
            int current = size;
            int paren = parent(current);
            if (paren >= 1)
                while (Heap[current].run < Heap[paren].run) {
                    swap(current, paren);
                    current = parent(current);
                }
        }

        public void print() {
            int i;
            for (i = 1; i <= size; i++)
                System.out.println("(" + Heap[i].time + "," + Heap[i].run + ")");
        }

        public point removemin() {
            swap(1, size);
            size--;
            if (size != 0)
                pushdown(1);

            return Heap[size + 1];
        }

        private void pushdown(int position) {
            int smallestchild;
            while (!isleaf(position)) {
                smallestchild = leftchild(position);
                if ((smallestchild < size)
                        && (Heap[smallestchild].run > Heap[smallestchild + 1].run))
                    smallestchild = smallestchild + 1;
                if (Heap[position].run <= Heap[smallestchild].run)
                    return;
                swap(position, smallestchild);
                position = smallestchild;
            }
        }
    }

    public static void main(String[] args) throws IOException {
        MHeap h = new MHeap(10);
        FileInputStream fstream = new FileInputStream("input.txt");
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        point ele;
        String strLin;
        strLin = in.readLine();
        StringTokenizer st = new StringTokenizer(strLin, " (,)");
        int mp = st.countTokens();
        point[] task = new point[mp / 2];
        point[] tem = new point[mp / 2];
        int l = 0, num;
        while (st.hasMoreTokens())
        {
            String x = st.nextToken();
            double x1 = Double.parseDouble(x);
            String y = st.nextToken();
            double y1 = Double.parseDouble(y);
            point pt = new point(l + 1, x1, y1);
            task[l] = pt;
            l++;
        }
        num = l;
        int j = 0, i = 0, killed = 0;
        double now = 0, prev = 0, diff, kill = 0;
        point pt = task[0];
        while (i != num)
        {
            now = task[i].time;
            if (i == 0)
            {
                h.insert(task[i]);
                i++;
                prev = now;
            }
            else
            {
                pt = h.removemin();
                diff = now - prev;
                if (pt.run > diff)
                {
                    pt.run = pt.run - diff;
                    h.insert(pt);
                }
                else
                {
                    pt.total = prev + pt.run - pt.time;
                    kill = prev + pt.run;
                    pt.run = 0;
                    tem[j++] = pt;
                }
                h.insert(task[i]);
                prev = kill > 0 ? kill : now;
                i++;
            }
        }

        h.insert(h.removemin());
        while (h.size != 0)
        {
            pt = h.removemin();
            pt.total = prev + pt.run - pt.time;
            tem[j++] = pt;
            prev = prev + pt.run;
        }
        for (i = 0; i < j; i++)
            System.out.println("Process-" + tem[i].no + " processed for " + tem[i].total);
    }
}

Tuesday, April 10, 2012

Android : Failed to parse the output of 'adb version'

This error occurs due to 32bit incompatibility on your system.

One solution is to install the 32 bit compatibility on your system :
sudo apt-get install ia32-libs

Fix In-Built Mic Problem in Ubuntu

Code for terminal:
sudo gedit /etc/modprobe.d/alsa-base.conf

Add this line to the bottom of the gedit file:
options snd-hda-intel model=auto

then Reboot and you're done!

BURG (say good bye to GRUB)

BURG stands for Brand-new Universal loadeR from GRUB. It's based on GRUB, and add features like new object format and configurable menu system.

For Ubuntu 11.04 users, add the following PPA :
sudo add-apt-repository ppa:n-muench/burg
Then use the following command to download and install the loader, themes and emulator:
sudo apt-get update && sudo apt-get install burg
During the installation, it should ask you to write the new boot loader to MBR. If you skip that step, you can later use the following command to update MBR of hd0:
sudo burg-install "(hd0)"

Tuesday, April 3, 2012

Spell Checker using TRIE






Spell checker program using TRIE data structure. Dictionary source is from Dictionary.txt, which is to be in the same directory of the program, or else change the file name and location



//Possible.java

import java.io.*;
import java.util.*;

class TrieNode {
    char letter;
    TrieNode[] links;
    boolean fullWord;

    TrieNode(char letter, boolean fullWord) {
        this.letter = letter;
        links = new TrieNode[26];
        this.fullWord = fullWord;
    }
}

public class Possible {

    static TrieNode createTree()
    {
        return(new TrieNode('', false));
    }

    static void insertWord(TrieNode root, String word)
    {
        int offset = 97;
        int l = word.length();
        char[] letters = word.toCharArray();
        TrieNode curNode = root;
        for (int i = 0; i < l; i++)
        {
            if (curNode.links[letters[i] - offset] == null)
                curNode.links[letters[i] - offset] = new TrieNode(letters[i], i == l - 1 ? true : false);
            curNode = curNode.links[letters[i] - offset];
        }
    }

    static boolean find(TrieNode root, String word)
    {
        char[] letters = word.toCharArray();
        int l = letters.length;
        int offset = 97;
        TrieNode curNode = root;
        int i;
        for (i = 0; i < l; i++)
        {
            if (curNode == null)
                return false;
            curNode = curNode.links[letters[i] - offset];
        }
        if (i == l && curNode == null)
            return false;
        if (curNode != null && !curNode.fullWord)
            return false;
        return true;
    }

    static void printTree(TrieNode root, int level, char[] branch, BufferedWriter out) throws IOException
    {
        if (root == null)
            return;
        for (int i = 0; i < root.links.length; i++)
        {
            branch[level] = root.letter;
            printTree(root.links[i], level + 1, branch, out);
        }
        if (root.fullWord)
        {
            for (int j = 1; j <= level; j++)
                out.write(branch[j]);
            out.write("n");
        }
    }

    public static void possible(TrieNode tree, String word) {
        Set<String> result = new HashSet<String>();
        // Remove a character
        for (int i = 0; i < word.length(); ++i)
            result.add(word.substring(0, i) + word.substring(i + 1));
        // Swap two consecutive characters
        for (int i = 0; i < word.length() - 1; ++i)
            result.add(word.substring(0, i) + word.substring(i + 1, i + 2) + word.substring(i, i + 1)
                    + word.substring(i + 2));
        // Replace a character with other
        for (int i = 0; i < word.length(); ++i)
            for (char c = 'a'; c <= 'z'; ++c)
                result.add(word.substring(0, i) + String.valueOf(c) + word.substring(i + 1));
        // Add a new character
        for (int i = 0; i <= word.length(); ++i)
            for (char c = 'a'; c <= 'z'; ++c)
                result.add(word.substring(0, i) + String.valueOf(c) + word.substring(i));
        ArrayList<String> res = new ArrayList<String>(result);
        int j = 0;
        for (int i = 0; i < result.size(); i++)
            if (find(tree, res.get(i))) {
                if (j == 0)
                    System.out.print("Do you mean : ");
                System.out.print(res.get(i) + " ");
                j++;
            }
    }

    public static void main(String[] args) throws IOException
    {
        TrieNode tree = createTree();
        long build1 = System.currentTimeMillis();
        File f = new File("Dictionary.txt");
        FileInputStream fread = new FileInputStream(f);
        BufferedReader br = new BufferedReader(new InputStreamReader(fread));
        String ele;
        while ((ele = br.readLine()) != null)
            insertWord(tree, ele);
        long build2 = System.currentTimeMillis();
        System.out.println("Time to build data structure is:" + (build2 - build1));
        BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter the word :");
        String searchWord = br1.readLine();
        if (find(tree, searchWord))
        {
            System.out.println("The word was found");
        }
        else
        {
            long find1 = System.currentTimeMillis();
            possible(tree, searchWord);
            long find2 = System.currentTimeMillis();
            System.out.println("nTime to find possibilities is:" + (find2 - find1));
            System.out.println("Do you want to add this word to dictionary [y/n]: ");
            if (br1.readLine().equals("y")) {
                long add = System.currentTimeMillis();
                insertWord(tree, searchWord);
                long add1 = System.currentTimeMillis();
                try {
                    f.delete();
                    File f1 = new File("Dictionary.txt");
                    FileWriter fwrite = new FileWriter(f1, true);
                    BufferedWriter out = new BufferedWriter(fwrite);
                    char[] branch = new char[50];
                    printTree(tree, 0, branch, out);
                    out.close();
                } catch (Exception e) {
                    System.err.println("Error: " + e.getMessage());
                }
                long add2 = System.currentTimeMillis();
                System.out.println("Time to add a new word to Trie and file are:" + (add1 - add) + " " + (add2 - add1));
            }
        }
    }
}

Spell Checker




This is a program which takes a word as input and gives possible suggestions to correct the spelling of the word, by searching them from the dictionary of words(a text file Dictionary.txt).





// Spell.java

import java.io.*;
import java.util.*;

public class Spell
{
    static ArrayList<String> wordsList = new ArrayList<String>();
    public static void main(String args[]) throws IOException
    {
        try {
            long build1 = System.currentTimeMillis();
            FileInputStream fstream = new FileInputStream("Dictionary.txt");
            BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
            String ele;
            while ((ele = br.readLine()) != null)
                wordsList.add(ele);
            long build2 = System.currentTimeMillis();
            System.out.println("Time to build data structure is:" + (build2 - build1));
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        }

        BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter the word :");
        String wd = br1.readLine();
        possible(wd);
        if (!wordsList.contains(wd)) {
            System.out.println("Do you want to add word to dictionary [y/n]: ");
            if (br1.readLine().equals("y")) {
                long add1 = System.currentTimeMillis();
                wordsList.add(wd);
                try {
                    FileWriter fstream = new FileWriter("Dictionary.txt", true);
                    BufferedWriter out = new BufferedWriter(fstream);
                    out.write("n" + wd);
                    out.close();
                } catch (Exception e) {
                    System.err.println("Error: " + e.getMessage());
                }
                long add2 = System.currentTimeMillis();
                System.out.println("Time to add word to data structure is:" + (add2 - add1));
            }
        }
    }

    public static void possible(String word) {
        long find1 = System.currentTimeMillis();
        Set<String> result = new HashSet<String>();
        // Remove a character
        for (int i = 0; i < word.length(); ++i)
            result.add(word.substring(0, i) + word.substring(i + 1));
        // Swap two consecutive characters
        for (int i = 0; i < word.length() - 1; ++i)
            result.add(word.substring(0, i) + word.substring(i + 1, i + 2) + word.substring(i, i + 1)
                    + word.substring(i + 2));
        // Replace a character with other
        for (int i = 0; i < word.length(); ++i)
            for (char c = 'a'; c <= 'z'; ++c)
                result.add(word.substring(0, i) + String.valueOf(c) + word.substring(i + 1));
        // Add a new character
        for (int i = 0; i <= word.length(); ++i)
            for (char c = 'a'; c <= 'z'; ++c)
                result.add(word.substring(0, i) + String.valueOf(c) + word.substring(i));
        ArrayList<String> res = new ArrayList<String>(result);
        int j = 0;
        for (int i = 0; i < result.size(); i++)
            if (wordsList.contains(res.get(i))) {
                if (j == 0)
                    System.out.print("You could have typed : ");
                System.out.print(res.get(i) + " ");
                j++;
            }
        long find2 = System.currentTimeMillis();
        System.out.println("nTime to find possibilities is:" + (find2 - find1));
    }
}