Box type pattern in java - java

I want to create a pattern something look likes a box.
I tried every reflection and mirror but unable to achieve the exact loop.
It's the box look like loop for String s="ROHIT";

This does what you asked for (the idea is to check on wich inside level you are):
public static void main(String[] args) {
String s = "ROHIT";
int size = s.length() * 2;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if ((j > i && size - j > i) || (size - j > size - i && j >= size - i)) {
print(s, i);
} else {
print(s, j);
}
}
System.out.println("");
}
}
private static void print(String s, int i) {
if (i < s.length()) {
System.out.print(s.charAt(i));
} else {
System.out.print(s.charAt(s.length() - i % s.length() - 1));
}
}
Actually, this is more intuitive:
public static void main(String[] args) {
String s = "ROHIT";
int size = s.length() * 2;
int max = s.length();
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
int level = Math.min(normalize(i, max), normalize(j, max));
System.out.print(s.charAt(level));
}
System.out.println("");
}
}
private static int normalize(int i, int max) {
return (i >= max) ? 2 * max - i - 1: i;
}

int n = 2 * word.length();
for (int i = 0; i < n; ++i) {
int iWord = i < word.length()? i : 2 * word.length() - 1 - i;
for (int j = 0; j < n; ++j) {
int jWord = j < word.length()? j : 2 * word.length() - 1 - j;
int diagonal = Math.min(iWord, jWord);
System.out.print(word.charAt(diagonal));
}
System.out.println();
}

This is the algortithm /pseuodo code
for(each row)
{
if(firstrow) print total row;
else if(lastrow) print total row;
else {
for (each column)
{
if(firstcol) print col;
if(lastcol) print col;
}
}
}

Related

Are there ways to resolve the problems mentioned in the description in order to run the code on Intellij IDEA?

It says, "Class 'Practice' is never used" and "Method 'maxAreaOfIsland(int[][])' is never used" with the addition of "Typo: In word 'trav'". The output should be the number 6, but it never gets to it because of the above problems. I am trying to fix the problem in order to generate an output.The code was borrowed from leetcode.com. It works in that website's java compiler, but not int Intellij IDEA.
class Practice {
private int n, m;
public int maxAreaOfIsland(int[][] grid) {
int ans = 0;
n = grid.length;
m = grid[0].length;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (grid[i][j] > 0) ans = Math.max(ans, trav(i, j, grid));
return ans;
}
private int trav(int i, int j, int[][] grid) {
if (i < 0 || j < 0 || i >= n || j >= m || grid[i][j] < 1) return 0;
grid[i][j] = 0;
return 1 + trav(i-1, j, grid) + trav(i, j-1, grid) + trav(i+1, j, grid) + trav(i, j+1, grid);
}
public static void main(String[] args){
int value;
Practice example = new Practice();
int[][] yourGrid = {{0,0,1,0,0,0,0,1,0,0,0,0,0},{0,0,0,0,0,0,0,1,1,1,0,0,0},{0,1,1,0,1,0,0,0,0,0,0,0,0},{0,1,0,0,1,1,0,0,1,0,1,0,0},{0,1,0,0,1,1,0,0,1,1,1,0,0},{0,0,0,0,0,0,0,0,0,0,1,0,0},{0,0,0,0,0,0,0,1,1,1,0,0,0},{0,0,0,0,0,0,0,1,1,0,0,0,0}};
value = example.maxAreaOfIsland(yourGrid);
System.out.println(value);
}
}
Based on the updates/and comments:
The working example is here:
class Practice {
private int n, m;
public int maxAreaOfIsland(int[][] grid) {
int ans = 0;
n = grid.length;
m = grid[0].length;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (grid[i][j] > 0) ans = Math.max(ans, trav(i, j, grid));
return ans;
}
private int trav(int i, int j, int[][] grid) {
if (i < 0 || j < 0 || i >= n || j >= m || grid[i][j] < 1) return 0;
grid[i][j] = 0;
return 1 + trav(i-1, j, grid) + trav(i, j-1, grid) + trav(i+1, j, grid) + trav(i, j+1, grid);
}
public static void main(String[] args){
Practice example = new Practice();
int[][] yourGrid = {{0,0,1,0,0,0,0,1,0,0,0,0,0},{0,0,0,0,0,0,0,1,1,1,0,0,0},{0,1,1,0,1,0,0,0,0,0,0,0,0},{0,1,0,0,1,1,0,0,1,0,1,0,0},{0,1,0,0,1,1,0,0,1,1,1,0,0},{0,0,0,0,0,0,0,0,0,0,1,0,0},{0,0,0,0,0,0,0,1,1,1,0,0,0},{0,0,0,0,0,0,0,1,1,0,0,0,0}};
example.maxAreaOfIsland(yourGrid);
}
}
(Note: Explanations are already mentioned in the comments/updates)

