Merge branch 'master' into level-order
This commit is contained in:
commit
bb6f8271ff
87
Conversions/AnyBaseToAnyBase.java
Normal file
87
Conversions/AnyBaseToAnyBase.java
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
import java.util.InputMismatchException;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class for converting from any base to any other base, though it's unclear how digits greater than
|
||||||
|
* 36 would be represented in bases >36.
|
||||||
|
*
|
||||||
|
* @author Michael Rolland
|
||||||
|
* @version 2017.09.29
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class AnyBaseToAnyBase {
|
||||||
|
|
||||||
|
// Driver
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner in = new Scanner(System.in);
|
||||||
|
System.out.print("Enter number: ");
|
||||||
|
String n = in.nextLine();
|
||||||
|
int b1=0,b2=0;
|
||||||
|
while (true) {
|
||||||
|
try {
|
||||||
|
System.out.print("Enter beginning base: ");
|
||||||
|
b1 = in.nextInt();
|
||||||
|
System.out.print("Enter end base: ");
|
||||||
|
b2 = in.nextInt();
|
||||||
|
break;
|
||||||
|
} catch (InputMismatchException e) {
|
||||||
|
System.out.println("Invalid input.");
|
||||||
|
in.next();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println(base2base(n, b1, b2));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method to convert any integer from base b1 to base b2. Works by converting from b1 to decimal,
|
||||||
|
* then decimal to b2.
|
||||||
|
* @param n The integer to be converted.
|
||||||
|
* @param b1 Beginning base.
|
||||||
|
* @param b2 End base.
|
||||||
|
* @return n in base b2.
|
||||||
|
*/
|
||||||
|
public static String base2base(String n, int b1, int b2) {
|
||||||
|
// Declare variables: decimal value of n,
|
||||||
|
// character of base b1, character of base b2,
|
||||||
|
// and the string that will be returned.
|
||||||
|
int decimalValue = 0, charB2;
|
||||||
|
char charB1;
|
||||||
|
String output="";
|
||||||
|
// Go through every character of n
|
||||||
|
for (int i=0; i<n.length(); i++) {
|
||||||
|
// store the character in charB1
|
||||||
|
charB1 = n.charAt(i);
|
||||||
|
// if it is a non-number, convert it to a decimal value >9 and store it in charB2
|
||||||
|
if (charB1 >= 'A' && charB1 <= 'Z')
|
||||||
|
charB2 = 10 + (charB1 - 'A');
|
||||||
|
// Else, store the integer value in charB2
|
||||||
|
else
|
||||||
|
charB2 = charB1 - '0';
|
||||||
|
// Convert the digit to decimal and add it to the
|
||||||
|
// decimalValue of n
|
||||||
|
decimalValue = decimalValue * b1 + charB2;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Converting the decimal value to base b2:
|
||||||
|
// A number is converted from decimal to another base
|
||||||
|
// by continuously dividing by the base and recording
|
||||||
|
// the remainder until the quotient is zero. The number in the
|
||||||
|
// new base is the remainders, with the last remainder
|
||||||
|
// being the left-most digit.
|
||||||
|
|
||||||
|
// While the quotient is NOT zero:
|
||||||
|
while (decimalValue != 0) {
|
||||||
|
// If the remainder is a digit < 10, simply add it to
|
||||||
|
// the left side of the new number.
|
||||||
|
if (decimalValue % b2 < 10)
|
||||||
|
output = Integer.toString(decimalValue % b2) + output;
|
||||||
|
// If the remainder is >= 10, add a character with the
|
||||||
|
// corresponding value to the new number. (A = 10, B = 11, C = 12, ...)
|
||||||
|
else
|
||||||
|
output = (char)((decimalValue % b2)+55) + output;
|
||||||
|
// Divide by the new base again
|
||||||
|
decimalValue /= b2;
|
||||||
|
}
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
}
|
@ -40,7 +40,7 @@ public class Fibonacci {
|
|||||||
f = 1;
|
f = 1;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
f = fib(n-1) + fib(n-2);
|
f = fibMemo(n-1) + fibMemo(n-2);
|
||||||
map.put(n,f);
|
map.put(n,f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
38
Dynamic Programming/Knapsack.java
Normal file
38
Dynamic Programming/Knapsack.java
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
// A Dynamic Programming based solution for 0-1 Knapsack problem
|
||||||
|
|
||||||
|
public class Knapsack
|
||||||
|
{
|
||||||
|
|
||||||
|
private static int knapSack(int W, int wt[], int val[], int n)
|
||||||
|
{
|
||||||
|
int i, w;
|
||||||
|
int rv[][] = new int[n+1][W+1]; //rv means return value
|
||||||
|
|
||||||
|
// Build table rv[][] in bottom up manner
|
||||||
|
for (i = 0; i <= n; i++)
|
||||||
|
{
|
||||||
|
for (w = 0; w <= W; w++)
|
||||||
|
{
|
||||||
|
if (i==0 || w==0)
|
||||||
|
rv[i][w] = 0;
|
||||||
|
else if (wt[i-1] <= w)
|
||||||
|
rv[i][w] = Math.max(val[i-1] + rv[i-1][w-wt[i-1]], rv[i-1][w]);
|
||||||
|
else
|
||||||
|
rv[i][w] = rv[i-1][w];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return rv[n][W];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Driver program to test above function
|
||||||
|
public static void main(String args[])
|
||||||
|
{
|
||||||
|
int val[] = new int[]{50, 100, 130};
|
||||||
|
int wt[] = new int[]{10, 20, 40};
|
||||||
|
int W = 50;
|
||||||
|
int n = val.length;
|
||||||
|
System.out.println(knapSack(W, wt, val, n));
|
||||||
|
}
|
||||||
|
}
|
62
Dynamic Programming/LongestIncreasingSubsequence.java
Normal file
62
Dynamic Programming/LongestIncreasingSubsequence.java
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Afrizal Fikri (https://github.com/icalF)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class LongestIncreasingSubsequence {
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
int n = sc.nextInt();
|
||||||
|
|
||||||
|
int ar[] = new int[n];
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
ar[i] = sc.nextInt();
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println(LIS(ar));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int upperBound(int[] ar, int l, int r, int key) {
|
||||||
|
while (l < r-1) {
|
||||||
|
int m = (l + r) / 2;
|
||||||
|
if (ar[m] >= key)
|
||||||
|
r = m;
|
||||||
|
else
|
||||||
|
l = m;
|
||||||
|
}
|
||||||
|
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int LIS(int[] array) {
|
||||||
|
int N = array.length;
|
||||||
|
if (N == 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
int[] tail = new int[N];
|
||||||
|
int length = 1; // always points empty slot in tail
|
||||||
|
|
||||||
|
tail[0] = array[0];
|
||||||
|
for (int i = 1; i < N; i++) {
|
||||||
|
|
||||||
|
// new smallest value
|
||||||
|
if (array[i] < tail[0])
|
||||||
|
tail[0] = array[i];
|
||||||
|
|
||||||
|
// array[i] extends largest subsequence
|
||||||
|
else if (array[i] > tail[length-1])
|
||||||
|
tail[length++] = array[i];
|
||||||
|
|
||||||
|
// array[i] will become end candidate of an existing subsequence or
|
||||||
|
// Throw away larger elements in all LIS, to make room for upcoming grater elements than array[i]
|
||||||
|
// (and also, array[i] would have already appeared in one of LIS, identify the location and replace it)
|
||||||
|
else
|
||||||
|
tail[upperBound(tail, -1, length-1, array[i])] = array[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return length;
|
||||||
|
}
|
||||||
|
}
|
33
Dynamic Programming/rod_cutting.java
Normal file
33
Dynamic Programming/rod_cutting.java
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
/* A Dynamic Programming solution for Rod cutting problem
|
||||||
|
Returns the best obtainable price for a rod of
|
||||||
|
length n and price[] as prices of different pieces */
|
||||||
|
|
||||||
|
public class RodCutting
|
||||||
|
{
|
||||||
|
|
||||||
|
private static int cutRod(int price[],int n)
|
||||||
|
{
|
||||||
|
int val[] = new int[n+1];
|
||||||
|
val[0] = 0;
|
||||||
|
|
||||||
|
for (int i = 1; i<=n; i++)
|
||||||
|
{
|
||||||
|
int max_val = Integer.MIN_VALUE;
|
||||||
|
for (int j = 0; j < i; j++)
|
||||||
|
max_val = Math.max(max_val,price[j] + val[i-j-1]);
|
||||||
|
|
||||||
|
val[i] = max_val;
|
||||||
|
}
|
||||||
|
|
||||||
|
return val[n];
|
||||||
|
}
|
||||||
|
|
||||||
|
//main function to test
|
||||||
|
public static void main(String args[])
|
||||||
|
{
|
||||||
|
int arr[] = new int[] {2, 5, 13, 19, 20};
|
||||||
|
int size = arr.length;
|
||||||
|
System.out.println("Maximum Obtainable Value is " +
|
||||||
|
cutRod(arr, size));
|
||||||
|
}
|
||||||
|
}
|
17
Misc/Abecedarian.java
Normal file
17
Misc/Abecedarian.java
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
//Oskar Enmalm 29/9/17
|
||||||
|
//An Abecadrian is a word where each letter is in alphabetical order
|
||||||
|
|
||||||
|
class Abecedarian{
|
||||||
|
|
||||||
|
public static boolean isAbecedarian(String s){
|
||||||
|
int index = s.length() - 1;
|
||||||
|
|
||||||
|
for(int i =0; i <index; i++){
|
||||||
|
|
||||||
|
if(s.charAt(i)<=s.charAt(i + 1)){} //Need to check if each letter for the whole word is less than the one before it
|
||||||
|
|
||||||
|
else{return false;}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
24
Misc/FibToN.java
Normal file
24
Misc/FibToN.java
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class FibToN {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//take input
|
||||||
|
Scanner scn = new Scanner(System.in);
|
||||||
|
int N = scn.nextInt();
|
||||||
|
// print fibonacci sequence less than N
|
||||||
|
int first = 0, second = 1;
|
||||||
|
//first fibo and second fibonacci are 0 and 1 respectively
|
||||||
|
|
||||||
|
while(first <= N){
|
||||||
|
//print first fibo 0 then add second fibo into it while updating second as well
|
||||||
|
|
||||||
|
System.out.println(first);
|
||||||
|
|
||||||
|
int next = first+ second;
|
||||||
|
first = second;
|
||||||
|
second = next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
15
Misc/GCD.java
Normal file
15
Misc/GCD.java
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
//Oskar Enmalm 3/10/17
|
||||||
|
//This is Euclid's algorithm which is used to find the greatest common denominator
|
||||||
|
|
||||||
|
public class GCD{
|
||||||
|
|
||||||
|
public static int gcd(int a, int b) {
|
||||||
|
|
||||||
|
int r = a % b;
|
||||||
|
while (r != 0) {
|
||||||
|
b = r;
|
||||||
|
r = b % r;
|
||||||
|
}
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
}
|
55
Misc/KMP.java
Normal file
55
Misc/KMP.java
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
|
||||||
|
/*
|
||||||
|
Implementation of Knuth–Morris–Pratt algorithm
|
||||||
|
Usage:
|
||||||
|
final String T = "AAAAABAAABA";
|
||||||
|
final String P = "AAAA";
|
||||||
|
KMPmatcher(T, P);
|
||||||
|
*/
|
||||||
|
public class KMP {
|
||||||
|
|
||||||
|
// find the starting index in string T[] that matches the search word P[]
|
||||||
|
public void KMPmatcher(final String T, final String P) {
|
||||||
|
final int m = T.length();
|
||||||
|
final int n = P.length();
|
||||||
|
final int[] pi = computePrefixFunction(P);
|
||||||
|
int q = 0;
|
||||||
|
for (int i = 0; i < m; i++) {
|
||||||
|
while (q > 0 && T.charAt(i) != P.charAt(q)) {
|
||||||
|
q = pi[q - 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (T.charAt(i) == P.charAt(q)) {
|
||||||
|
q++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (q == n) {
|
||||||
|
System.out.println("Pattern starts: " + (i + 1 - n));
|
||||||
|
q = pi[q - 1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// return the prefix function
|
||||||
|
private int[] computePrefixFunction(final String P) {
|
||||||
|
final int n = P.length();
|
||||||
|
final int[] pi = new int[n];
|
||||||
|
pi[0] = 0;
|
||||||
|
int q = 0;
|
||||||
|
for (int i = 1; i < n; i++) {
|
||||||
|
while (q > 0 && P.charAt(q) != P.charAt(i)) {
|
||||||
|
q = pi[q - 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (P.charAt(q) == P.charAt(i)) {
|
||||||
|
q++;
|
||||||
|
}
|
||||||
|
|
||||||
|
pi[i] = q;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return pi;
|
||||||
|
}
|
||||||
|
}
|
144
Misc/LowestBasePalindrome.java
Normal file
144
Misc/LowestBasePalindrome.java
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
import java.util.InputMismatchException;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class for finding the lowest base in which a given integer is a palindrome.
|
||||||
|
* Includes auxiliary methods for converting between bases and reversing strings.
|
||||||
|
*
|
||||||
|
* NOTE: There is potential for error, see note at line 63.
|
||||||
|
*
|
||||||
|
* @author RollandMichael
|
||||||
|
* @version 2017.09.28
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class LowestBasePalindrome {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner in = new Scanner(System.in);
|
||||||
|
int n=0;
|
||||||
|
while (true) {
|
||||||
|
try {
|
||||||
|
System.out.print("Enter number: ");
|
||||||
|
n = in.nextInt();
|
||||||
|
break;
|
||||||
|
} catch (InputMismatchException e) {
|
||||||
|
System.out.println("Invalid input!");
|
||||||
|
in.next();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println(n+" is a palindrome in base "+lowestBasePalindrome(n));
|
||||||
|
System.out.println(base2base(Integer.toString(n),10, lowestBasePalindrome(n)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Given a number in base 10, returns the lowest base in which the
|
||||||
|
* number is represented by a palindrome (read the same left-to-right
|
||||||
|
* and right-to-left).
|
||||||
|
* @param num A number in base 10.
|
||||||
|
* @return The lowest base in which num is a palindrome.
|
||||||
|
*/
|
||||||
|
public static int lowestBasePalindrome(int num) {
|
||||||
|
int base, num2=num;
|
||||||
|
int digit;
|
||||||
|
char digitC;
|
||||||
|
boolean foundBase=false;
|
||||||
|
String newNum = "";
|
||||||
|
String digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||||
|
|
||||||
|
while (!foundBase) {
|
||||||
|
// Try from bases 2 to num-1
|
||||||
|
for (base=2; base<num2; base++) {
|
||||||
|
newNum="";
|
||||||
|
while(num>0) {
|
||||||
|
// Obtain the first digit of n in the current base,
|
||||||
|
// which is equivalent to the integer remainder of (n/base).
|
||||||
|
// The next digit is obtained by dividing n by the base and
|
||||||
|
// continuing the process of getting the remainder. This is done
|
||||||
|
// until n is <=0 and the number in the new base is obtained.
|
||||||
|
digit = (num % base);
|
||||||
|
num/=base;
|
||||||
|
// If the digit isn't in the set of [0-9][A-Z] (beyond base 36), its character
|
||||||
|
// form is just its value in ASCII.
|
||||||
|
|
||||||
|
// NOTE: This may cause problems, as the capital letters are ASCII values
|
||||||
|
// 65-90. It may cause false positives when one digit is, for instance 10 and assigned
|
||||||
|
// 'A' from the character array and the other is 65 and also assigned 'A'.
|
||||||
|
|
||||||
|
// Regardless, the character is added to the representation of n
|
||||||
|
// in the current base.
|
||||||
|
if (digit>=digits.length()) {
|
||||||
|
digitC=(char)(digit);
|
||||||
|
newNum+=digitC;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
newNum+=digits.charAt(digit);
|
||||||
|
}
|
||||||
|
// Num is assigned back its original value for the next iteration.
|
||||||
|
num=num2;
|
||||||
|
// Auxiliary method reverses the number.
|
||||||
|
String reverse = reverse(newNum);
|
||||||
|
// If the number is read the same as its reverse, then it is a palindrome.
|
||||||
|
// The current base is returned.
|
||||||
|
if (reverse.equals(newNum)) {
|
||||||
|
foundBase=true;
|
||||||
|
return base;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// If all else fails, n is always a palindrome in base n-1. ("11")
|
||||||
|
return num-1;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String reverse(String str) {
|
||||||
|
String reverse = "";
|
||||||
|
for(int i=str.length()-1; i>=0; i--) {
|
||||||
|
reverse += str.charAt(i);
|
||||||
|
}
|
||||||
|
return reverse;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String base2base(String n, int b1, int b2) {
|
||||||
|
// Declare variables: decimal value of n,
|
||||||
|
// character of base b1, character of base b2,
|
||||||
|
// and the string that will be returned.
|
||||||
|
int decimalValue = 0, charB2;
|
||||||
|
char charB1;
|
||||||
|
String output="";
|
||||||
|
// Go through every character of n
|
||||||
|
for (int i=0; i<n.length(); i++) {
|
||||||
|
// store the character in charB1
|
||||||
|
charB1 = n.charAt(i);
|
||||||
|
// if it is a non-number, convert it to a decimal value >9 and store it in charB2
|
||||||
|
if (charB1 >= 'A' && charB1 <= 'Z')
|
||||||
|
charB2 = 10 + (charB1 - 'A');
|
||||||
|
// Else, store the integer value in charB2
|
||||||
|
else
|
||||||
|
charB2 = charB1 - '0';
|
||||||
|
// Convert the digit to decimal and add it to the
|
||||||
|
// decimalValue of n
|
||||||
|
decimalValue = decimalValue * b1 + charB2;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Converting the decimal value to base b2:
|
||||||
|
// A number is converted from decimal to another base
|
||||||
|
// by continuously dividing by the base and recording
|
||||||
|
// the remainder until the quotient is zero. The number in the
|
||||||
|
// new base is the remainders, with the last remainder
|
||||||
|
// being the left-most digit.
|
||||||
|
|
||||||
|
// While the quotient is NOT zero:
|
||||||
|
while (decimalValue != 0) {
|
||||||
|
// If the remainder is a digit < 10, simply add it to
|
||||||
|
// the left side of the new number.
|
||||||
|
if (decimalValue % b2 < 10)
|
||||||
|
output = Integer.toString(decimalValue % b2) + output;
|
||||||
|
// If the remainder is >= 10, add a character with the
|
||||||
|
// corresponding value to the new number. (A = 10, B = 11, C = 12, ...)
|
||||||
|
else
|
||||||
|
output = (char)((decimalValue % b2)+55) + output;
|
||||||
|
// Divide by the new base again
|
||||||
|
decimalValue /= b2;
|
||||||
|
}
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
}
|
59
Misc/ReturnSubsequence.java
Normal file
59
Misc/ReturnSubsequence.java
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
/*
|
||||||
|
This program will return all the subsequences of the input string in a string array;
|
||||||
|
Sample Input:
|
||||||
|
abc
|
||||||
|
Sample Output:
|
||||||
|
"" ( Empty String )
|
||||||
|
c
|
||||||
|
b
|
||||||
|
bc
|
||||||
|
a
|
||||||
|
ac
|
||||||
|
ab
|
||||||
|
abc
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class ReturnSubsequence {
|
||||||
|
/*
|
||||||
|
Main function will accept the given string and implement return subsequences function
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println("Enter String: ");
|
||||||
|
Scanner s=new Scanner(System.in);
|
||||||
|
String givenString=s.next(); //given string
|
||||||
|
String[] subsequence=returnSubsequence(givenString); //calling returnSubsequence() function
|
||||||
|
System.out.println("Subsequences : ");
|
||||||
|
for(int i=0;i<subsequence.length;i++) //print the given array of subsequences
|
||||||
|
{
|
||||||
|
System.out.println(subsequence[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
Recursive function to return Subsequences
|
||||||
|
*/
|
||||||
|
private static String[] returnSubsequence(String givenString) {
|
||||||
|
if(givenString.length()==0) // If string is empty we will create an array of size=1 and insert "" (Empty string) in it
|
||||||
|
{
|
||||||
|
String[] ans=new String[1];
|
||||||
|
ans[0]="";
|
||||||
|
return ans;
|
||||||
|
|
||||||
|
}
|
||||||
|
String[] SmallAns=returnSubsequence(givenString.substring(1)); //recursive call to get subsequences of substring starting from index position=1
|
||||||
|
|
||||||
|
String[] ans=new String[2*SmallAns.length];// Our answer will be an array off string of size=2*SmallAns
|
||||||
|
int i=0;
|
||||||
|
for (;i<SmallAns.length;i++)
|
||||||
|
{
|
||||||
|
ans[i]=SmallAns[i]; //Copying all the strings present in SmallAns to ans string array
|
||||||
|
}
|
||||||
|
for (int k=0;k<SmallAns.length;k++)
|
||||||
|
{
|
||||||
|
ans[k+SmallAns.length]=givenString.charAt(0)+SmallAns[k]; // Insert character at index=0 of the given substring in front of every string in SmallAns
|
||||||
|
}
|
||||||
|
return ans;
|
||||||
|
}
|
||||||
|
}
|
74
Misc/ReverseStackUsingRecursion.java
Normal file
74
Misc/ReverseStackUsingRecursion.java
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
/* Program to reverse a Stack using Recursion*/
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.Stack;
|
||||||
|
|
||||||
|
public class ReverseStackUsingRecursion {
|
||||||
|
|
||||||
|
//Stack
|
||||||
|
private static Stack<Integer> stack=new Stack<>();
|
||||||
|
|
||||||
|
//Main function
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//To Create a Dummy Stack containing integers from 0-9
|
||||||
|
for(int i=0;i<10;i++)
|
||||||
|
{
|
||||||
|
stack.push(i);
|
||||||
|
}
|
||||||
|
System.out.println("STACK");
|
||||||
|
|
||||||
|
//To print that dummy Stack
|
||||||
|
for(int k=9;k>=0;k--)
|
||||||
|
{
|
||||||
|
System.out.println(k);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Reverse Function called
|
||||||
|
reverseUsingRecursion(stack);
|
||||||
|
|
||||||
|
System.out.println("REVERSED STACK : ");
|
||||||
|
//To print reversed stack
|
||||||
|
while (!stack.isEmpty())
|
||||||
|
{
|
||||||
|
System.out.println(stack.pop());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//Function Used to reverse Stack Using Recursion
|
||||||
|
private static void reverseUsingRecursion(Stack<Integer> stack) {
|
||||||
|
if(stack.isEmpty()) // If stack is empty then return
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
/* All items are stored in call stack until we reach the end*/
|
||||||
|
|
||||||
|
int temptop=stack.peek();
|
||||||
|
stack.pop();
|
||||||
|
reverseUsingRecursion(stack); //Recursion call
|
||||||
|
insertAtEnd(temptop); // Insert items held in call stack one by one into stack
|
||||||
|
}
|
||||||
|
|
||||||
|
//Function used to insert element at the end of stack
|
||||||
|
private static void insertAtEnd(int temptop) {
|
||||||
|
if(stack.isEmpty())
|
||||||
|
{
|
||||||
|
stack.push(temptop); // If stack is empty push the element
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
int temp = stack.peek(); /* All the items are stored in call stack until we reach end*/
|
||||||
|
stack.pop();
|
||||||
|
|
||||||
|
insertAtEnd(temptop); //Recursive call
|
||||||
|
|
||||||
|
stack.push(temp);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
38
Misc/StackPostfixNotation.java
Normal file
38
Misc/StackPostfixNotation.java
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class Postfix {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
String post = scanner.nextLine(); // Takes input with spaces in between eg. "1 21 +"
|
||||||
|
System.out.println(postfixEvaluate(post));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Evaluates the given postfix expression string and returns the result.
|
||||||
|
public static int postfixEvaluate(String exp) {
|
||||||
|
Stack<Integer> s = new Stack<Integer> ();
|
||||||
|
Scanner tokens = new Scanner(exp);
|
||||||
|
|
||||||
|
while (tokens.hasNext()) {
|
||||||
|
if (tokens.hasNextInt()) {
|
||||||
|
s.push(tokens.nextInt()); // If int then push to stack
|
||||||
|
} else { // else pop top two values and perform the operation
|
||||||
|
int num2 = s.pop();
|
||||||
|
int num1 = s.pop();
|
||||||
|
String op = tokens.next();
|
||||||
|
|
||||||
|
if (op.equals("+")) {
|
||||||
|
s.push(num1 + num2);
|
||||||
|
} else if (op.equals("-")) {
|
||||||
|
s.push(num1 - num2);
|
||||||
|
} else if (op.equals("*")) {
|
||||||
|
s.push(num1 * num2);
|
||||||
|
} else {
|
||||||
|
s.push(num1 / num2);
|
||||||
|
}
|
||||||
|
|
||||||
|
// "+", "-", "*", "/"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return s.pop();
|
||||||
|
}
|
||||||
|
}
|
26
Misc/TowerOfHanoiUsingRecursion.java
Normal file
26
Misc/TowerOfHanoiUsingRecursion.java
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
class TowerOfHanoi
|
||||||
|
{
|
||||||
|
public static void shift(int n, String startPole, String intermediatePole, String endPole)
|
||||||
|
{
|
||||||
|
if (n == 0) // if n becomes zero the program returns thus ending the loop.
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Shift function is called in recursion for swapping the n-1 disc from the startPole to the intermediatePole
|
||||||
|
shift(n - 1, startPole, endPole, intermediatePole);
|
||||||
|
System.out.println("\nMove \"" + n + "\" from " + startPole + " --> " + endPole); // Result Printing
|
||||||
|
// Shift function is called in recursion for swapping the n-1 disc from the intermediatePole to the endPole
|
||||||
|
shift(n - 1, intermediatePole, startPole, endPole);
|
||||||
|
}
|
||||||
|
public static void main(String[] args)
|
||||||
|
{
|
||||||
|
System.out.print("Enter number of discs on Pole 1: ");
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
int numberOfDiscs = scanner.nextInt(); //input of number of discs on pole 1
|
||||||
|
shift(numberOfDiscs, "Pole1", "Pole2", "Pole3"); //Shift function called
|
||||||
|
}
|
||||||
|
}
|
27
Misc/crc32.java
Normal file
27
Misc/crc32.java
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
import java.util.BitSet;
|
||||||
|
|
||||||
|
//Generates a crc32 checksum for a given string or byte array
|
||||||
|
public class crc32 {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println(Integer.toHexString(crc32("Hello World")));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int crc32(String str) {
|
||||||
|
return crc32(str.getBytes());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int crc32(byte[] data) {
|
||||||
|
BitSet bitSet = BitSet.valueOf(data);
|
||||||
|
int crc32 = 0xFFFFFFFF; //initial value
|
||||||
|
for(int i=0;i<data.length*8;i++) {
|
||||||
|
if(((crc32>>>31)&1) != (bitSet.get(i)?1:0))
|
||||||
|
crc32 = (crc32 << 1) ^ 0x04C11DB7; //xoring with polynomial
|
||||||
|
else
|
||||||
|
crc32 = (crc32 << 1);
|
||||||
|
}
|
||||||
|
crc32 = Integer.reverse(crc32); //result reflect
|
||||||
|
return crc32 ^ 0xFFFFFFFF; //final xor value
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
33
Misc/root_precision.java
Normal file
33
Misc/root_precision.java
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
import java.io.*;
|
||||||
|
import java.util.*;
|
||||||
|
import java.text.*;
|
||||||
|
import java.math.*;
|
||||||
|
import java.util.regex.*;
|
||||||
|
|
||||||
|
public class Solution {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//take input
|
||||||
|
Scanner scn = new Scanner(System.in);
|
||||||
|
|
||||||
|
int N = scn.nextInt(); //N is the input number
|
||||||
|
int P = scn.nextInt(); //P is precision value for eg - P is 3 in 2.564 and 5 in 3.80870.
|
||||||
|
|
||||||
|
System.out.println(squareRoot(N, P));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double squareRoot(int N, int P) {
|
||||||
|
double rv = 0; //rv means return value
|
||||||
|
|
||||||
|
double root = Math.pow(N, 0.5);
|
||||||
|
|
||||||
|
//calculate precision to power of 10 and then multiply it with root value.
|
||||||
|
int precision = (int) Math.pow(10, P);
|
||||||
|
root = root * precision;
|
||||||
|
/*typecast it into integer then divide by precision and again typecast into double
|
||||||
|
so as to have decimal points upto P precision */
|
||||||
|
|
||||||
|
rv = (int)root;
|
||||||
|
return (double)rv/precision;
|
||||||
|
}
|
||||||
|
}
|
49
Others/SieveOfEratosthenes.java
Normal file
49
Others/SieveOfEratosthenes.java
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Varun Upadhyay (https://github.com/varunu28)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class SieveOfEratosthenes {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method implements the Sieve of Eratosthenes Algorithm
|
||||||
|
*
|
||||||
|
* @param n The number till which we have to check for prime
|
||||||
|
* Prints all the prime numbers till n
|
||||||
|
**/
|
||||||
|
|
||||||
|
public static void findPrimesTillN(int n) {
|
||||||
|
int[] arr = new int[n+1];
|
||||||
|
|
||||||
|
for (int i=0;i<=n;i++) {
|
||||||
|
arr[i] = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
arr[0] = arr[1] = 0;
|
||||||
|
|
||||||
|
for (int i=2;i<=Math.sqrt(n);i++) {
|
||||||
|
if (arr[i] == 1) {
|
||||||
|
for (int j=2;i*j <= n;j++) {
|
||||||
|
arr[i*j] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i=0;i<n+1;i++) {
|
||||||
|
if (arr[i] == 1) {
|
||||||
|
System.out.print(i + " ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Driver Program
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int n = 100;
|
||||||
|
|
||||||
|
// Prints 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
|
||||||
|
findPrimesTillN(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -23,7 +23,7 @@ class BinarySearch
|
|||||||
if ( lb > ub)
|
if ( lb > ub)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
int mid = (ub+lb)/2;
|
int mid = (ub+lb) >>> 1;
|
||||||
int comp = key.compareTo(array[mid]);
|
int comp = key.compareTo(array[mid]);
|
||||||
|
|
||||||
if (comp < 0)
|
if (comp < 0)
|
||||||
|
53
Searches/interpolationSearch.java
Normal file
53
Searches/interpolationSearch.java
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
|
||||||
|
class Test
|
||||||
|
{
|
||||||
|
// Array of items on which search will
|
||||||
|
// be conducted.
|
||||||
|
static int arr[] = new int[]{10, 12, 13, 16, 18, 19, 20, 21, 22, 23,
|
||||||
|
24, 33, 35, 42, 47};
|
||||||
|
|
||||||
|
// If x is present in arr[0..n-1], then returns
|
||||||
|
// index of it, else returns -1.
|
||||||
|
static int interpolationSearch(int x)
|
||||||
|
{
|
||||||
|
// Find indexes of two corners
|
||||||
|
int lo = 0, hi = (arr.length - 1);
|
||||||
|
|
||||||
|
// Since array is sorted, an element present
|
||||||
|
// in array must be in range defined by corner
|
||||||
|
while (lo <= hi && x >= arr[lo] && x <= arr[hi])
|
||||||
|
{
|
||||||
|
// Probing the position with keeping
|
||||||
|
// uniform distribution in mind.
|
||||||
|
int pos = lo + (((hi-lo) /
|
||||||
|
(arr[hi]-arr[lo]))*(x - arr[lo]));
|
||||||
|
|
||||||
|
// Condition of target found
|
||||||
|
if (arr[pos] == x)
|
||||||
|
return pos;
|
||||||
|
|
||||||
|
// If x is larger, x is in upper part
|
||||||
|
if (arr[pos] < x)
|
||||||
|
lo = pos + 1;
|
||||||
|
|
||||||
|
// If x is smaller, x is in lower part
|
||||||
|
else
|
||||||
|
hi = pos - 1;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Driver method
|
||||||
|
public static void main(String[] args)
|
||||||
|
{
|
||||||
|
int x = 18; // Element to be searched
|
||||||
|
int index = interpolationSearch(x);
|
||||||
|
|
||||||
|
// If element was found
|
||||||
|
if (index != -1)
|
||||||
|
System.out.println("Element found at index " + index);
|
||||||
|
else
|
||||||
|
System.out.println("Element not found.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
117
ciphers/Caesar.java
Normal file
117
ciphers/Caesar.java
Normal file
@ -0,0 +1,117 @@
|
|||||||
|
/**
|
||||||
|
Author : FAHRI YARDIMCI
|
||||||
|
|
||||||
|
A Java implementation of Caesar Cipher.
|
||||||
|
/It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. /
|
||||||
|
**/
|
||||||
|
import java.util.Scanner;
|
||||||
|
public class Caesar {
|
||||||
|
public static String encode (String message,int shift)
|
||||||
|
{
|
||||||
|
String encoded = "";
|
||||||
|
for(int i = 0 ; i<message.length() ;i++)
|
||||||
|
{
|
||||||
|
int current = message.charAt(i); //using char to shift characters because ascii is in-order latin alphabet
|
||||||
|
if(current==32)
|
||||||
|
{
|
||||||
|
encoded += " ";
|
||||||
|
continue;
|
||||||
|
|
||||||
|
}
|
||||||
|
else if (current>=65 && current<= 90)
|
||||||
|
{
|
||||||
|
int numAlphabet = message.charAt(i);
|
||||||
|
if(shift + numAlphabet > 90)
|
||||||
|
{
|
||||||
|
int j = 90 - numAlphabet;
|
||||||
|
char nextKey = (char)(65 + (shift - j - 1));
|
||||||
|
encoded += nextKey;
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
char nextKey = (char)(current + shift);
|
||||||
|
encoded += nextKey;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (current>=97 && current <= 122)
|
||||||
|
{
|
||||||
|
int numAlphabet = message.charAt(i);
|
||||||
|
if(shift + numAlphabet > 122)
|
||||||
|
{
|
||||||
|
int j = 122 - numAlphabet;
|
||||||
|
char nextKey = (char)(97 + (shift - j - 1));
|
||||||
|
encoded += nextKey;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
char nextKey = (char)(current + shift);
|
||||||
|
encoded += nextKey;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return encoded;
|
||||||
|
}
|
||||||
|
public static String decode (String message,int shift)
|
||||||
|
{
|
||||||
|
String decoded = "";
|
||||||
|
for(int i = 0 ; i<message.length() ;i++)
|
||||||
|
{
|
||||||
|
int current = message.charAt(i);
|
||||||
|
if(current==32)
|
||||||
|
{
|
||||||
|
decoded += " ";
|
||||||
|
continue;
|
||||||
|
|
||||||
|
}
|
||||||
|
else if (current>=65 && current<= 90)
|
||||||
|
{
|
||||||
|
int numAlphabet = message.charAt(i);
|
||||||
|
if(numAlphabet - shift < 65)
|
||||||
|
{
|
||||||
|
int j = numAlphabet - 65;
|
||||||
|
char nextKey = (char)(90 - (shift - j - 1));
|
||||||
|
decoded += nextKey;
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
char nextKey = (char)(current - shift);
|
||||||
|
decoded += nextKey;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (current>=97 && current <= 122)
|
||||||
|
{
|
||||||
|
int numAlphabet = message.charAt(i);
|
||||||
|
if(numAlphabet - shift < 97)
|
||||||
|
{
|
||||||
|
int j = numAlphabet - 97;
|
||||||
|
char nextKey = (char)(122 - (shift - j - 1));
|
||||||
|
decoded += nextKey;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
char nextKey = (char)(current - shift);
|
||||||
|
decoded += nextKey;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return decoded;
|
||||||
|
}
|
||||||
|
public static void main(String[] args)
|
||||||
|
{
|
||||||
|
Scanner input = new Scanner(System.in);
|
||||||
|
System.out.println("Please enter the message (Latin Alphabet)");
|
||||||
|
String message = input.nextLine();
|
||||||
|
System.out.println(message);
|
||||||
|
System.out.println("Please enter the shift number");
|
||||||
|
int shift = input.nextInt() % 26;
|
||||||
|
System.out.println("(E)ncode or (D)ecode ?");
|
||||||
|
char choice = input.next().charAt(0);
|
||||||
|
if(choice == 'E' || choice=='e')
|
||||||
|
System.out.println("ENCODED MESSAGE IS \n" + encode(message,shift)); //send our function to handle
|
||||||
|
if(choice =='D' || choice =='d')
|
||||||
|
System.out.println("DECODED MESSAGE IS \n" + decode(message,shift));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
169
ciphers/ColumnarTranspositionCipher.java
Normal file
169
ciphers/ColumnarTranspositionCipher.java
Normal file
@ -0,0 +1,169 @@
|
|||||||
|
/**
|
||||||
|
* Columnar Transposition Cipher Encryption and Decryption.
|
||||||
|
* @author <a href="https://github.com/freitzzz">freitzzz</a>
|
||||||
|
*/
|
||||||
|
public class ColumnarTranspositionCipher {
|
||||||
|
private static String keyword;
|
||||||
|
private static Object[][] table;
|
||||||
|
private static String abecedarium;
|
||||||
|
public static final String ABECEDARIUM="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.;:-@";
|
||||||
|
private static final String ENCRYPTION_FIELD="≈";
|
||||||
|
private static final char ENCRYPTION_FIELD_CHAR='≈';
|
||||||
|
/**
|
||||||
|
* Encrypts a certain String with the Columnar Transposition Cipher Rule
|
||||||
|
* @param word Word being encrypted
|
||||||
|
* @param keyword String with keyword being used
|
||||||
|
* @return a String with the word encrypted by the Columnar Transposition Cipher Rule
|
||||||
|
*/
|
||||||
|
public static String encrpyter(String word,String keyword){
|
||||||
|
ColumnarTranspositionCipher.keyword=keyword;
|
||||||
|
abecedariumBuilder(500);
|
||||||
|
table=tableBuilder(word);
|
||||||
|
Object[][] sortedTable=sortTable(table);
|
||||||
|
String wordEncrypted="";
|
||||||
|
for(int i=0;i<sortedTable[0].length;i++){
|
||||||
|
for(int j=1;j<sortedTable.length;j++){
|
||||||
|
wordEncrypted+=sortedTable[j][i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return wordEncrypted;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encrypts a certain String with the Columnar Transposition Cipher Rule
|
||||||
|
* @param word Word being encrypted
|
||||||
|
* @param keyword String with keyword being used
|
||||||
|
* @param abecedarium String with the abecedarium being used. null for default one
|
||||||
|
* @return a String with the word encrypted by the Columnar Transposition Cipher Rule
|
||||||
|
*/
|
||||||
|
public static String encrpyter(String word,String keyword,String abecedarium){
|
||||||
|
ColumnarTranspositionCipher.keyword=keyword;
|
||||||
|
if(abecedarium!=null){
|
||||||
|
ColumnarTranspositionCipher.abecedarium=abecedarium;
|
||||||
|
}else{
|
||||||
|
ColumnarTranspositionCipher.abecedarium=ABECEDARIUM;
|
||||||
|
}
|
||||||
|
table=tableBuilder(word);
|
||||||
|
Object[][] sortedTable=sortTable(table);
|
||||||
|
String wordEncrypted="";
|
||||||
|
for(int i=0;i<sortedTable[0].length;i++){
|
||||||
|
for(int j=1;j<sortedTable.length;j++){
|
||||||
|
wordEncrypted+=sortedTable[j][i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return wordEncrypted;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Decryps a certain encrypted String with the Columnar Transposition Cipher Rule
|
||||||
|
* @return a String decrypted with the word ecrypted by the Columnar Transpositiion Cipher Rule
|
||||||
|
*/
|
||||||
|
public static String decrypter(){
|
||||||
|
String wordDecrypted="";
|
||||||
|
for(int i=1;i<table.length;i++){
|
||||||
|
for(int j=0;j<table[i].length;j++){
|
||||||
|
wordDecrypted+=table[i][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return wordDecrypted.replaceAll(ENCRYPTION_FIELD,"");
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Builds a table with the word to be encrpyted in rows by the Columnar Transposition Cipher Rule
|
||||||
|
* @return An Object[][] with the word to be encrypted filled in rows and columns
|
||||||
|
*/
|
||||||
|
private static Object[][] tableBuilder(String word){
|
||||||
|
Object[][] table=new Object[rows(word)+1][keyword.length()];
|
||||||
|
char[] wordInChards=word.toCharArray();
|
||||||
|
//Fils in the respective numbers
|
||||||
|
table[0]=findElements();
|
||||||
|
int charElement=0;
|
||||||
|
for(int i=1;i<table.length;i++){
|
||||||
|
for(int j=0;j<table[i].length;j++){
|
||||||
|
if(charElement<wordInChards.length){
|
||||||
|
table[i][j]=(char)wordInChards[charElement];
|
||||||
|
charElement++;
|
||||||
|
}else{
|
||||||
|
table[i][j]=ENCRYPTION_FIELD_CHAR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return table;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Determines the number of rows the table should have regarding the Columnar Transposition Cipher Rule
|
||||||
|
* @return an int with the number of rows that the table should have in order to respect the Columnar Transposition Cipher Rule.
|
||||||
|
*/
|
||||||
|
private static int rows(String word){
|
||||||
|
if((double)word.length()/keyword.length()>word.length()/keyword.length()){
|
||||||
|
return (word.length()/keyword.length())+1;
|
||||||
|
}else{
|
||||||
|
return word.length()/keyword.length();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private static Object[] findElements(){
|
||||||
|
Object[] charValues=new Object[keyword.length()];
|
||||||
|
for(int i=0;i<charValues.length;i++){
|
||||||
|
for(int j=0;j<abecedarium.length();j++){
|
||||||
|
if(keyword.charAt(i)==abecedarium.charAt(j))charValues[i]=j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return charValues;
|
||||||
|
}
|
||||||
|
private static Object[][] sortTable(Object[][] table){
|
||||||
|
Object[][] tableSorted=new Object[table.length][table[0].length];
|
||||||
|
for(int i=0;i<tableSorted.length;i++){
|
||||||
|
for(int j=0;j<tableSorted[i].length;j++){
|
||||||
|
tableSorted[i][j]=table[i][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for(int i=0;i<tableSorted[0].length;i++){
|
||||||
|
for(int j=i+1;j<tableSorted[0].length;j++){
|
||||||
|
if((int)tableSorted[0][i]>(int)table[0][j]){
|
||||||
|
Object[] column=getColumn(tableSorted,tableSorted.length,i);
|
||||||
|
switchColumns(tableSorted,j,i,column);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return tableSorted;
|
||||||
|
}
|
||||||
|
private static Object[] getColumn(Object[][] table,int rows,int column){
|
||||||
|
Object[] columnArray=new Object[rows];
|
||||||
|
for(int i=0;i<rows;i++){
|
||||||
|
columnArray[i]=table[i][column];
|
||||||
|
}
|
||||||
|
return columnArray;
|
||||||
|
}
|
||||||
|
private static void switchColumns(Object[][] table, int firstColumnIndex,int secondColumnIndex,Object[] columnToSwitch){
|
||||||
|
for(int i=0;i<table.length;i++){
|
||||||
|
table[i][secondColumnIndex]=table[i][firstColumnIndex];
|
||||||
|
table[i][firstColumnIndex]=columnToSwitch[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Creates an abecedarium with a specified ascii inded
|
||||||
|
* @param value Number of characters being used based on the ASCII Table
|
||||||
|
*/
|
||||||
|
private static void abecedariumBuilder(int value){
|
||||||
|
abecedarium="";
|
||||||
|
for(int i=0;i<value;i++){
|
||||||
|
abecedarium+=(char)i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private static void showTable(){
|
||||||
|
for(int i=0;i<table.length;i++){
|
||||||
|
for(int j=0;j<table[i].length;j++){
|
||||||
|
System.out.print(table[i][j]+" ");
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static void main(String[] args){
|
||||||
|
String keywordForExample="asd215";
|
||||||
|
String wordBeingEncrypted="This is a test of the Columnar Transposition Cipher";
|
||||||
|
System.out.println("### Example of Columnar Transposition Cipher ###\n");
|
||||||
|
System.out.println("Word being encryped ->>> "+wordBeingEncrypted);
|
||||||
|
System.out.println("Word encrypted ->>> "+ColumnarTranspositionCipher.encrpyter(wordBeingEncrypted,keywordForExample));
|
||||||
|
System.out.println("Word decryped ->>> "+ColumnarTranspositionCipher.decrypter());
|
||||||
|
System.out.println("\n### Encrypted Table ###");
|
||||||
|
showTable();
|
||||||
|
}
|
||||||
|
}
|
@ -1,20 +1,20 @@
|
|||||||
/**
|
/**
|
||||||
* This entire class is used to build a Binary Tree data structure.
|
* This entire class is used to build a Binary Tree data structure.
|
||||||
* There is the Node Class and the Tree Class, both explained below.
|
* There is the Node Class and the Tree Class, both explained below.
|
||||||
*
|
*
|
||||||
* @author Unknown
|
* @author Unknown
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class implements the nodes that will go on the Binary Tree.
|
* This class implements the nodes that will go on the Binary Tree.
|
||||||
* They consist of the data in them, the node to the left, the node
|
* They consist of the data in them, the node to the left, the node
|
||||||
* to the right, and the parent from which they came from.
|
* to the right, and the parent from which they came from.
|
||||||
*
|
*
|
||||||
* @author Unknown
|
* @author Unknown
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
class Node{
|
class Node{
|
||||||
/** Data for the node */
|
/** Data for the node */
|
||||||
public int data;
|
public int data;
|
||||||
@ -26,10 +26,10 @@ class Node{
|
|||||||
public Node parent;
|
public Node parent;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor of Node
|
* Constructor of Node
|
||||||
*
|
*
|
||||||
* @param value Value to put in the node
|
* @param value Value to put in the node
|
||||||
*/
|
*/
|
||||||
public Node(int value){
|
public Node(int value){
|
||||||
data = value;
|
data = value;
|
||||||
left = null;
|
left = null;
|
||||||
@ -40,56 +40,54 @@ class Node{
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A binary tree is a data structure in which an element
|
* A binary tree is a data structure in which an element
|
||||||
* has two successors(children). The left child is usually
|
* has two successors(children). The left child is usually
|
||||||
* smaller than the parent, and the right child is usually
|
* smaller than the parent, and the right child is usually
|
||||||
* bigger.
|
* bigger.
|
||||||
*
|
*
|
||||||
* @author Unknown
|
* @author Unknown
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
class Tree{
|
class Tree{
|
||||||
/** The root of the Binary Tree */
|
/** The root of the Binary Tree */
|
||||||
private Node root;
|
private Node root;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
*/
|
*/
|
||||||
public Tree(){
|
public Tree(){
|
||||||
root = null;
|
root = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method to find a Node with a certain value
|
* Method to find a Node with a certain value
|
||||||
*
|
*
|
||||||
* @param key Value being looked for
|
* @param key Value being looked for
|
||||||
* @return The node if it finds it, otherwise returns the parent
|
* @return The node if it finds it, otherwise returns the parent
|
||||||
*/
|
*/
|
||||||
public Node find(int key){
|
public Node find(int key) {
|
||||||
Node current = root;
|
Node current = root;
|
||||||
Node last = root;
|
while (current != null) {
|
||||||
while(current != null){
|
if(key < current.data) {
|
||||||
last = current;
|
|
||||||
if(key < current.data)
|
|
||||||
current = current.left;
|
current = current.left;
|
||||||
else if(key > current.data)
|
} else if(key > current.data) {
|
||||||
current = current.right;
|
current = current.right;
|
||||||
//If you find the value return it
|
} else { // If you find the value return it
|
||||||
else
|
|
||||||
return current;
|
return current;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return last;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Inserts certain value into the Binary Tree
|
* Inserts certain value into the Binary Tree
|
||||||
*
|
*
|
||||||
* @param value Value to be inserted
|
* @param value Value to be inserted
|
||||||
*/
|
*/
|
||||||
public void put(int value){
|
public void put(int value){
|
||||||
Node newNode = new Node(value);
|
Node newNode = new Node(value);
|
||||||
if(root == null)
|
if(root == null)
|
||||||
root = newNode;
|
root = newNode;
|
||||||
else{
|
else{
|
||||||
//This will return the soon to be parent of the value you're inserting
|
//This will return the soon to be parent of the value you're inserting
|
||||||
Node parent = find(value);
|
Node parent = find(value);
|
||||||
@ -109,29 +107,29 @@ class Tree{
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deletes a given value from the Binary Tree
|
* Deletes a given value from the Binary Tree
|
||||||
*
|
*
|
||||||
* @param value Value to be deleted
|
* @param value Value to be deleted
|
||||||
* @return If the value was deleted
|
* @return If the value was deleted
|
||||||
*/
|
*/
|
||||||
public boolean remove(int value){
|
public boolean remove(int value){
|
||||||
//temp is the node to be deleted
|
//temp is the node to be deleted
|
||||||
Node temp = find(value);
|
Node temp = find(value);
|
||||||
|
|
||||||
//If the value doesn't exist
|
//If the value doesn't exist
|
||||||
if(temp.data != value)
|
if(temp.data != value)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
//No children
|
//No children
|
||||||
if(temp.right == null && temp.left == null){
|
if(temp.right == null && temp.left == null){
|
||||||
if(temp == root)
|
if(temp == root)
|
||||||
root = null;
|
root = null;
|
||||||
|
|
||||||
//This if/else assigns the new node to be either the left or right child of the parent
|
//This if/else assigns the new node to be either the left or right child of the parent
|
||||||
else if(temp.parent.data < temp.data)
|
else if(temp.parent.data < temp.data)
|
||||||
temp.parent.right = null;
|
temp.parent.right = null;
|
||||||
else
|
else
|
||||||
temp.parent.left = null;
|
temp.parent.left = null;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -162,9 +160,9 @@ class Tree{
|
|||||||
|
|
||||||
//This if/else assigns the new node to be either the left or right child of the parent
|
//This if/else assigns the new node to be either the left or right child of the parent
|
||||||
if(temp.parent.data < temp.data)
|
if(temp.parent.data < temp.data)
|
||||||
temp.parent.right = successor;
|
temp.parent.right = successor;
|
||||||
else
|
else
|
||||||
temp.parent.left = successor;
|
temp.parent.left = successor;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -175,96 +173,96 @@ class Tree{
|
|||||||
if(temp == root){
|
if(temp == root){
|
||||||
root = temp.right; return true;}
|
root = temp.right; return true;}
|
||||||
|
|
||||||
temp.right.parent = temp.parent;
|
temp.right.parent = temp.parent;
|
||||||
|
|
||||||
//Assigns temp to left or right child
|
//Assigns temp to left or right child
|
||||||
if(temp.data < temp.parent.data)
|
if(temp.data < temp.parent.data)
|
||||||
temp.parent.left = temp.right;
|
temp.parent.left = temp.right;
|
||||||
else
|
else
|
||||||
temp.parent.right = temp.right;
|
temp.parent.right = temp.right;
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
|
//If it has a left child
|
||||||
|
else{
|
||||||
|
if(temp == root){
|
||||||
|
root = temp.left; return true;}
|
||||||
|
|
||||||
|
temp.left.parent = temp.parent;
|
||||||
|
|
||||||
|
//Assigns temp to left or right side
|
||||||
|
if(temp.data < temp.parent.data)
|
||||||
|
temp.parent.left = temp.left;
|
||||||
|
else
|
||||||
|
temp.parent.right = temp.left;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
//If it has a left child
|
|
||||||
else{
|
|
||||||
if(temp == root){
|
|
||||||
root = temp.left; return true;}
|
|
||||||
|
|
||||||
temp.left.parent = temp.parent;
|
/**
|
||||||
|
* This method finds the Successor to the Node given.
|
||||||
|
* Move right once and go left down the tree as far as you can
|
||||||
|
*
|
||||||
|
* @param n Node that you want to find the Successor of
|
||||||
|
* @return The Successor of the node
|
||||||
|
*/
|
||||||
|
public Node findSuccessor(Node n){
|
||||||
|
if(n.right == null)
|
||||||
|
return n;
|
||||||
|
Node current = n.right;
|
||||||
|
Node parent = n.right;
|
||||||
|
while(current != null){
|
||||||
|
parent = current;
|
||||||
|
current = current.left;
|
||||||
|
}
|
||||||
|
return parent;
|
||||||
|
}
|
||||||
|
|
||||||
//Assigns temp to left or right side
|
/**
|
||||||
if(temp.data < temp.parent.data)
|
* Returns the root of the Binary Tree
|
||||||
temp.parent.left = temp.left;
|
*
|
||||||
else
|
* @return the root of the Binary Tree
|
||||||
temp.parent.right = temp.left;
|
*/
|
||||||
return true;
|
public Node getRoot(){
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prints leftChild - root - rightChild
|
||||||
|
*
|
||||||
|
* @param localRoot The local root of the binary tree
|
||||||
|
*/
|
||||||
|
public void inOrder(Node localRoot){
|
||||||
|
if(localRoot != null){
|
||||||
|
inOrder(localRoot.left);
|
||||||
|
System.out.print(localRoot.data + " ");
|
||||||
|
inOrder(localRoot.right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prints root - leftChild - rightChild
|
||||||
|
*
|
||||||
|
* @param localRoot The local root of the binary tree
|
||||||
|
*/
|
||||||
|
public void preOrder(Node localRoot){
|
||||||
|
if(localRoot != null){
|
||||||
|
System.out.print(localRoot.data + " ");
|
||||||
|
preOrder(localRoot.left);
|
||||||
|
preOrder(localRoot.right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prints rightChild - leftChild - root
|
||||||
|
*
|
||||||
|
* @param localRoot The local root of the binary tree
|
||||||
|
*/
|
||||||
|
public void postOrder(Node localRoot){
|
||||||
|
if(localRoot != null){
|
||||||
|
postOrder(localRoot.left);
|
||||||
|
postOrder(localRoot.right);
|
||||||
|
System.out.print(localRoot.data + " ");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method finds the Successor to the Node given.
|
|
||||||
* Move right once and go left down the tree as far as you can
|
|
||||||
*
|
|
||||||
* @param n Node that you want to find the Successor of
|
|
||||||
* @return The Successor of the node
|
|
||||||
*/
|
|
||||||
public Node findSuccessor(Node n){
|
|
||||||
if(n.right == null)
|
|
||||||
return n;
|
|
||||||
Node current = n.right;
|
|
||||||
Node parent = n.right;
|
|
||||||
while(current != null){
|
|
||||||
parent = current;
|
|
||||||
current = current.left;
|
|
||||||
}
|
|
||||||
return parent;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the root of the Binary Tree
|
|
||||||
*
|
|
||||||
* @return the root of the Binary Tree
|
|
||||||
*/
|
|
||||||
public Node getRoot(){
|
|
||||||
return root;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Prints leftChild - root - rightChild
|
|
||||||
*
|
|
||||||
* @param localRoot The local root of the binary tree
|
|
||||||
*/
|
|
||||||
public void inOrder(Node localRoot){
|
|
||||||
if(localRoot != null){
|
|
||||||
inOrder(localRoot.left);
|
|
||||||
System.out.print(localRoot.data + " ");
|
|
||||||
inOrder(localRoot.right);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Prints root - leftChild - rightChild
|
|
||||||
*
|
|
||||||
* @param localRoot The local root of the binary tree
|
|
||||||
*/
|
|
||||||
public void preOrder(Node localRoot){
|
|
||||||
if(localRoot != null){
|
|
||||||
System.out.print(localRoot.data + " ");
|
|
||||||
preOrder(localRoot.left);
|
|
||||||
preOrder(localRoot.right);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Prints rightChild - leftChild - root
|
|
||||||
*
|
|
||||||
* @param localRoot The local root of the binary tree
|
|
||||||
*/
|
|
||||||
public void postOrder(Node localRoot){
|
|
||||||
if(localRoot != null){
|
|
||||||
postOrder(localRoot.left);
|
|
||||||
postOrder(localRoot.right);
|
|
||||||
System.out.print(localRoot.data + " ");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -6,6 +6,7 @@ import java.util.LinkedList;
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
// Driver Program
|
// Driver Program
|
||||||
public class TreeTraversal {
|
public class TreeTraversal {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
Loading…
Reference in New Issue
Block a user