Redundancy Checker

August 4th, 2009









/*

 *        Code by David Beuze. No rights reserved :) - 2006

 *

 *        contact: smerpy@gmail.com

 */



/*

 *        This code snippet takes a list of objects and checks for any redundancy in the list.

 *        In this example I’ve taken a list of Integer, but it can be generalized to any kind

 *        of object.

 */



/*Output:

 

 * Oh no! The value 1 is redundant.

 

 * */





import java.util.Arrays;

import java.util.List;

import java.util.ListIterator;



public class RedundancyChecker {



  public static void main(String[] a) {

    new RedundancyChecker();

  }



  public RedundancyChecker() {



    Integer[] array = new Integer[5]// Create and

    // array of

    // Integer

    Integer i0 = new Integer(0);

    array[0= i0;

    Integer i1 = new Integer(1)// <——–

    array[1= i1; // |

    Integer i2 = new Integer(2)// |— redundant

    // values

    array[2= i2; // |

    Integer i3 = new Integer(1)// <——–

    array[3= i3;

    Integer i4 = new Integer(4);

    array[4= i4;



    // transform the array into a List

    List l = Arrays.asList(array);



    // Check the List

    checkForRedundancy(l);

  }



  public boolean checkForRedundancy(List l) {

    ListIterator li1 = l.listIterator()// Make a

    // general

    // ListIterator

    // on the list



    while (li1.hasNext()) {

      // Store the value of the actual first checked

      // element of the List,

      // it needs to be stored because it calls the

      // .next(), which we can call only once per loop

      // in order to sweep the whole list.

      int check1 = ((Integerli1.next()).intValue();



      // Make a second ListIterator that will start

      // with the element that is just after the

      // actual first checked one,

      // in order to avoid checking several times the

      // same elements.

      ListIterator li2 = l.listIterator(li1.nextIndex() 1);



      while (li2.hasNext()) {

        // Store the following elements one by

        // one and check to see if they match

        // the actual one.

        int check2 = ((Integerli2.next()).intValue();

        if (check1 == check2) {

          System.out.println("Oh no! The value " + check1 + " is redundant.");

          return true;

        }

      }

      // The .next() method has already been called at

      // the beginning of the loop.

    }

    return false;

  }

}

           

       

Using the TRANSLATE() Function: to encode and decode

August 4th, 2009





SQL>

SQL> –Using the TRANSLATE() Function

SQL>

SQL> –TRANSLATE(x, from_string, to_stringto convert the occurrences of characters in from_string found in x to corresponding characters in to_string.

SQL>

SQL> –TRANSLATE(): shift each character in the string SECRET MESSAGE: MEET ME IN THE PARK by four places to the right: A becomes E, B becomes F

SQL>

SQL> SELECT TRANSLATE(‘SECRET MESSAGE: MEET ME IN THE PARK‘,

  2     ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ‘,

  3     ‘EFGHIJKLMNOPQRSTUVWXYZABCD‘) FROM dual;



TRANSLATE(‘SECRETMESSAGE:MEETMEINTH

———————————–

WIGVIX QIWWEKI: QIIX QI MR XLI TEVO



SQL>

SQL> –select translate(‘www.java2s.com‘,’wjavscom‘,’abced123‘) from dual;

SQL>

SQL>



           

       

Currency: getInstance(Locale loc)

August 4th, 2009





/*

 * Output: 

Symbol: $

Default fractional digits: 2

 */



import java.util.Currency;

import java.util.Locale;



public class MainClass {

  public static void main(String args[]) {

    Currency c;



    c = Currency.getInstance(Locale.US);



    System.out.println("Symbol: " + c.getSymbol());

    System.out.println("Default fractional digits: "

        + c.getDefaultFractionDigits());

  }

}





           

       

Assign value to global variable

August 4th, 2009





#include <stdio.h>



int count;



int main(void)

{

  extern int count; /* this refers to global count */



  count = 10;

  printf("%d", count);



  return 0;

}





           

       

Copy vector and list

August 4th, 2009



 

 



/* The following code example is taken from the book

 * "The C++ Standard Library - A Tutorial and Reference"

 * by Nicolai M. Josuttis, Addison-Wesley, 1999

 *

 * (C) Copyright Nicolai M. Josuttis 1999.

 * Permission to copy, use, modify, sell and distribute this software

 * is granted provided this copyright notice appears in all copies.

 * This software is provided "as is" without express or implied

 * warranty, and with no claim as to its suitability for any purpose.

 */

#include <iostream>

#include <vector>

#include <list>

#include <deque>

#include <algorithm>

using namespace std;



int main()

{

    list<int>   coll1;

    vector<int> coll2;

    

    // insert elements from 1 to 9

    for (int i=1; i<=9; ++i) {

        coll1.push_back(i);

    }



    // resize destination to have enough room for the overwriting algorithm

    coll2.resize (coll1.size());



    /* copy elements from first into second collection

     * - overwrites existing elements in destination

     */

    copy (coll1.begin(), coll1.end(),     // source

          coll2.begin());                 // destination



    /* create third collection with enough room

     * - initial size is passed as parameter

     */

    deque<int> coll3(coll1.size());



    // copy elements from first into third collection

    copy (coll1.begin(), coll1.end(),     // source

          coll3.begin());                 // destination

}