How to find common numbers or an anomaly in a 2D array and cause it to trigger something else

I have a 2D array (a matrix of 10x10) with values ranging from 0 to -5.
I want a method to be triggered when there is a sequence of a value found within the array.
For example, there is a sequence of two negative 2. I want it to trigger an event/method that will give a bonus score of 4. This should happen only when there are two -2's and not if there is just one -2.
I tried achieving something like that but I cant figure out how to tell the program to only trigger when 'n' number of a value is found within the matrix.
public class Test {
static int board[][] = new int[10][10];
public static void Test() {
int i, j;
board[0][0] = -1;
board[0][1] = -1;
board[1][1] = -2;
board[1][2] = -2;
board[1][3] = -2;
board[1][4] = -2;
for (i = 0; i < board.length; i++) {
System.out.println("");
for (j = 0; j < board.length; j++) {
//board[i][j] = 0;
System.out.print(board[i][j]);
}
}
System.out.println();
}
public static void scanBoard() {
int i, j;
for (i = 0; i < board.length; i++) {
for (j = 0; j < board.length; j++) {
if (board[i][j] == -1) {
System.out.println("Hello");
}
}
}
}
public static void main(String[] args) {
Test(); //prints out whole array
scanBoard(); //scans for
}
}
public class Main {
static final int size = 10;
static int[][] matrix = new int[size][size];
public static void main(String[] args) {
System.out.println("The first matrix.\n");
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (i == 3 && j > 3) {
matrix[i][j] = -2; //-2
} else {
matrix[i][j] = 1;
}
System.out.print(matrix[i][j]);
}
System.out.println();
}
scanBoard();
System.out.println("\nThe second matrix.\n");
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (i == 9 && j > 5) {
matrix[i][j] = 2; //changed it from -2 to 2
} else {
matrix[i][j] = 1;
}
System.out.print(matrix[i][j]);
}
System.out.println();
}
scanBoard();
}
static void scanBoard() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (matrix[i][j] == -2 && (j + 3 < size)) {
if (matrix[i][j + 1] == -2 && matrix[i][j + 2] == -2 && matrix[i][j + 3] == -2) {
System.out.println("\nThere you go, a special effect!".toUpperCase());
}
}
}
}
}
}
I am not sure if this is the result you wished to see according to your request. I hope this helps you. And I did some changes in your code so it will be easier to read (In my opinion lol).
public class Main {
static final int size = 10;
static int[][] matrix = new int[size][size];
public static void main(String[] args) {
System.out.println("The first matrix.\n");
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (i == 9 && (j == 0 || j == 1)) {
matrix[i][j] = -2; //-2
} else {
matrix[i][j] = 1;
}
System.out.print(matrix[i][j]);
}
System.out.println();
}
scanBoard();
System.out.println("\nThe second matrix.\n");
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (i == 8 && (j == 5 || j == 6)) {
matrix[i][j] = 2; //changed it from -2 to 2
} else {
matrix[i][j] = 1;
}
System.out.print(matrix[i][j]);
}
System.out.println();
}
scanBoard();
}
static void scanBoard() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (matrix[i][j] == -2 && (j + 1 < size)) {
if (matrix[i][j + 1] == -2) {
//You can remove the '.toUpperCase()', it's just my personal preference
System.out.println("\nThere you go, a special effect!".toUpperCase());
}
}
}
}
}
}
From what I understood from the problem statement and comments, you want your scanBoard to behave like this:
public static void scanBoard(int value, int frequency) {
int i, j;
if (value <= 0 && value >= -5 && frequency >= 2 && frequency <= 10) {
for (i = 0; i < board.length; i++) {
int rowFrequency = 0;
for (j = 1; j < board.length; j++) {
if (board[i][j] == value && board[i][j - 1] == value) {
rowFrequency++;
} else {
rowFrequency = 0;
}
if (rowFrequency + 1 >= frequency) {
System.out.println("Hello");
}
}
}
}
}
public static void main(String[] args) {
Test(); //prints out whole array
scanBoard(-2, 4); //prints Hello once
scanBoard(-2, 3); //prints Hello twice
scanBoard(-2, 3); //prints Hello thrice
}

