Skip to content

Instantly share code, notes, and snippets.

@rihenperry
Created November 22, 2014 17:51
Show Gist options
  • Save rihenperry/7d58b9ae25a1be37ce5d to your computer and use it in GitHub Desktop.
Save rihenperry/7d58b9ae25a1be37ce5d to your computer and use it in GitHub Desktop.
topcoders problems
package topCoderAlgos;
/**
* This program generates topCoderAlgos.Fibonacii series till
* the target number
*/
import java.util.ArrayList;
public class Fibonacii {
private ArrayList<Integer> fibSeries;
public ArrayList<Integer> generateFibonacii(int number) {
fibSeries = new ArrayList<>();
int leftNum = 0;
int rightNum = 1;
for (int num = 0; num <= number; num++) {
fibSeries.add(leftNum);
leftNum = leftNum + rightNum;
rightNum = leftNum - rightNum;
}
return fibSeries;
}
}
package topCoderAlgos;/*
* This program is based on a concept of real world Image Dithering which
* works by mixing more than one solid unique color elements(pixels) and
* generating new color elements from the same.
* This program checks for unique dithered elements in the screen and
* returns the result*/
public class ImageDithering {
private int netCount = 0;
public int count(String dithered, String[] screen) {
char ditherChar[] = dithered.toCharArray();
for (String element : screen) {
char elementToChar[] = element.toCharArray();
for (int eachChar = 0; eachChar < elementToChar.length; eachChar++)
for (char eachDither : ditherChar)
if (eachDither == elementToChar[eachChar]) netCount++;
}
return netCount;
}
}
package topCoderAlgos;
/**
* This class performs matrix operations
*/
public class Matrix {
private int N;
private int[][] c;
public int[][] computeDotMatrix(int[][] a,int[][] b){
N = a.length;
c = new int[N][N];
System.out.println(N);
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
{ // Compute dot product of row i and column j.
for (int k = 0; k < N; k++){
System.out.println("c"+"["+i+"]"+"["+j+"]"+"="+"a["+i+"]"+"["+k+"]"+"b"+"["+k+"]"+"["+j+"]"+"N="+N);
c[i][j] += a[i][k]*b[k][j];
}
}
return c;
}
}
package topCoderAlgos; /**
* Prime class supports generation of customized numbers using
* built-in configuration
*/
import java.util.ArrayList;
public class PrimeNumbers {
private ArrayList<Integer> primeList;
public boolean checkForPrime(int number) throws ArithmeticException {
for (int i = 2; i <= number; i++) {
if (number % i == 0) {
if (number == i)
return true;
else
break;
}
}
return false;
}
public ArrayList<Integer> generatePrimeNumbers(int number) {
//Java 7 diamond syntax.Not supported in version > 1.6
//compute primes till the target number
primeList = new ArrayList<>();
for (int num = 2; num <= number; num++) {
if (checkForPrime(num)) {
primeList.add(num);
}
}
return primeList;
}
}

topCoderAlgos

these are some of the problems that I have tackled on Topcoder.These maybe helpful for verification.

package topCoderAlgos;/*
*Class topCoderAlgos.Time takes CPU time and breaks it into
* hours,minutes and seconds.It further presents it
* in human readable format
*/
public class Time {
public String convertMachineTime(int seconds) {
short calHr, calMin, calMinSec;
calHr = (short) (seconds / (60 * 60));
calMinSec = (short) (seconds / 60);
calMin = (short) (calMinSec - (calHr * 60));
seconds = (short) (seconds % 3600);
seconds = seconds % 60;
return String.format("%d:%d:%d", calHr, calMin, seconds);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment