Text Alignment HackerRank Solution

This Python program is designed to demonstrate text alignment using Python's built-in string functions. The user provides an integer input that determines the thickness of the pattern. The pattern consists of 'H' characters arranged in a specific alignment to form a shape.

Secure your next interview 🎯

CodeRankGPT is a tool powered by GPT-4 that quietly assists you during your coding interview, providing the solutions you need.
In real-time and absolutely undetectable 🥷

Here is the Text Alignment HackerRank Solution HackerRank solution using CodeRankGPT:

The solution works by using Python's built-in string functions such as 'rjust', 'ljust', and 'center' to align the 'H' characters. The program first creates the top cone, top pillars, middle belt, bottom pillars, and bottom cone of the shape in that order. For each part, a for loop is used to print the appropriate number of 'H' characters in the correct alignment. The thickness of the shape is determined by the user's input.


thickness = int(input())
c = "H"

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

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

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

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

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

If you have a HackerRank coding test coming up, you can use CodeRankGPT to your advantage. It will assist you during your interview and help ensure you get the job.

AI is here now, and other candidates might be using it to get ahead and win the job. 🧐

Built on Unicorn Platform