Trying to form a half triangle using a for loop, nothing is printing out

This is a practice exam question I was given to study for my java exam approaching...I was given the main method and cannot change the input, only alter the two other methods and their code. I need to print out
&
&&
&&&
&&&&
&&&&&
&&&&&&
I think I have written my for loop wrong to create the blank spaces, I cannot seem to get this write with the main method I have been give, any ideas?
public static void main(String[] args) {
int size = 6;
char c = '&';
for (int i = 1; i < size + 1; i++) {
drawBlanks(size, size - i);
drawChars(size, size - i, c);
System.out.println();
}
System.out.println();
}
public static void drawChars(int size, int i, char c) {
for (int j = size; j < 1; j--) {
System.out.print(c);
}
}
public static void drawBlanks(int size, int i) {
for (int k = 0; k <= i; k++) {
System.out.print(" ");
}
}
You have a problem in this loop :
for(int j = size; j < 1; j--)
Instead change it to :
for (int j = size; j > i; j--) {
//-------------------^_^
j should be > to i not j < to 1
Alternative solution within one loop:
public class Main {
public static void main(String[] args) {
int rowCount = 10;
int whiteSpaceCount = rowCount - 1;
for (int i = 0; i < rowCount; i++) {
for (int j = 0; j < rowCount; j++) {
char ch = ' ';
if (j >= whiteSpaceCount)
ch = '&';
System.out.print(ch);
}
System.out.println();
whiteSpaceCount = rowCount - (i + 2);
}
}
}
You need to change j < 1; to j > 1;, After that your output will be,
Output:
&&&&&
&&&&&
&&&&&
&&&&&
&&&&&
&&&&&
To get your expected output change j > 1 to j > i.
public static void main(String[] args) {
int size = 6;
char c = '&';
for (int i = 1; i < size + 1; i++) {
drawBlanks(size, size - i);
drawChars(size, size - i, c);
System.out.println();
}
System.out.println();
}
public static void drawChars(int size, int i, char c) {
for (int j = size; j > i; j--) {
System.out.print(c);
}
}
public static void drawBlanks(int size, int i) {
for (int k = 0; k <= i; k++) {
System.out.print(" ");
}
}
Output:
&
&&
&&&
&&&&
&&&&&
&&&&&&

Creating a double mirrored triangle

I need help making a mirrored triangle like this:
* *
** **
*** ***
********
I can get each one seperatly, but I can't combine them.
public static void main(String[] args){
for( int i = 1; i <= 5; i++ ){
for( int j = 0; j < i; j++ ){
System.out.print("*");
}
System.out.println();
}
for(int i = 0; i < 6; i++)
{
for(int j = 5; j > 0; j--)
{
if(i < j)
System.out.print(" ");
else
System.out.print("*");
}
System.out.println();
}
}
You need to track the position from both sides to be able to show * at correct location. Here is the solution:
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 10; j++) {
if (j <= i || j > 10 - i) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
You can do it using this code.
public class Test {
public void sizeOfTri(int triSize) { //Number of lines
int line = 1;
for(int i = 0; i < triSize; i++) { //Handles Each line
int temp = triSize * 2;
for(int j = 1; j < temp; j++) { //Handles Each space on the line
if(j <= line && j == triSize || j >= temp - line && j == triSize) { //For the very last line because it needs an extra *
System.out.print("**");
} else if(j <= line || j >= temp - line) { //For normal lines
System.out.print("*");
} else if(j == triSize) {
System.out.print(" "); //For the second to last line because it needs an extra space to make it look mirrored
} else { //For normal lines
System.out.print(" ");
}
}
System.out.println();
line++;
}
}
public static void main(String[] args) {
new Test().sizeOfTri(4);
}
}
I commented on next to if statements on which part does what. This will produce an output which looks like the below when run
* *
** **
*** ***
********
Although, all the above implementations are really good ones. I thought of doing it bit differently and make use of 2-D arrays.
package algorithms;
public class DrawMirror {
static void initialize(String[][] array){
for (int i=0; i<MAX_X; i++){
for (int j=0; j < MAX_Y; j++){
array[i][j] = " ";
}
}
}
static void draw(String[][] array, int x, int y){
for (int i=0; i < y; i++){
array[x][i] = "*";
array[x][MAX_Y-i-1] = "*";
}
}
final static int MAX_X = 4;
final static int MAX_Y = 8;
public static void main(String[] args) {
String[][] array = new String[MAX_X][MAX_Y];
initialize(array);
for (int i=0; i < MAX_X ; i++){
draw(array,i,i+1);
}
for( int i = 0; i < MAX_X; i++ ){
for( int j = 0; j < MAX_Y; j++ ){
System.out.print(array[i][j]);
}
System.out.println();
}
}
}
The following code is a function with variable height.
public static void printDoubleTriangle(int height) {
for(int i = 0; i < height; i++) {
for(int j = 0; j < 2*height; j++) {
if(j <= i || (2*height - j - 1) <= i) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}

ArrayIndexOutOfBounds in binomial cofficient dynamic programming

I am trying to create a method that calculates (N choose R) using dynamic programming but I get an array out of bounds exception:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
at BinomialCoefficients.method5(BinomialCoefficients.java:127)
at BinomialCoefficients.main(BinomialCoefficients.java:50)
I am using a 2 dimensional array. Here is my code,
protected static long method5(long lN, long lR)
{
long lArray[][] = new long[(int) (lN+1)][(int) (lR+1)];
for(int i = 0; i <= lN; i++)
{
lArray[i][0] = 1;
}
for(int i = 0; i <= lN; i++)
{
lArray[i][i] = 1;
}
for(int i = 0; i <= lN; i++)
{
for(int j = i; j <= i; j++)
{
lArray[i][j] = lArray[i-1][j-1] + lArray[i-1][j];
}
}
/*for(int i = 0; i <= lN; i++)
{
for(int j = 0; j <= i; j++)
{
System.out.print(String.format("%5d", lArray[i][j]));
}
System.out.println();
}*/
return lArray[(int) lN][(int) lR];
}
Can somebody tell me what is wrong with it?
This code looks suspicious
for(int i = 0; i <= lN; i++)
{
lArray[i][i] = 1;
}
considering lArray was initialized using two potentially different values:
long lArray[][] = new long[(int) (lN+1)][(int) (lR+1)];
I think you should have an inner loop here, such as:
for(int i = 0; i <= lN; i++) {
for (int j=0; j<=lR; j++) {
lArray[i][j] = 1;
}
}
I see the indexing error has already been corrected. But will point out that you're allocating a square array when you only need one row. You're also not taking advantage of symmetry. Consider something like this:
public class Binomial {
public static long n_choose_r(int n, int r) {
r = Math.min(r, n - r);
long [] a = new long[r + 1];
a[0] = 1;
for (int i = 1; i <= n; ++i) {
if (i <= r) {
a[i] = 1;
}
for (int j = Math.min(r, i - 1); j > 0; --j) {
a[j] += a[j - 1];
}
}
return a[r];
}
public static void main(String [] args) {
System.out.println(n_choose_r(6, 4));
}
}

Categories

Resources