How would I make this pascal triangle into a square? - java

How would I make this pascal triangle into a square?
import java.util.*;
public class PascalSquare {
public static void main(String[] args) {
int row = 7;
int[][] pascal = new int[row +1][];
pascal[1] = new int[1 + 2];
pascal[1][1] = 1;
for (int i = 2; i <= row; i++) {
pascal[i] = new int[i + 2];
for (int j = 1; j < pascal[i].length - 1; j++) {
pascal[i][j] = pascal[i-1][j-1] + pascal[i-1][j];
}
}
for (int i = 1; i <= row; i++) {
for (int j = 1; j < pascal[i].length - 1; j++) {
System.out.print(pascal[i][j] + " ");
}
System.out.println();
}
}
}
// I want this to be a 7*7 square that will follow the same principle as a pascal triangle(adding the numbers above)

Related

How can I add something from the Java recursion problem on finding paths?

I'm making a Java program where it counts the number of paths. I want it to only go up (north) and right (east). I also want it to output the paths taken. Is there something I can do?
import java.io.*;
public class PathsRecursion {
public static int countPaths(int n, int e) {
int dp[][] = new int[n + 1][e + 1];
for (int i = 0; i <= n; i++) {
dp[i][0] = 1;
}
for (int i = 0; i <= e; i++) {
dp[0][i] = 1;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= e; j++) {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
return dp[n][e];
}
public static void main(String[] args) {
int n = 1, e = 3;
System.out.println("Number of Paths: " + countPaths(n, e));
}
}
You added an extra row and column to you dp array. It needs to be a n by e array, not n + 1 by e + 1 array.
import java.io.*;
public class PathsRecursion {
public static int countPaths(int n, int e) {
int dp[][] = new int[n][e];
for (int i = 0; i <= n; i++) {
dp[i][0] = 1;
}
for (int i = 0; i <= e; i++) {
dp[0][i] = 1;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= e; j++) {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
return dp[n - 1][e - 1];
}
public static void main(String[] args) {
int n = 1, e = 3;
System.out.println("Number of Paths: " + countPaths(n, e));
}
}

why is system.out.println using 2-d arrays giveing error?

I am trying to populate my 2-D array with random intergers. The only issue I am having is the last system.out.println(); is highlighted in red. It says error can't resolve symbol 'println'
int[][] array = new int[row][col];
for (int i = 0; i <= rows; i++) {
System.out.println(i);
}
System.out.println();
for (int j = 0; j <= col; j++) {
System.out.println(j);
}
array[row][col] = ((int)(Math.random() * 10));
System.out.println(array[row][col]);
}
// this last line is highlighted in red
System.out.println();
}
}
You can try this
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int row;
int col;
System.out.println("row?");
row = sc.nextInt ();
System.out.println("column?");
col = sc.nextInt ();
int[][] array = new int[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
array[i][j] = ((int)(Math.random() * 10));
}
}
for(int i = 0 ;i < row ;i++) {
for(int j = 0 ; j < col ; j++) {
System.out.print(array[i][j] );
}
System.out.println();
}
}
}
You need to take care not to go beyond the end of the arrays, so the <= need to be <. You also want to use the for loops within each other to get the indices right. Thus:
import java.util.*;
public class Array {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int row;
int col;
System.out.println("row?");
row = sc.nextInt();
System.out.println("column?");
col = sc.nextInt();
int[][] array = new int[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
array[i][j] = ((int)(Math.random() * 10));
System.out.println("row " + i + " column " + j + " " + array[i][j]);
}
}
// this last line is highlighted in red
System.out.println();
}
}
REVISION: now with pretty print of table:
import java.util.*;
public class Array {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int row;
int col;
System.out.println("row?");
row = sc.nextInt();
System.out.println("column?");
col = sc.nextInt();
System.out.print("```|");
for (int j = 0; j < col; j++) {
System.out.print(" " + j);
}
System.out.println();
for (int j = 0; j <= col; j++) {
System.out.print("----");
}
System.out.println();
//if you want to print any header inforation or the values of row and column, do it here outside of the loops
int[][] array = new int[row][col];
for (int i = 0; i < row; i++) {
System.out.print(i + " |");
for (int j = 0; j < col; j++) {
array[i][j] = ((int)(Math.random() * 10));
System.out.print(" " + array[i][j]); //feel free to add vertical bars here if you wish
}
System.out.println();
}
}
}

