Text Alignment in Python - HackerRank Solution

Text Alignment - Python HackerRank Solution

Hello Friends, How are you? Today I am going to solve the HackerRank Text Alignment Problem in Python with a very easy explanation. In this article, you will get one or more approaches to solving this problem. So let's start-

{tocify} $title={Table of Contents}

Default Arguments Python HackerRank Solution

HackerRank Python Text Alignment Solution - Problem Statement

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

.ljust(width)

This method returns a left-aligned string of length width.

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

.center(width)

This method returns a centred string of length width.

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

.rjust(width)

This method returns a right-aligned string of length width.

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

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 0

5 {codeBox}

Sample Output 0

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 {codeBox}

Python Text Alignment - Hacker Rank Solution

Approach I: Text Alignment HackerRank Python Solution

# ========================
#       Information
# ========================

# Name: Text Alignment in Python HackerRank
# Direct Link: https://www.hackerrank.com/challenges/text-alignment/problem
# Difficulty: Easy
# Max Score: 10
# Language: Pypy 3

# ========================
#         Solution Start
# ========================

#Text Alignment in Python - Hacker Rank Solution
   
# Enter your code here. Read input from STDIN. Print output to STDOUT

#Replace all ______ with rjust, ljust or center. 

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

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

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

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

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

#Bottom Cone
# replace ______ To rjust |  ______ To ljust |  ______ To rjust
for i in range(thickness):
    print(((c*(thickness-i-1)).rjust(thickness)+c+(c*(thickness-i-1)).ljust(thickness)).rjust(thickness*6))
   
#Text Alignment in Python - Hacker Rank Solution END
# MyEduWaves


Disclaimer: The above Problem ( Python Text Alignment ) is generated by Hackerrank but the Solution is Provided by MyEduWaves. This tutorial is only for Educational and Learning purposes. Authority if any queries regarding this post or website fill out the contact form.

I hope you have understood the solution to this HackerRank Problem. All these three solutions will pass all the test cases. Now visit Python Text Alignment Hackerrank Problem and try to solve it again.

All the Best!

Post a Comment

Previous Post Next Post