0

Hi I have an array of numbers and a K number which is the number of numbers on each line. I want to display numbers like this:

+---+---+
|123|  5|
+---+---+
|  6| 14|
+---+---+

so array = [123, 5, 6, 14] and K = 2, and there are pluses and minuses and pipes. The number of minuses is equal to number of digits in biggest number.

How can I write this in Javascript? I have:

let res = ""
let max = Math.max(null, A)
let digits = max.toString().length;

function line(numPlus, numMinus) {
 let string  = "";
for (let i = 0; i  < numPlus; i++) {
  string += "+"
for(let j = 0; j < numMinus; j++) {
 string += "-"
}
string += "+"
}
}

line(K+1, digits);

2
  • Hi, perhaps loop over the array and print a top and bottom separator line for each iteration Commented Sep 30, 2020 at 22:15
  • Given your example I'm not sure the need for K - is it not possible to use the length of the array? Commented Sep 30, 2020 at 22:37

4 Answers 4

2
let array = [123, 5, 6, 14];
let k = 2;
let m = Math.max(...(array.map(e => e.toString().length)));
let t = new Array(k+1).fill("+").join("-".repeat(m));
let pa = array.map(e => e.toString().padStart(m, " "));
let str = "";
for (let i = 0; i < array.length/k; i++) {
  let s = pa.slice(k*i, k*i+k);
  while (s.length < k) s.push(" ".repeat(m));
  str += t + "\n|" + s.join("|") + "|\n";
}
console.log(str + t);
Sign up to request clarification or add additional context in comments.

2 Comments

