Hackerank | Text Alignment | String Formatting

admin
By -
0

 HackerRank Text Alignment | String Formatting 

In this question already provided the code which print hackerrank logo as output we just need to put  alignment func to  placeholder ------- and its done

its use string function alignment and  padding stuff

ljust ; left just

rjust ; right just

center :  center

On First look the code look complicated and you do not figure out but its easy  ,if you  ever before tried those star patterns based question on your college you can solve it with ease

Do not look the whole code and Do not try to  print or solve whole logo patterns 

Try each code block or for loops and try to  print each  part of logo one by one

First Top Cone ,Top Pillars Left and right  and belt here just using center  and then again bottom pillars

with left and right

if your figure out this means your have solved  half of problem and you can print half logo

the main issue was bottom cone that present in right-side this part is little tricky where we have to  carefully look it


Top Cone

Top Cone and Both Top Pillars

Top Cone ,Left and Right Top Pillars and Middle Belt

Top Cone, Top Pillars, Belt , Bottom Pillars



Problem: https://www.hackerrank.com/challenges/text-alignment/problem

code:

#Replace all ______ with rjust, ljust or center.

thickness = int(input()) #This must be an odd number
c = 'H'

#Top Cone
for i in range(thickness):
    print((c*i).rjust(thickness-1)+c+(c*i).ljust(thickness-1))

#Top Pillars
for i in range(thickness+1):
    print((c*thickness).center(thickness*2)+(c*thickness).center(thickness*6))

#Middle Belt
for i in range((thickness+1)//2):
    print((c*thickness*5).center(thickness*6))    

#Bottom Pillars
for i in range(thickness+1):
    print((c*thickness).center(thickness*2)+(c*thickness).center(thickness*6))    

#Bottom Cone
for i in range(thickness):
    print(((c*(thickness-i-1)).rjust(thickness)+c+(c*(thickness-i-1)).ljust(thickness)).rjust(thickness*6))


Output:



If you find this solution effective please leave us comments , your comment will motivate use to  more informative blogs for future


Post a Comment

0Comments

Please Select Embedded Mode To show the Comment System.*