Levenshtein distance in java outputting the wrong number

For my university assignment in java I have been asked to provide "extra analytics functions" I decided to use Levenshtein distance but I have an issue where the number outputted to the console is one less than the actual answer. So the distance between "cat" and "hat" should be 1 but it's displaying as 0
public class Levenshtein {
public Levenshtein(String first, String second) {
char [] s = first.toCharArray();
char [] t = second .toCharArray();
int Subcost = 0;
int[][] array = new int[first.length()][second.length()];
for (int i = 0; i < array[0].length; i++)
{
array[0][i] = i;
}
for (int j = 0; j < array.length; j++)
{
array [j][0]= j;
}
for (int i = 1; i < second.length(); i++)
{
for (int j = 1; j < first.length(); j++)
{
if (s[j] == t [i])
{
Subcost = 0;
}
else
{
Subcost = 1;
}
array [j][i] = Math.min(array [j-1][i] +1,
Math.min(array [j][i-1] +1,
array [j-1][i-1] + Subcost) );
}
}
UI.output("The Levenshtein distance is -> " + array[first.length()-1][second.length()-1]);
}
}
Apparently you're using the following algorithm:
https://en.wikipedia.org/wiki/Levenshtein_distance#Iterative_with_full_matrix
I think you were not too accurate with indices. I'm not sure where exactly the problem is, but here is a working version:
public int calculateLevenshteinDistance(String first, String second) {
char[] s = first.toCharArray();
char[] t = second.toCharArray();
int substitutionCost = 0;
int m = first.length();
int n = second.length();
int[][] array = new int[m + 1][n + 1];
for (int i = 1; i <= m; i++) {
array[i][0] = i;
}
for (int j = 1; j <= n; j++) {
array[0][j] = j;
}
for (int j = 1; j <= n; j++) {
for (int i = 1; i <= m; i++) {
if (s[i - 1] == t[j - 1]) {
substitutionCost = 0;
} else {
substitutionCost = 1;
}
int deletion = array[i - 1][j] + 1;
int insertion = array[i][j - 1] + 1;
int substitution = array[i - 1][j - 1] + substitutionCost;
int cost = Math.min(
deletion,
Math.min(
insertion,
substitution));
array[i][j] = cost;
}
}
return array[m][n];
}

Can not get precise results for the hourglass algorithm

