Text Alignment in Python

Text Alignment in Python

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} In Python, a string of text can be aligned left, right and centre. This method returns a left-aligned string of length width.

>>> width = 20 >>> print ‘HackerRank’.ljust(width,’-‘) HackerRank———- {codeBox}

This method returns a centred string of length width.

>>> width = 20 >>> print ‘HackerRank’.center(width,’-‘) —–HackerRank—– {codeBox}

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

>>> width = 20 >>> print ‘HackerRank’.rjust(width,’-‘) ———-HackerRank {codeBox}

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. A single line containing the thickness value for the logo. The thickness must be an odd number.

5 {codeBox}

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}

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
No Comments

Sorry, the comment form is closed at this time.