Algorithm


Problem Name: Python - Text Alignment

Problem Link: hhttps://www.hackerrank.com/challenges/text-alignment/problem?isFullScreen=true

In this HackerRank Functions in PYTHON problem solution,

In Python, a string of text can be aligned left, right and center.

 

.ljust(width)

 

This method returns a left aligned string of length width.

 

>>> width = 20
>>> print 'HackerRank'.ljust(width,'-')
HackerRank----------  

 


 

.center(width)

 

This method returns a centered string of length width.

 

>>> width = 20
>>> print 'HackerRank'.center(width,'-')
-----HackerRank-----

 


 

.rjust(width)

 

This method returns a right aligned string of length width.

 

>>> width = 20
>>> print 'HackerRank'.rjust(width,'-')
----------HackerRank

 


 

Task

 

You are given a partial code that is used for generating the HackerRank Logo of variable thickness.
Your task is to replace the blank (______) with rjust, ljust or center.

Input Format

A single line containing the thickness value for the logo.

 

Constraints

The thickness must be an odd number.

0 < thickness < 50

Output Format

Output the desired logo.

Sample Input

5

Sample Output

    H    
   HHH   
  HHHHH  
 HHHHHHH 
HHHHHHHHH
  HHHHH               HHHHH             
  HHHHH               HHHHH             
  HHHHH               HHHHH             
  HHHHH               HHHHH             
  HHHHH               HHHHH             
  HHHHH               HHHHH             
  HHHHHHHHHHHHHHHHHHHHHHHHH   
  HHHHHHHHHHHHHHHHHHHHHHHHH   
  HHHHHHHHHHHHHHHHHHHHHHHHH   
  HHHHH               HHHHH             
  HHHHH               HHHHH             
  HHHHH               HHHHH             
  HHHHH               HHHHH             
  HHHHH               HHHHH             
  HHHHH               HHHHH             
                    HHHHHHHHH 
                     HHHHHHH  
                      HHHHH   
                       HHH    
                        H 

 

 

 

Code Examples

#1 Code Example with Python Programming

Code - Python Programming


t = int(input()) # thickness
assert t % 2 == 1
total_width = t * 6
ch = 'H'         # filler

def cone(aligned_left: bool = False, pointing_down: bool = False):
    width = t * 2 - 1
    layer_gen = range(t)
    align = '>' if aligned_left else '<'
    if pointing_down:
        layer_gen = reversed(layer_gen)
        
    for layer in layer_gen:
        unaligned_layer = f"{ch * (1 + layer * 2): ^{width}}"
        print(f"{unaligned_layer:{align}{total_width - 1}}")

def pillars():
    for layer in range(t + 1):
        print(f"{ch * t: ^{t*2}}{ch * t:^{total_width}}")
        
def middle_belt():
    for i in range((t + 1) // 2):
        print(f"{ch * t * 5: ^{total_width}}")

cone()                                      #Top Cone
pillars()                                   #Top Pillars
middle_belt()                               #Middle Belt
pillars()                                   #Bottom Pillars
cone(aligned_left=True, pointing_down=True) #Bottom Cone
Copy The Code & Try With Live Editor
Advertisements

Demonstration


Previous
[Solved] Weather Observation Station 16 in SQL solution in Hackerrank
Next
[Solved] Population Census in SQL solution in Hackerrank