Archive for the ‘Code Snippets’ Category.
March 7, 2010, 11:17 pm
public static void printSpirally(int[][] a) {
int N = a[0].length;
for (int i = N - 1, j = 0; i > 0; i--, j++) {
for (int k = j; k < i; k++)System.out.println(a[j][k]);
for (int k = j; k < i; k++)System.out.println(a[k][i]);
for (int k = i; k > j; k--)System.out.println(a[i][k]);
for (int k = i; k > j; k--)System.out.println(a[k][j]);
}
int middle = (N / 2);
if ((N % 2) == 1) {
System.out.println(a[middle][middle]);
}
}
March 7, 2010, 10:59 am
public static boolean isVowel(char inp) {
char c = Character.toUpperCase(inp);
if ((c == 'A') || (c == 'E') || (c == 'I') || (c == 'O') || (c == 'U')) {
return true;
}
return false;
}
public static void reverseVowels(char[] a) {
int start = 0;
int end = a.length - 1;
while (start < end) {
while ((start < a.length) && !isVowel(a[start])) {
start++;
}
while ((end > 0) && !isVowel(a[end])) {
end--;
}
if ((start > = end) || (start == a.length) || (end == -1)) {
return;
}
//swap start and end;
char t = a[start];
a[start] = a[end];
a[end] = t;
start++;
end--;
}
}
March 7, 2010, 12:05 am
public static void shuffle(int[] a)
{
int N = a.length;
Random r = new Random(new Date().getTime());
for (int i = 0; i < a.length-1; i++)
{
int tmp=0;
// Find a random element from i to N-1
int randElem = i + r.nextInt(N-i);
//Replace randElem with current element
tmp = a[i];a[i]=a[randElem];a[randElem]=tmp;
}
}
March 6, 2010, 9:34 pm
Find if the sum of two elements in a sorted array sum up to b (a number) with complexity of O(n).
public static void find2ElementsOfSum(int[] a,int S)
{
int left = 0;
int right = a.length-1;
while(left < right)
{
int sum = a[left]+a[right];
System.out.println("current Sum="+sum);
if(sum==S){
System.out.println("FOUND left="+left+" rigt="+right);
return;
}
if(sum < S)
{
left++;
}else
{
right--;
}
}
System.out.println("NOT FOUND");
}
March 6, 2010, 6:43 am
Given an array of positive and negative numbers. Find the contiguous sub array of maximum sum.
public static void subArraySum(int a[])
{
int i=0;
int j=0;
int start=0;
int end=0;
int maxSum = -99999;
int cSum = 0;
while(j < a.length)
{
cSum +=a[j];
if(cSum>maxSum)// Set this as the new result
{
start=i;
end = j;
maxSum=cSum;
}else if(cSum < 0)
{
cSum=0;
i=j+1;
}
j++;
}
System.out.println("start="+start+" end="+end+" maxSum="+maxSum);
}
March 5, 2010, 10:28 pm
public static int binarySearch(int a[],int s)
{
int N = a.length;
int start=0;
int end = N-1;
while(start <= end)
{
int mid = start+(end-start)/2;
if(a[mid] > s)
{
end=mid-1;
}else if(a[mid] < s)
{
start=mid+1;
}else
{
return mid;
}
}
return -(start+1);
}
February 12, 2010, 1:48 am
To find the definition of view SYS.ALL_TABLES use the following SQL code
select TEXT
from DBA_VIEWS
where OWNER = 'SYS'
and VIEW_NAME = 'ALL_TABLES'
December 19, 2009, 9:52 am
$colorList = array(0 =>"red",1=>"green",2=>"blue",3=>"black",4=>"white");
foreach( $colorList as $key => $value){
echo "Key: $key, Val: $value ";
}
OUTPUT:
Key: 0, Val: red
Key: 1, Val: green
Key: 2, Val: blue
Key: 3, Val: black
Key: 4, Val: white
December 13, 2009, 11:43 am
<?php
//Connect to Databse
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($G['dbname']);
?>
<?php
// Simple query returning a single row 'COL_VAL'
$query = " SELECT 'COL_VAL' as COL_VAL ";
//Fetch Result
$result = mysql_query($query) or die('Error2, query failed:'.$query);
//Iterate through Result
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$C_COL_VAL = $row['COL_VAL'];
echo "$C_COL_VAL";
}
?>
<?php
//Closing DB Connection
mysql_close($conn);
?>
December 13, 2009, 11:04 am
$mappings = array ('windows' => 'microsoft', 'ipod' => 'apple', 'corby' => 'samsum');
print_r($mappings);
output:
Array ( [windows] => microsoft [ipod] => apple [corby] => samsum )