The only suggestion I might add is to calculate the max using array.reduce((a, b) => a > b ? a : b, -Infinity).toString().length. There might be speed improvements (although I'm not certain) to using this method over the combination of .map and Math.max
thanks! i edited my post so K= 2 and there are 4 elements in the array. how would you do that, if there are 2 rows?
1

Something Like This Maybe

let numbers = [100,25555,60,21,456,789,231,54,2,4,13,6,579,4562156, -5, 0.5, -1.5]
let maxLength = 0;

for(let i = 0; i < numbers.length; i++){
    if(maxLength < numbers[i].toString().length){
        maxLength = numbers[i].toString().length
    }
}


function printDivider(cells){
    let row = '+';
    for(let i = 0; i < cells; i++){
        for(let j = 0; j < maxLength; j++){
            row+= '-'
        }
        row += '+';
    }
    console.log(row)
}



function printNumberRow(cells){
    let row = '|'
    for(let i = 0; i< cells; i++){
        if(numbers.length !== 0){
            let currentNumber = numbers.shift().toString();
            currentNumber = currentNumber.padStart(maxLength, ' ');
            row+=currentNumber;
        }
        else{
            row+= ''.padStart(maxLength, ' ');
        }
        row += '|';
    }
    console.log(row);
}

function printGraph(numbersPerRow){
    let maxRows = Math.round(numbers.length / numbersPerRow)
    if(numbers.length % numbersPerRow >= 1){
        maxRows++;
    }
    for(let i = 0; i < maxRows; i++){
        printDivider(numbersPerRow)
        printNumberRow(numbersPerRow)
        printDivider(numbersPerRow)
    }

}


printGraph(5)


+-------+-------+-------+-------+-------+
|    100|  25555|     60|     21|    456|
+-------+-------+-------+-------+-------+
+-------+-------+-------+-------+-------+
|    789|    231|     54|      2|      4|
+-------+-------+-------+-------+-------+
+-------+-------+-------+-------+-------+
|     13|      6|    579|4562156|     -5|
+-------+-------+-------+-------+-------+
+-------+-------+-------+-------+-------+
|    0.5|   -1.5|       |       |       |
+-------+-------+-------+-------+-------+

Comments

1

Here is the Java solution. I am just formatting @user17315860 answer. His answer is correct but not at all ridable.

import java.util.ArrayList;

class Solution {
    static void solution(int[] A, int K) {
        int maxLength = 0;

        ArrayList<Integer> arr = new ArrayList<>();
        for (int x : A) {
            arr.add(x);
        }
        for (int i = 0; i < A.length; i++) {
            if (maxLength < Integer.toString(A[i]).length()) {
                maxLength = Integer.toString(A[i]).length();
            }
        }
        int completeRow = A.length / K;
        int iRow = A.length % K;
        if (completeRow > 0) {
            printDivider(K, maxLength, arr);
        } else {
            printDivider(iRow, maxLength, arr);
        }
        System.out.println();
        for (int i = 0; i < completeRow; i++) {
            printNumberRow(K, maxLength, arr);
            System.out.println();
            printDivider(K, maxLength, arr);
            System.out.println();
        }
        if (iRow > 0) {
            printNumberRow(iRow, maxLength, arr);
            System.out.println();
            printDivider(iRow, maxLength, arr);
            System.out.println();
        }

    }

    static void printDivider(int cells, int maxLength, ArrayList<Integer> A) {
        String row = "+";
        for (int i = 0; i < cells; i++) {
            for (int j = 0; j < maxLength; j++) {
                row += "-";
            }
            row += "+";
        }
        System.out.print(row);
    }

    static public void printNumberRow(int cells, int maxLength, ArrayList<Integer> A) {
        String row = "|";
        for (int i = 0; i < cells; i++) {
            if (A.size() != 0) {
                int num = A.get(0);
                A.remove(0);
                // String currentNumber = Integer.toString(num);
                String aa = "";
                int intlength = Integer.toString(num).length();
                for (int j = 0; j < maxLength - intlength; j++) {
                    aa = aa + " ";
                }
                aa = aa + Integer.toString(num);
                row += aa;
            }
            // else{
            // row+= ''.padStart(maxLength, ' ');
            // }
            row += '|';
        }
        System.out.print(row);
    }

    public static void main(String[] args) {
        int[] A = new int[] { 4, 35, 80, 123, 12345, 44, 8, 5, 24, 3,22,35 };
        solution(A, 4);
    }
}

Comments

0

//A simple and effective solution for exact result import java.util.*;

class Solution { public void solution(int[] A, int K) {

int maxLength = 0;

ArrayList<Integer> arr = new ArrayList<>();
for(int x :A){
    arr.add(x);
}
for(int i = 0; i < A.length; i++){
    if(maxLength < Integer.toString(A[i]).length()){
        maxLength = Integer.toString(A[i]).length();
    }
}
    int completeRow = A.length / K;
    int iRow = A.length % K;
    if(completeRow>0){
        printDivider(K, maxLength,arr);
    }
    else{
        printDivider(iRow,maxLength,arr);
    }
    System.out.println();
    for(int i=0;i<completeRow;i++){
        printNumberRow(K, maxLength,arr);
        System.out.println();
        printDivider(K, maxLength,arr);
        System.out.println();
    }
    if(iRow > 0){
        printNumberRow(iRow, maxLength,arr);
        System.out.println();
        printDivider(iRow, maxLength,arr);
        System.out.println();
    }

}

void printDivider(int cells, int maxLength ,ArrayList<Integer> A){
String row = "+";
for(int i = 0; i < cells; i++){
    for(int j = 0; j < maxLength; j++){
        row+="-";
    }
    row += "+";
}
System.out.print(row);

}

public void printNumberRow(int cells,int maxLength, ArrayList A){ String row = "|";

for(int i = 0; i< cells; i++){
    if(A.size() != 0){
        int num=A.get(0);
        A.remove(0);
        // String currentNumber = Integer.toString(num);
        String aa = "" ; 
        int intlength = Integer.toString(num).length() ; 
        for(int j = 0  ; j < maxLength-intlength ; j++ ) {
            aa = aa+" " ;
        }
        aa =aa +  Integer.toString(num) ;
        row+=aa;
    }
    // else{
    //     row+= ''.padStart(maxLength, ' ');
    // }
    row += '|';
}
System.out.print(row);

}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.