Record a written test in grape city

Title Source:

Title website: https://www.grapecity.com.cn/career/challenge

Title Description:

Title: Data Visualization

Programming language: Unlimited

Title Description: there is a saying: "the text is not as good as the table, and the table is not as good as the picture". It vividly describes the different efficiency and experience that charts bring to the receiver when conveying information. Therefore, with the continuous improvement of computer computing power, data scale and decision-making needs, the application of data visualization is becoming more and more common.

The scope of data visualization is very wide, involving many concepts and fields such as data acquisition, processing, modeling, graphics and human-computer interaction. If you want to get started faster and get a better experience, it is a wiser choice to use professional tools and services such as DragonFly BI.

Today, we personally realize simple data visualization through a simplified proposition. Write a program to output a histogram composed of characters for a given set of data and requirements.

input

In the first row, an integer N (1 < = N < = 20) represents the number of entries of this group of data.
The second line, two strings, is used to indicate the sorting method of data displayed on the histogram. The first string is "Name" or "Value", indicating that the sorting is based on the Name or Value of the data entry; The second string is "ASC" or "DESC", indicating ascending or descending order.
The next N lines, each containing a string s and a number V, separated by spaces, represent a piece of data. S is the name of the data entry, which only contains lowercase letters. V is the corresponding value, which is an integer, (0 < = V < = 1000000)

output

Chart outline corner symbol:

  • "┌"(\u250c)
  • "┐"(\u2510)
  • "└"(\u2514)
  • "┘"(\u2518)

Horizontal and vertical lines in the chart:

  • "─"(\u2500)
  • "│"(\u2502)

Various cross lines in the chart:

  • "├"(\u251c)
  • "┤"(\u2524)
  • "┬"(\u252c)
  • "┴"(\u2534)
  • "┼"(\u253c)

Characters used to spell columns:

  • "█"(\u2588)

Spaces in chart:

  • " "(\u0020)

The width of the name area in the chart is determined by the maximum length of the name in this group of data. All names are aligned to the right. The maximum length of the column in the chart is 20. The length of each column is obtained by multiplying the ratio of the corresponding data of the column and the maximum value in this group of data (this value must be greater than 0) by 20, and the part less than one grid is rounded off.

Input example

3
Value DESC
apple 5
pen 3
pineapple 10

Output example

Solution:

import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;

public class test {
    public static void main(String []args) {
    	Scanner sc = new Scanner(System.in);
    	int num = sc.nextInt();
    	String way = sc.next();
    	String seq = sc.next();
    	
    	String name;
    	int value;
    	int maxLength = 0;
    	int maxNum = 0;
    	
    	Object[] arr = new Object[num];
    	HashMap<Object, Object> map = new HashMap<>();
    	
    	for (int i=0; i<num; i++) {
    		name = sc.next();
    		value = sc.nextInt();
    		if (name.length() > maxLength)
    			maxLength = name.length();
    		if (value > maxNum)
    			maxNum = value;
    		if (way.equals("Name")) {
    			map.put(name, value);
    			arr[i] = name;
    		} else {
    			map.put(value, name);
    			arr[i] = value;
    		}
    	}
    	
    	//sort
    	Arrays.sort(arr);
    	if (seq.equals("DESC")) {
    		int l = arr.length;
    		Object temp=0;
    		for (int i=0; i<l/2; i++) {
    			temp = arr[i];
    			arr[i] = arr[l-i-1];
    			arr[l-i-1] = temp;
    		}
    	}
    	
    	//Output first line
    	System.out.print('\u250c');
    	for (int i=0; i<maxLength; i++)
    		System.out.print('\u2500');
    	System.out.print('\u252c');
    	for (int i=0; i<20; i++)
        	System.out.print('\u2500');
    	System.out.println('\u2510');
    	
    	//Output the following lines, in groups of two lines
    	for (int k=0; k<num; k++) {
    		//Get the row data
    		if (way.equals("Name")) {
    			name = (String)arr[k];
    			value = (int)map.get(arr[k]);
    		} else {
    			value = (int)arr[k];
    			name = (String)map.get(arr[k]);
    		}
    		//Calculate column length
    		int q = (int)((double)value/maxNum*20);
    		//Output with data row
    		System.out.print('\u2502');
    		for (int i=1; i<=maxLength-name.length(); i++) {
    			System.out.print('\u0020');
    		}
    		System.out.print(name);
    		System.out.print('\u2502');
    		for (int i=1; i<=q; i++) {
        		System.out.print('\u2588');
    		}
    		for (int i=q; i<20; i++) {
    			System.out.print('\u0020');
    		}
    		System.out.println('\u2502');
    		//Output border row
    		if (k == num-1) {
    			System.out.print('\u2514');
    			for (int i=0; i<maxLength; i++) {
    				System.out.print('\u2500');
    			}
    			System.out.print('\u2534');
    			for (int i=0; i<20; i++) {
    				System.out.print('\u2500');
    			}
    			System.out.println('\u2518');
    		} else {
    			System.out.print('\u251c');
    			for (int i=0; i<maxLength; i++) {
    				System.out.print('\u2500');
    			}
    			System.out.print('\u253c');
    			for (int i=0; i<20; i++) {
    				System.out.print('\u2500');
    			}
    			System.out.println('\u2524');
    		}
    	}
    }
}

Operation screenshot:

Keywords: Java HashMap unicode

Added by jellis on Sun, 05 Sep 2021 03:56:52 +0300