Sunday, September 23, 2012

How to count character occurences in a text using PLSQL

Find how many "\" in the given string:

1. E.g. string = '12\34\567\89\kkk   '
select length(string )-length(replace(string ,'\',''))
from dual;

2. with t as (
select string from dual)
select length(regexp_replace( string , '[^\]')) / length('\') cnt
from t;

3. SELECT LENGTH(REGEXP_REPLACE(string ,'[^\]')) FROM DUAL;

*********** Following works only in 11g and higher.. **************

4.select  regexp_count('this@here@there.com', '[@]') from dual; 

5. for 2 or more characters as one
SELECT REGEXP_COUNT('123123123123123', '(12)3', 1, 'i') REGEXP_COUNT
   FROM DUAL;




Wednesday, January 4, 2012

Math Game using HTML, CSS, JavaScript

<html>
<head>

<!-- link style sheet -->
<link href="../css/main.css" rel="stylesheet" type="text/css">

<!--starts scripting-->
<script type="text/javascript">

    function load_game()
    {
       
        //declare arrays and variables for use below
        var numbers1 = new Array();
        var numbers2 = new Array();
        var answers = new Array();
        var operators= new Array();
        var opt = new Array();
        var ans = new Array();
        var score=0;
        var mks=2;
       
       
        for (x = 0; x < 10; x++){

            //generating random numbers
            numbers1[x] = (Math.floor(Math.random()*101));
            numbers2[x] = (Math.floor(Math.random()*101));
            operators[x] = (Math.floor(Math.random()*4));
        }

   
        //promt to user to input answers
        for (z = 0; z < 10; z++){           

            if(operators[z]==0)
            {
                opt[z]='+';
                answers[z] = prompt(numbers1[z]+" + "+numbers2[z],"");   
                ans [z] = numbers1[z] + numbers2[z];
            }
            if(operators[z]==1)
            {
                opt[z]='-';
                answers[z] = prompt(numbers1[z]+" - "+numbers2[z],"");   
                ans [z] = numbers1[z] - numbers2[z];
            }
            if(operators[z]==2)
            {
                opt[z]='*';
                answers[z] = prompt(numbers1[z]+" * "+numbers2[z],"");   
                ans [z] = numbers1[z] * numbers2[z];
            }
            if(operators[z]==3)
            {
                opt[z]='/';
                answers[z] = prompt(numbers1[z]+" / "+numbers2[z],"Round answer into 2 decimal points");   
                ans [z] =Math.round(numbers1[z] / numbers2[z]*100)/100;
            }

            if(isNaN(answers[z])){

                alert("Invalid Answer");
                z=z-1;
            }
       
        }
       
        //Display results..
        for(i=0;i<10;i++){

            document.write((i+1)+") "+numbers1[i]+opt[i]+numbers2[i]+"= ");

            //check for correct answer
             if(answers[i]==ans[i])
             {
                 document.write(answers[i]+" ..... Well done!! correct answer.<br/>");
                score=score+mks;
             }

            // check whether the answered or skipped
            else if(answers[i]=="" || answers[i]==null){
                document.write("   ...... skipped by the player.<br/>");

            }

            // when answer is incorrect
             else
             {
                document.write(answers[i]+" ..... Oops!! incorrect, The correct answer is:"+ans[i]+".<br/>");
             }   

        }
       
        document.write("<br/>Your Score is: "+score); //DISPALY SCORE

        //Change background color according to the score
        if(score < 6){
            document.bgColor='#F6358A';
        }
        else if(score < 10){
            document.bgColor='#46C7C7';
        }
        else if(score < 16){
            document.bgColor='#4CC417';
        }
        else{
            document.bgColor='#6698FF';
        }
    }

</script>

</head>

<body bgcolor='#FFCC66'>

    <br/><h1 align="center">** Welcome To Maths Game **</h1>
   
    <script>
        load_game(); //run the game..
    </script>
   

    <p>
        <font size="2" ><br/><hr /><a href="mailto:wmwaves@gmail.com"> wmwaves@gmail.com</a>
        </br> Last modified: 15 May 2011  </font>
    </p>

</body>
</html>

Tuesday, February 8, 2011

Data manipulating Algorithms using Arrays

/* Author : Tharanga Welihinda
 * Date :26/10/2010
 *
 */

 import java.util.*;

 public class Set_Array{
    
     public static int arrsize=22;
     public static Integer[] Numbers=new Integer[arrsize];
     public static Integer[] temp=new Integer[arrsize];
     public static int z,flage,no;
    
     public static void insert(int num){
    
         Numbers[no_elements()]=num;
         no++;
     }
     public static int no_elements()
     {
         int x=0;
         while(Numbers[x]!=null){
             x++;
         }       
         return x;
     }
    
     public static void insert(int num,int pos){
        
         for(int i=no_elements();i>pos;i--){
            
             Numbers[i]=Numbers[i-1];
         }
         Numbers[pos]=num;
         no++;
     }
    
     public static Integer[] search(int e){   
         z=0;   
         for(int x=0;x<no;x++)
         {
             if(Numbers[x]==e)
             {
                 temp[z]=x;
                 z++;
             }
         }
         return temp;
     }
    
     public static void Delete(int num){
        
         temp[0]=null;
         temp=search(num);
        
         if(temp[0]==null){
             System.out.println ("\nSorry.. No such a Element");
             return ;
         }
        
         int var=z;
         for(int e=0;e<z;e++){
        
             for(int i=temp[var-1];i<no_elements();i++){       
       
                 Numbers[i]=Numbers[i+1];
             }
             var--;
             no--;
         }
         dashes();
         printarray(Numbers);
         System.out.println ();
     }
     public static void printarray(Integer[] arr){
    
         for(int x=0;x<arr.length;x++){
             if(arr[x]!=null)
             System.out.print (arr[x]+" ");
            
         }
     }
    
     public static void arraysort(Integer[] arr){
        
         Integer[] sorted=new Integer[no];
         int s=0;
         for(int x=0;x<arr.length;x++){
             if(arr[x]!=null){
                 sorted[s]=arr[x];
                 s++;
             }
         }
         Arrays.sort(sorted);
         printarray(sorted);
     }
     public static void dashes(){
         for(int a=0;a<20;a++)
             System.out.print ("--");
             System.out.println ();
     }
    
     public static void main (String[] args) {
         try{
             do{
                 flage=1;
                 Scanner input=new Scanner(System.in);
                 System.out.print ("\nEnter number of Elements you want : ");
                 no=input.nextInt();
                 System.out.println ();
                
                 if(no<1 || no>20)
                 {
                     System.out.println ("Enter a number 1 - 20");
                     flage=0;
                 }
             }while(flage==0);
    
             Scanner input=new Scanner(System.in);

             for(int k=0;k<no;k++){
            
             System.out.print ("Enter element no "+(k)+": ");
             Numbers[k]=input.nextInt();
            
             }
            
             System.out.println ("\nYour List of Numbers are:");
             dashes();
             printarray(Numbers);
            
             System.out.print ("\n\nEnter number you want to input : ");
             int value=input.nextInt();
             System.out.print ("\nEnter the position you want\nenter 99 to skip the position : ");
             int pos=input.nextInt();
             if(pos<0 || pos>no)
                 System.out.println ("Enter the position between 0 -"+no);
            
             if(pos==99)
                 insert(value);
             else
                 insert(value,pos);
                
             System.out.println ("\nValue Inserted ...");
             dashes();
             printarray(Numbers);
            
             System.out.print ("\n\nEnter number you want to search : ");
             value=input.nextInt();
            
             search(value);
             if(temp[0]==null)
                 System.out.println ("\nSorry.. No such a Element");
             else{
                 System.out.print ("\nElemeny found in position: ");
                 printarray(temp);
                 System.out.println ();
             }
                            
             System.out.print ("\nEnter number you want to delete : ");
             value=input.nextInt();
             Delete(value);
            
             System.out.println ("\nSorted List of your array");
             dashes();
             arraysort(Numbers);
             System.out.println ();
         }
         catch(Exception e){
             System.out.println ("\n*** WRONG INPUT!.. TRY AGAIN.. ***");
            
            
                
         }
    
    }   
    
 }