Here is the link for definition of the hourglass problem:
https://www.hackerrank.com/challenges/30-2d-arrays
I wrote the following program:
package day11;
import java.util.Scanner;
public class Solution {
public static void main(String ... args){
Scanner scan = new Scanner(System.in);
int[][] arr = new int[6][6];
int maxHourGlassValue = 0;
int temp = 0;
int currMax = 0;
int k = 0, l = 0;
for(int i = 0 ; i < 6 ; i++){
for(int j = 0 ; j < 6 ; j++){
arr[i][j] = scan.nextInt();
}
}
for(int i = 1 ; i < 5 ; i++){
for(int j = 1 ; j < 5 ; j++){
if(maxHourGlassValue < currMax){
maxHourGlassValue = currMax;
}
}
}
System.out.println(maxHourGlassValue);
}
}
I could only run 6 out of 8 given test cases. What could possibly go wrong ????
Try this code, i did not write that code, i just copy it and modified it from here
import java.util.Scanner;
public class Test {
public static void main(String ... args){
Scanner scan = new Scanner(System.in);
int size = 6;
int[][] m = new int[size][size];
//numbers input
for(int i=0; i<size; i++)
{
for(int j=0; j<size; j++)
{
m[i][j] = scan.nextInt();
}
}
int temp = 0, MaxSum = -99999;
for (int i=0; i<size; ++i) {
for (int j=0; j<size; ++j) {
if (j+2 < size && i+2 < size) {
temp = m[i][j] + m[i][j+1] + m[i][j+2] + m[i+1][j+1] + m[i+2][j] + m[i+2][j+1] + m[i+2][j+2];
if (temp >= MaxSum) {
MaxSum = temp;
}
}
}
}
System.out.println(MaxSum);
}
}
Below is the code which will run successfully for all the test cases of hourglass problem.
public static void main(String[] args) {
try (Scanner scan = new Scanner(System.in)) {
int[][] arr = new int[6][6];
int maxHourGlassValue = -63;//Assigning (-9*7=)-63 which is the minimum possible value of "hourglass sum".
//Reading inputs.
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
arr[i][j] = scan.nextInt();
}
}
//Logic.
/**
* Index of both i and j will run from 1 to 4 (one less than n-1 where n = 6)
* So for each i and j iteration calculating the sum of hourglass.
*/
int iHGValueTemp = 0;
for (int i = 1; i < 5; i++) {
for (int j = 1; j < 5; j++) {
iHGValueTemp = arr[i][j] + /*Main element*/
arr[i - 1][j - 1] + arr[i - 1][j] + arr[i - 1][j + 1]+ /*Top three elements of main element.*/
arr[i + 1][j - 1] + arr[i + 1][j] + arr[i + 1][j + 1]; /*Bottom three elements of main element.*/
if (iHGValueTemp > maxHourGlassValue) {
maxHourGlassValue = iHGValueTemp;
}
}
}
//Output.
System.out.println(maxHourGlassValue);
}
}
I have written description within code in the comments only. Please refer to that and discuss with me if any doubt.

multidimensional array sorting by column in java

i need some help in this problem, there are three columns namely phase#, block# and lot#, i needed to sort the phase# in ascending order while the other two will be sorted accordingly to its phase#:
Problem:
Phase# 1-2-1-3-1-2-1
Block# 1-1-2-1-2-1-1
Lot# 1-2-2-2-3-1-1
What it should be like:
Phase# 1-1-1-1-2-2-3
Block# 1-2-2-1-1-1-1
Lot# 1-2-3-1-2-1-2
here's what I've got so far:
import java.io.*;
import java.util.*;
public class test{
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int x = 7;
int y = 3;
int[][] phase= new int[x][y];
int swap = 0, temp, i, min = 0;
phase[0][0] = 1;
phase[1][0] = 2;
phase[2][0] = 1;
phase[3][0] = 3;
phase[4][0] = 1;
phase[5][0] = 2;
phase[6][0] = 1;
phase[0][1] = 1;
phase[1][1] = 1;
phase[2][1] = 2;
phase[3][1] = 1;
phase[4][1] = 2;
phase[5][1] = 1;
phase[6][1] = 1;
phase[0][2] = 2;
phase[1][2] = 2;
phase[2][2] = 2;
phase[3][2] = 2;
phase[4][2] = 3;
phase[5][2] = 1;
phase[6][2] = 1;
System.out.println("UNSORTED: \n");
System.out.println("Phase#\tBlock#\tLot#");
for(i = 0; i < phase.length; i++){
System.out.print(phase[i][0] + "\t" + phase[i][1] + "\t"+phase[i][2]);
System.out.print("\n");
}
System.out.println("\nSORTED:\n");
for(i = 0; i <= phase.length- 1; i++){
min = i;
for(int a = i+1; a < phase.length; a++ ){
if(phase[a][0]<phase[a][0]){
temp=phase[i][0];
phase[i][0]=phase[a][0];
phase[a][0]=temp;
}
}
}
System.out.println("Phase#\tBlock#\tLot#");
for(int j = 0; j < phase.length; j++){
System.out.print(phase[j][0] + "\t" + phase[j][1] + "\t"+phase[j][2]);
System.out.print("\n");
}
}
}
Implement Comparator interface as (int [] left, int[] right) -> left[0]-right[0] and pass it to Arrays.sort() method:
Arrays.sort(phase, (int [] left, int[] right) -> left[0]-right[0]);

Categories

Resources