style: enable ConstantName
in checkstyle (#5139)
Co-authored-by: Maria Paszkiewicz SCC <maria.paszkiewicz@kit.edu>
This commit is contained in:
parent
ede3e4651f
commit
1e2d7e9431
@ -108,7 +108,7 @@
|
|||||||
|
|
||||||
<!-- Checks for Naming Conventions. -->
|
<!-- Checks for Naming Conventions. -->
|
||||||
<!-- See https://checkstyle.org/checks/naming/index.html -->
|
<!-- See https://checkstyle.org/checks/naming/index.html -->
|
||||||
<!-- TODO <module name="ConstantName"/> -->
|
<module name="ConstantName"/>
|
||||||
<!-- TODO <module name="LocalFinalVariableName"/> -->
|
<!-- TODO <module name="LocalFinalVariableName"/> -->
|
||||||
<!-- TODO <module name="LocalVariableName"/> -->
|
<!-- TODO <module name="LocalVariableName"/> -->
|
||||||
<!-- TODO <module name="MemberName"/> -->
|
<!-- TODO <module name="MemberName"/> -->
|
||||||
|
@ -26,8 +26,8 @@ import java.util.*;
|
|||||||
*/
|
*/
|
||||||
public class KnightsTour {
|
public class KnightsTour {
|
||||||
|
|
||||||
private static final int base = 12;
|
private static final int BASE = 12;
|
||||||
private static final int[][] moves = {
|
private static final int[][] MOVES = {
|
||||||
{1, -2},
|
{1, -2},
|
||||||
{2, -1},
|
{2, -1},
|
||||||
{2, 1},
|
{2, 1},
|
||||||
@ -41,19 +41,19 @@ public class KnightsTour {
|
|||||||
private static int total; // total squares in chess
|
private static int total; // total squares in chess
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
grid = new int[base][base];
|
grid = new int[BASE][BASE];
|
||||||
total = (base - 4) * (base - 4);
|
total = (BASE - 4) * (BASE - 4);
|
||||||
|
|
||||||
for (int r = 0; r < base; r++) {
|
for (int r = 0; r < BASE; r++) {
|
||||||
for (int c = 0; c < base; c++) {
|
for (int c = 0; c < BASE; c++) {
|
||||||
if (r < 2 || r > base - 3 || c < 2 || c > base - 3) {
|
if (r < 2 || r > BASE - 3 || c < 2 || c > BASE - 3) {
|
||||||
grid[r][c] = -1;
|
grid[r][c] = -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int row = 2 + (int) (Math.random() * (base - 4));
|
int row = 2 + (int) (Math.random() * (BASE - 4));
|
||||||
int col = 2 + (int) (Math.random() * (base - 4));
|
int col = 2 + (int) (Math.random() * (BASE - 4));
|
||||||
|
|
||||||
grid[row][col] = 1;
|
grid[row][col] = 1;
|
||||||
|
|
||||||
@ -95,7 +95,7 @@ public class KnightsTour {
|
|||||||
private static List<int[]> neighbors(int row, int column) {
|
private static List<int[]> neighbors(int row, int column) {
|
||||||
List<int[]> neighbour = new ArrayList<>();
|
List<int[]> neighbour = new ArrayList<>();
|
||||||
|
|
||||||
for (int[] m : moves) {
|
for (int[] m : MOVES) {
|
||||||
int x = m[0];
|
int x = m[0];
|
||||||
int y = m[1];
|
int y = m[1];
|
||||||
if (grid[row + y][column + x] == 0) {
|
if (grid[row + y][column + x] == 0) {
|
||||||
@ -109,7 +109,7 @@ public class KnightsTour {
|
|||||||
// Returns the total count of neighbors
|
// Returns the total count of neighbors
|
||||||
private static int countNeighbors(int row, int column) {
|
private static int countNeighbors(int row, int column) {
|
||||||
int num = 0;
|
int num = 0;
|
||||||
for (int[] m : moves) {
|
for (int[] m : MOVES) {
|
||||||
if (grid[row + m[1]][column + m[0]] == 0) {
|
if (grid[row + m[1]][column + m[0]] == 0) {
|
||||||
num++;
|
num++;
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ package com.thealgorithms.ciphers;
|
|||||||
*/
|
*/
|
||||||
public class Polybius {
|
public class Polybius {
|
||||||
|
|
||||||
private static final char[][] key = {
|
private static final char[][] KEY = {
|
||||||
// 0 1 2 3 4
|
// 0 1 2 3 4
|
||||||
/* 0 */ {'A', 'B', 'C', 'D', 'E'},
|
/* 0 */ {'A', 'B', 'C', 'D', 'E'},
|
||||||
/* 1 */ {'F', 'G', 'H', 'I', 'J'},
|
/* 1 */ {'F', 'G', 'H', 'I', 'J'},
|
||||||
@ -26,9 +26,9 @@ public class Polybius {
|
|||||||
|
|
||||||
private static String findLocationByCharacter(final char character) {
|
private static String findLocationByCharacter(final char character) {
|
||||||
final StringBuilder location = new StringBuilder();
|
final StringBuilder location = new StringBuilder();
|
||||||
for (int i = 0; i < key.length; i++) {
|
for (int i = 0; i < KEY.length; i++) {
|
||||||
for (int j = 0; j < key[i].length; j++) {
|
for (int j = 0; j < KEY[i].length; j++) {
|
||||||
if (character == key[i][j]) {
|
if (character == KEY[i][j]) {
|
||||||
location.append(i).append(j);
|
location.append(i).append(j);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -53,7 +53,7 @@ public class Polybius {
|
|||||||
for (int i = 0; i < chars.length; i += 2) {
|
for (int i = 0; i < chars.length; i += 2) {
|
||||||
int pozitionX = Character.getNumericValue(chars[i]);
|
int pozitionX = Character.getNumericValue(chars[i]);
|
||||||
int pozitionY = Character.getNumericValue(chars[i + 1]);
|
int pozitionY = Character.getNumericValue(chars[i + 1]);
|
||||||
plaintext.append(key[pozitionX][pozitionY]);
|
plaintext.append(KEY[pozitionX][pozitionY]);
|
||||||
}
|
}
|
||||||
return plaintext.toString();
|
return plaintext.toString();
|
||||||
}
|
}
|
||||||
|
@ -3,10 +3,10 @@ package com.thealgorithms.conversions;
|
|||||||
// hex = [0 - 9] -> [A - F]
|
// hex = [0 - 9] -> [A - F]
|
||||||
class DecimalToHexaDecimal {
|
class DecimalToHexaDecimal {
|
||||||
|
|
||||||
private static final int sizeOfIntInHalfBytes = 8;
|
private static final int SIZE_OF_INT_IN_HALF_BYTES = 8;
|
||||||
private static final int numberOfBitsInAHalfByte = 4;
|
private static final int NUMBER_OF_BITS_IN_HALF_BYTE = 4;
|
||||||
private static final int halfByte = 0x0F;
|
private static final int HALF_BYTE = 0x0F;
|
||||||
private static final char[] hexDigits = {
|
private static final char[] HEX_DIGITS = {
|
||||||
'0',
|
'0',
|
||||||
'1',
|
'1',
|
||||||
'2',
|
'2',
|
||||||
@ -27,12 +27,12 @@ class DecimalToHexaDecimal {
|
|||||||
|
|
||||||
// Returns the hex value of the dec entered in the parameter.
|
// Returns the hex value of the dec entered in the parameter.
|
||||||
public static String decToHex(int dec) {
|
public static String decToHex(int dec) {
|
||||||
StringBuilder hexBuilder = new StringBuilder(sizeOfIntInHalfBytes);
|
StringBuilder hexBuilder = new StringBuilder(SIZE_OF_INT_IN_HALF_BYTES);
|
||||||
hexBuilder.setLength(sizeOfIntInHalfBytes);
|
hexBuilder.setLength(SIZE_OF_INT_IN_HALF_BYTES);
|
||||||
for (int i = sizeOfIntInHalfBytes - 1; i >= 0; --i) {
|
for (int i = SIZE_OF_INT_IN_HALF_BYTES - 1; i >= 0; --i) {
|
||||||
int j = dec & halfByte;
|
int j = dec & HALF_BYTE;
|
||||||
hexBuilder.setCharAt(i, hexDigits[j]);
|
hexBuilder.setCharAt(i, HEX_DIGITS[j]);
|
||||||
dec >>= numberOfBitsInAHalfByte;
|
dec >>= NUMBER_OF_BITS_IN_HALF_BYTE;
|
||||||
}
|
}
|
||||||
return hexBuilder.toString().toLowerCase();
|
return hexBuilder.toString().toLowerCase();
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ package com.thealgorithms.conversions;
|
|||||||
*/
|
*/
|
||||||
public class IntegerToRoman {
|
public class IntegerToRoman {
|
||||||
|
|
||||||
private static final int[] allArabianRomanNumbers = new int[] {
|
private static final int[] ALL_ROMAN_NUMBERS_IN_ARABIC = new int[] {
|
||||||
1000,
|
1000,
|
||||||
900,
|
900,
|
||||||
500,
|
500,
|
||||||
@ -24,7 +24,7 @@ public class IntegerToRoman {
|
|||||||
4,
|
4,
|
||||||
1,
|
1,
|
||||||
};
|
};
|
||||||
private static final String[] allRomanNumbers = new String[] {
|
private static final String[] ALL_ROMAN_NUMBERS = new String[] {
|
||||||
"M",
|
"M",
|
||||||
"CM",
|
"CM",
|
||||||
"D",
|
"D",
|
||||||
@ -48,13 +48,13 @@ public class IntegerToRoman {
|
|||||||
|
|
||||||
StringBuilder builder = new StringBuilder();
|
StringBuilder builder = new StringBuilder();
|
||||||
|
|
||||||
for (int a = 0; a < allArabianRomanNumbers.length; a++) {
|
for (int a = 0; a < ALL_ROMAN_NUMBERS_IN_ARABIC.length; a++) {
|
||||||
int times = num / allArabianRomanNumbers[a];
|
int times = num / ALL_ROMAN_NUMBERS_IN_ARABIC[a];
|
||||||
for (int b = 0; b < times; b++) {
|
for (int b = 0; b < times; b++) {
|
||||||
builder.append(allRomanNumbers[a]);
|
builder.append(ALL_ROMAN_NUMBERS[a]);
|
||||||
}
|
}
|
||||||
|
|
||||||
num -= times * allArabianRomanNumbers[a];
|
num -= times * ALL_ROMAN_NUMBERS_IN_ARABIC[a];
|
||||||
}
|
}
|
||||||
|
|
||||||
return builder.toString();
|
return builder.toString();
|
||||||
|
@ -4,9 +4,7 @@ import java.util.*;
|
|||||||
|
|
||||||
public class RomanToInteger {
|
public class RomanToInteger {
|
||||||
|
|
||||||
private static final Map<Character, Integer> map = new HashMap<>() {
|
private static final Map<Character, Integer> ROMAN_TO_INT = new HashMap<>() {
|
||||||
private static final long serialVersionUID = 87605733047260530L;
|
|
||||||
|
|
||||||
{
|
{
|
||||||
put('I', 1);
|
put('I', 1);
|
||||||
put('V', 5);
|
put('V', 5);
|
||||||
@ -38,10 +36,10 @@ public class RomanToInteger {
|
|||||||
|
|
||||||
if (prev != ' ') {
|
if (prev != ' ') {
|
||||||
// checking current Number greater then previous or not
|
// checking current Number greater then previous or not
|
||||||
newPrev = map.get(prev) > newPrev ? map.get(prev) : newPrev;
|
newPrev = ROMAN_TO_INT.get(prev) > newPrev ? ROMAN_TO_INT.get(prev) : newPrev;
|
||||||
}
|
}
|
||||||
|
|
||||||
int currentNum = map.get(c);
|
int currentNum = ROMAN_TO_INT.get(c);
|
||||||
|
|
||||||
// if current number greater then prev max previous then add
|
// if current number greater then prev max previous then add
|
||||||
if (currentNum >= newPrev) {
|
if (currentNum >= newPrev) {
|
||||||
|
@ -5,14 +5,14 @@ import java.util.Scanner;
|
|||||||
|
|
||||||
public class LCA {
|
public class LCA {
|
||||||
|
|
||||||
private static final Scanner scanner = new Scanner(System.in);
|
private static final Scanner SCANNER = new Scanner(System.in);
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
// The adjacency list representation of a tree:
|
// The adjacency list representation of a tree:
|
||||||
ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
|
ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
|
||||||
|
|
||||||
// v is the number of vertices and e is the number of edges
|
// v is the number of vertices and e is the number of edges
|
||||||
int v = scanner.nextInt(), e = v - 1;
|
int v = SCANNER.nextInt(), e = v - 1;
|
||||||
|
|
||||||
for (int i = 0; i < v; i++) {
|
for (int i = 0; i < v; i++) {
|
||||||
adj.add(new ArrayList<Integer>());
|
adj.add(new ArrayList<Integer>());
|
||||||
@ -21,8 +21,8 @@ public class LCA {
|
|||||||
// Storing the given tree as an adjacency list
|
// Storing the given tree as an adjacency list
|
||||||
int to, from;
|
int to, from;
|
||||||
for (int i = 0; i < e; i++) {
|
for (int i = 0; i < e; i++) {
|
||||||
to = scanner.nextInt();
|
to = SCANNER.nextInt();
|
||||||
from = scanner.nextInt();
|
from = SCANNER.nextInt();
|
||||||
|
|
||||||
adj.get(to).add(from);
|
adj.get(to).add(from);
|
||||||
adj.get(from).add(to);
|
adj.get(from).add(to);
|
||||||
@ -38,7 +38,7 @@ public class LCA {
|
|||||||
dfs(adj, 0, -1, parent, depth);
|
dfs(adj, 0, -1, parent, depth);
|
||||||
|
|
||||||
// Inputting the two vertices whose LCA is to be calculated
|
// Inputting the two vertices whose LCA is to be calculated
|
||||||
int v1 = scanner.nextInt(), v2 = scanner.nextInt();
|
int v1 = SCANNER.nextInt(), v2 = SCANNER.nextInt();
|
||||||
|
|
||||||
// Outputting the LCA
|
// Outputting the LCA
|
||||||
System.out.println(getLCA(v1, v2, depth, parent));
|
System.out.println(getLCA(v1, v2, depth, parent));
|
||||||
|
@ -9,7 +9,7 @@ import java.util.Scanner;
|
|||||||
*/
|
*/
|
||||||
public class Fibonacci {
|
public class Fibonacci {
|
||||||
|
|
||||||
private static final Map<Integer, Integer> map = new HashMap<>();
|
private static final Map<Integer, Integer> CACHE = new HashMap<>();
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
// Methods all returning [0, 1, 1, 2, 3, 5, ...] for n = [0, 1, 2, 3, 4, 5, ...]
|
// Methods all returning [0, 1, 1, 2, 3, 5, ...] for n = [0, 1, 2, 3, 4, 5, ...]
|
||||||
@ -30,8 +30,8 @@ public class Fibonacci {
|
|||||||
* Outputs the nth fibonacci number
|
* Outputs the nth fibonacci number
|
||||||
*/
|
*/
|
||||||
public static int fibMemo(int n) {
|
public static int fibMemo(int n) {
|
||||||
if (map.containsKey(n)) {
|
if (CACHE.containsKey(n)) {
|
||||||
return map.get(n);
|
return CACHE.get(n);
|
||||||
}
|
}
|
||||||
|
|
||||||
int f;
|
int f;
|
||||||
@ -40,7 +40,7 @@ public class Fibonacci {
|
|||||||
f = n;
|
f = n;
|
||||||
} else {
|
} else {
|
||||||
f = fibMemo(n - 1) + fibMemo(n - 2);
|
f = fibMemo(n - 1) + fibMemo(n - 2);
|
||||||
map.put(n, f);
|
CACHE.put(n, f);
|
||||||
}
|
}
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
|
@ -6,8 +6,8 @@ import java.util.Scanner;
|
|||||||
|
|
||||||
public class MatrixChainMultiplication {
|
public class MatrixChainMultiplication {
|
||||||
|
|
||||||
private static final Scanner scan = new Scanner(System.in);
|
private static final Scanner SCANNER = new Scanner(System.in);
|
||||||
private static final ArrayList<Matrix> mArray = new ArrayList<>();
|
private static final ArrayList<Matrix> MATRICES = new ArrayList<>();
|
||||||
private static int size;
|
private static int size;
|
||||||
private static int[][] m;
|
private static int[][] m;
|
||||||
private static int[][] s;
|
private static int[][] s;
|
||||||
@ -24,14 +24,14 @@ public class MatrixChainMultiplication {
|
|||||||
int row = Integer.parseInt(mSize[1]);
|
int row = Integer.parseInt(mSize[1]);
|
||||||
|
|
||||||
Matrix matrix = new Matrix(count, col, row);
|
Matrix matrix = new Matrix(count, col, row);
|
||||||
mArray.add(matrix);
|
MATRICES.add(matrix);
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
for (Matrix m : mArray) {
|
for (Matrix m : MATRICES) {
|
||||||
System.out.format("A(%d) = %2d x %2d%n", m.count(), m.col(), m.row());
|
System.out.format("A(%d) = %2d x %2d%n", m.count(), m.col(), m.row());
|
||||||
}
|
}
|
||||||
|
|
||||||
size = mArray.size();
|
size = MATRICES.size();
|
||||||
m = new int[size + 1][size + 1];
|
m = new int[size + 1][size + 1];
|
||||||
s = new int[size + 1][size + 1];
|
s = new int[size + 1][size + 1];
|
||||||
p = new int[size + 1];
|
p = new int[size + 1];
|
||||||
@ -42,7 +42,7 @@ public class MatrixChainMultiplication {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < p.length; i++) {
|
for (int i = 0; i < p.length; i++) {
|
||||||
p[i] = i == 0 ? mArray.get(i).col() : mArray.get(i - 1).row();
|
p[i] = i == 0 ? MATRICES.get(i).col() : MATRICES.get(i - 1).row();
|
||||||
}
|
}
|
||||||
|
|
||||||
matrixChainOrder();
|
matrixChainOrder();
|
||||||
@ -109,7 +109,7 @@ public class MatrixChainMultiplication {
|
|||||||
|
|
||||||
private static String[] input(String string) {
|
private static String[] input(String string) {
|
||||||
System.out.print(string);
|
System.out.print(string);
|
||||||
return (scan.nextLine().split(" "));
|
return (SCANNER.nextLine().split(" "));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,7 +8,7 @@ import java.util.Random;
|
|||||||
*/
|
*/
|
||||||
public class FindKthNumber {
|
public class FindKthNumber {
|
||||||
|
|
||||||
private static final Random random = new Random();
|
private static final Random RANDOM = new Random();
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
/* generate an array with random size and random elements */
|
/* generate an array with random size and random elements */
|
||||||
@ -29,11 +29,11 @@ public class FindKthNumber {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static int[] generateArray(int capacity) {
|
private static int[] generateArray(int capacity) {
|
||||||
int size = random.nextInt(capacity) + 1;
|
int size = RANDOM.nextInt(capacity) + 1;
|
||||||
int[] array = new int[size];
|
int[] array = new int[size];
|
||||||
|
|
||||||
for (int i = 0; i < size; i++) {
|
for (int i = 0; i < size; i++) {
|
||||||
array[i] = random.nextInt() % 100;
|
array[i] = RANDOM.nextInt() % 100;
|
||||||
}
|
}
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
@ -10,10 +10,10 @@ import java.util.Scanner;
|
|||||||
public class Fibonacci {
|
public class Fibonacci {
|
||||||
|
|
||||||
// Exponentiation matrix for Fibonacci sequence
|
// Exponentiation matrix for Fibonacci sequence
|
||||||
private static final int[][] fibMatrix = {{1, 1}, {1, 0}};
|
private static final int[][] FIB_MATRIX = {{1, 1}, {1, 0}};
|
||||||
private static final int[][] identityMatrix = {{1, 0}, {0, 1}};
|
private static final int[][] IDENTITY_MATRIX = {{1, 0}, {0, 1}};
|
||||||
// First 2 fibonacci numbers
|
// First 2 fibonacci numbers
|
||||||
private static final int[][] baseFibNumbers = {{1}, {0}};
|
private static final int[][] BASE_FIB_NUMBERS = {{1}, {0}};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Performs multiplication of 2 matrices
|
* Performs multiplication of 2 matrices
|
||||||
@ -53,14 +53,14 @@ public class Fibonacci {
|
|||||||
*/
|
*/
|
||||||
public static int[][] fib(int n) {
|
public static int[][] fib(int n) {
|
||||||
if (n == 0) {
|
if (n == 0) {
|
||||||
return Fibonacci.identityMatrix;
|
return Fibonacci.IDENTITY_MATRIX;
|
||||||
} else {
|
} else {
|
||||||
int[][] cachedResult = fib(n / 2);
|
int[][] cachedResult = fib(n / 2);
|
||||||
int[][] matrixExpResult = matrixMultiplication(cachedResult, cachedResult);
|
int[][] matrixExpResult = matrixMultiplication(cachedResult, cachedResult);
|
||||||
if (n % 2 == 0) {
|
if (n % 2 == 0) {
|
||||||
return matrixExpResult;
|
return matrixExpResult;
|
||||||
} else {
|
} else {
|
||||||
return matrixMultiplication(Fibonacci.fibMatrix, matrixExpResult);
|
return matrixMultiplication(Fibonacci.FIB_MATRIX, matrixExpResult);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -69,7 +69,7 @@ public class Fibonacci {
|
|||||||
// Returns [0, 1, 1, 2, 3, 5 ..] for n = [0, 1, 2, 3, 4, 5.. ]
|
// Returns [0, 1, 1, 2, 3, 5 ..] for n = [0, 1, 2, 3, 4, 5.. ]
|
||||||
Scanner sc = new Scanner(System.in);
|
Scanner sc = new Scanner(System.in);
|
||||||
int n = sc.nextInt();
|
int n = sc.nextInt();
|
||||||
int[][] result = matrixMultiplication(fib(n), baseFibNumbers);
|
int[][] result = matrixMultiplication(fib(n), BASE_FIB_NUMBERS);
|
||||||
System.out.println("Fib(" + n + ") = " + result[1][0]);
|
System.out.println("Fib(" + n + ") = " + result[1][0]);
|
||||||
sc.close();
|
sc.close();
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@ public class Conway {
|
|||||||
*1s, two 2s, one 1" or 312211. https://en.wikipedia.org/wiki/Look-and-say_sequence
|
*1s, two 2s, one 1" or 312211. https://en.wikipedia.org/wiki/Look-and-say_sequence
|
||||||
* */
|
* */
|
||||||
|
|
||||||
private static final StringBuilder builder = new StringBuilder();
|
private static final StringBuilder BUILDER = new StringBuilder();
|
||||||
|
|
||||||
protected static List<String> generateList(String originalString, int maxIteration) {
|
protected static List<String> generateList(String originalString, int maxIteration) {
|
||||||
List<String> numbers = new ArrayList<>();
|
List<String> numbers = new ArrayList<>();
|
||||||
@ -25,9 +25,9 @@ public class Conway {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static String generateNextElement(String originalString) {
|
public static String generateNextElement(String originalString) {
|
||||||
builder.setLength(0);
|
BUILDER.setLength(0);
|
||||||
String[] stp = originalString.split("(?<=(.))(?!\\1)");
|
String[] stp = originalString.split("(?<=(.))(?!\\1)");
|
||||||
Arrays.stream(stp).forEach(s -> builder.append(s.length()).append(s.charAt(0)));
|
Arrays.stream(stp).forEach(s -> BUILDER.append(s.length()).append(s.charAt(0)));
|
||||||
return builder.toString();
|
return BUILDER.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,15 +9,15 @@ import java.util.Scanner;
|
|||||||
// Program will simply end if there is no match
|
// Program will simply end if there is no match
|
||||||
public class RabinKarp {
|
public class RabinKarp {
|
||||||
|
|
||||||
public static Scanner scanner = null;
|
public static Scanner SCANNER = null;
|
||||||
public static final int d = 256;
|
public static final int ALPHABET_SIZE = 256;
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
scanner = new Scanner(System.in);
|
SCANNER = new Scanner(System.in);
|
||||||
System.out.println("Enter String");
|
System.out.println("Enter String");
|
||||||
String text = scanner.nextLine();
|
String text = SCANNER.nextLine();
|
||||||
System.out.println("Enter pattern");
|
System.out.println("Enter pattern");
|
||||||
String pattern = scanner.nextLine();
|
String pattern = SCANNER.nextLine();
|
||||||
|
|
||||||
int q = 101;
|
int q = 101;
|
||||||
searchPat(text, pattern, q);
|
searchPat(text, pattern, q);
|
||||||
@ -32,14 +32,14 @@ public class RabinKarp {
|
|||||||
int j = 0;
|
int j = 0;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
||||||
h = (int) Math.pow(d, m - 1) % q;
|
h = (int) Math.pow(ALPHABET_SIZE, m - 1) % q;
|
||||||
|
|
||||||
for (i = 0; i < m; i++) {
|
for (i = 0; i < m; i++) {
|
||||||
// hash value is calculated for each character and then added with the hash value of the
|
// hash value is calculated for each character and then added with the hash value of the
|
||||||
// next character for pattern as well as the text for length equal to the length of
|
// next character for pattern as well as the text for length equal to the length of
|
||||||
// pattern
|
// pattern
|
||||||
p = (d * p + pattern.charAt(i)) % q;
|
p = (ALPHABET_SIZE * p + pattern.charAt(i)) % q;
|
||||||
t = (d * t + text.charAt(i)) % q;
|
t = (ALPHABET_SIZE * t + text.charAt(i)) % q;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i <= n - m; i++) {
|
for (i = 0; i <= n - m; i++) {
|
||||||
@ -67,7 +67,7 @@ public class RabinKarp {
|
|||||||
// value of the next character after the end of the evaluated characters is added to get
|
// value of the next character after the end of the evaluated characters is added to get
|
||||||
// the hash value of the next window of characters in the text
|
// the hash value of the next window of characters in the text
|
||||||
if (i < n - m) {
|
if (i < n - m) {
|
||||||
t = (d * (t - text.charAt(i) * h) + text.charAt(i + m)) % q;
|
t = (ALPHABET_SIZE * (t - text.charAt(i) * h) + text.charAt(i + m)) % q;
|
||||||
|
|
||||||
// if hash value becomes less than zero than q is added to make it positive
|
// if hash value becomes less than zero than q is added to make it positive
|
||||||
if (t < 0) {
|
if (t < 0) {
|
||||||
|
@ -6,8 +6,7 @@ public final class RabinKarpAlgorithm {
|
|||||||
private RabinKarpAlgorithm() {
|
private RabinKarpAlgorithm() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// d is the number of characters in the input alphabet
|
private static final int ALPHABET_SIZE = 256;
|
||||||
private static final int d = 256;
|
|
||||||
|
|
||||||
public static int search(String pattern, String text, int primeNumber) {
|
public static int search(String pattern, String text, int primeNumber) {
|
||||||
|
|
||||||
@ -19,13 +18,13 @@ public final class RabinKarpAlgorithm {
|
|||||||
int h = 1;
|
int h = 1;
|
||||||
|
|
||||||
// The value of h would be "pow(d, patternLength-1)%primeNumber"
|
// The value of h would be "pow(d, patternLength-1)%primeNumber"
|
||||||
for (int i = 0; i < patternLength - 1; i++) h = (h * d) % primeNumber;
|
for (int i = 0; i < patternLength - 1; i++) h = (h * ALPHABET_SIZE) % primeNumber;
|
||||||
|
|
||||||
// Calculate the hash value of pattern and first
|
// Calculate the hash value of pattern and first
|
||||||
// window of text
|
// window of text
|
||||||
for (int i = 0; i < patternLength; i++) {
|
for (int i = 0; i < patternLength; i++) {
|
||||||
hashForPattern = (d * hashForPattern + pattern.charAt(i)) % primeNumber;
|
hashForPattern = (ALPHABET_SIZE * hashForPattern + pattern.charAt(i)) % primeNumber;
|
||||||
hashForText = (d * hashForText + text.charAt(i)) % primeNumber;
|
hashForText = (ALPHABET_SIZE * hashForText + text.charAt(i)) % primeNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Slide the pattern over text one by one
|
// Slide the pattern over text one by one
|
||||||
@ -51,7 +50,7 @@ public final class RabinKarpAlgorithm {
|
|||||||
// Calculate hash value for next window of text: Remove
|
// Calculate hash value for next window of text: Remove
|
||||||
// leading digit, add trailing digit
|
// leading digit, add trailing digit
|
||||||
if (i < textLength - patternLength) {
|
if (i < textLength - patternLength) {
|
||||||
hashForText = (d * (hashForText - text.charAt(i) * h) + text.charAt(i + patternLength)) % primeNumber;
|
hashForText = (ALPHABET_SIZE * (hashForText - text.charAt(i) * h) + text.charAt(i + patternLength)) % primeNumber;
|
||||||
|
|
||||||
// handling negative hashForText
|
// handling negative hashForText
|
||||||
if (hashForText < 0) hashForText = (hashForText + primeNumber);
|
if (hashForText < 0) hashForText = (hashForText + primeNumber);
|
||||||
|
@ -8,7 +8,7 @@ import java.util.Random;
|
|||||||
*/
|
*/
|
||||||
public class BogoSort implements SortAlgorithm {
|
public class BogoSort implements SortAlgorithm {
|
||||||
|
|
||||||
private static final Random random = new Random();
|
private static final Random RANDOM = new Random();
|
||||||
|
|
||||||
private static <T extends Comparable<T>> boolean isSorted(T[] array) {
|
private static <T extends Comparable<T>> boolean isSorted(T[] array) {
|
||||||
for (int i = 0; i < array.length - 1; i++) {
|
for (int i = 0; i < array.length - 1; i++) {
|
||||||
@ -24,7 +24,7 @@ public class BogoSort implements SortAlgorithm {
|
|||||||
int length = array.length;
|
int length = array.length;
|
||||||
|
|
||||||
for (int i = 0; i < array.length; i++) {
|
for (int i = 0; i < array.length; i++) {
|
||||||
int randomIndex = i + random.nextInt(length - i);
|
int randomIndex = i + RANDOM.nextInt(length - i);
|
||||||
SortUtils.swap(array, randomIndex, i);
|
SortUtils.swap(array, randomIndex, i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,12 +4,12 @@ import java.util.Random;
|
|||||||
|
|
||||||
public class SortUtilsRandomGenerator {
|
public class SortUtilsRandomGenerator {
|
||||||
|
|
||||||
private static final Random random;
|
private static final Random RANDOM;
|
||||||
private static final long seed;
|
private static final long SEED;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
seed = System.currentTimeMillis();
|
SEED = System.currentTimeMillis();
|
||||||
random = new Random(seed);
|
RANDOM = new Random(SEED);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -30,7 +30,7 @@ public class SortUtilsRandomGenerator {
|
|||||||
* @return Double value [0, 1)
|
* @return Double value [0, 1)
|
||||||
*/
|
*/
|
||||||
public static Double generateDouble() {
|
public static Double generateDouble() {
|
||||||
return random.nextDouble();
|
return RANDOM.nextDouble();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -39,6 +39,6 @@ public class SortUtilsRandomGenerator {
|
|||||||
* @return int value [0, n)
|
* @return int value [0, n)
|
||||||
*/
|
*/
|
||||||
public static int generateInt(int n) {
|
public static int generateInt(int n) {
|
||||||
return random.nextInt(n);
|
return RANDOM.nextInt(n);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user