Skip to content
the poor coder

Hackerrank Print Pretty Solution

Poor Man
Published
Updated
Reading time
2 min read

Your manager gave you a text file with many lines of numbers to format and print. For each row of  space-separated doubles, format and print the numbers using the specifications in the Output Format section below.

Input Format

The first line contains an integer, , the number of test cases.
Each of the  subsequent lines describes a test case as  space-separated floating-point numbers: , , and , respectively.

Constraints

  • Each number will fit into a double.

Output Format

For each test case, print  lines containing the formatted , , and , respectively. Each , , and  must be formatted as follows:

  1. : Strip its decimal (i.e., truncate it) and print its hexadecimal representation (including the  prefix) in lower case letters.
  2. : Print it to a scale of  decimal places, preceded by a  or  sign (indicating if it's positive or negative), right justified, and left-padded with underscores so that the printed result is exactly  characters wide.
  3. : Print it to a scale of exactly nine decimal places, expressed in scientific notation using upper case.

Sample Input

Text
1  
100.345 2006.008 2331.41592653498

Sample Output

Text
0x64             
_______+2006.01  
2.331415927E+03

Explanation

For the first line of output,  (in reverse, ).
The second and third lines of output are formatted as described in the Output Format section.

Solution in cpp#

Approach 1.

Python
		/* Enter your code here */
        //cout << "Raw Input: " << A << " " << B << " " << C << endl;
        
        printf("%#lx\n",(long int)A);
        printf("%.*s%+.2f\n",15 - to_string((int)B).length() - 4,"_______________",B);
        printf("%.9E\n",C);

Approach 2.

Python
		std::cout << nouppercase << setw(0) << showbase << hex << (int64_t)A << endl;
		std::cout << right << setw(15) << setprecision(2) << showpos << fixed << setfill('_') << B <<endl;
		std::cout << uppercase << setprecision(9) << noshowpos << scientific << C << endl;

Approach 3.

Python
		/* Enter your code here */  

    cout <<showbase << nouppercase << left << hex << long(A) << endl;
    cout << setfill('_') << setw(15) << right << showpos << fixed << setprecision(2) << B << endl;
    cout << noshowpos << scientific << setprecision(9) << uppercase << C << endl;

Keep going

2020

Hackerrank Deque-STL Solution

Double ended queue or Deque(part of C++ STL) are sequence containers with dynamic sizes that can be expanded or contracted on both ends (either its front or its back). The member functions of deque that are mainly used…

2020

Hackerrank Maps-STL Solution

Maps are a part of the C++ STL.Maps are associative containers that store elements formed by a combination of a key value and a mapped value, following a specific order.The mainly used member functions of maps are: Map…

2020

Hackerrank Sets-STL Solution

Sets are a part of the C++ STL. Sets are containers that store unique elements following a specific order. Here are some of the frequently used member functions of sets: Declaration: Size: Insert: Erasing an element…

ESC