Friday 22 March 2024

Python pattern challenge - Day 5

 



def print_pattern():

    for i in range(5, 0, -1):

        for j in range(i, 0, -1):

            print(chr(64 + j), end="")

        print()

print_pattern()


Solution and Explanation: 


def print_pattern()::

This line defines a function named print_pattern().
for i in range(5, 0, -1)::

This outer loop iterates over the range from 5 down to 1. The third argument -1 in range() means that it decrements by 1 in each iteration.
for j in range(i, 0, -1)::

This inner loop iterates over the range from the current value of i down to 1. So, for each iteration of the outer loop, this inner loop prints characters in decreasing order.
print(chr(64 + j), end=""):

Inside the inner loop, chr(64 + j) converts the integer ASCII value 64 + j to the corresponding character. Since we're starting from 'E' (ASCII value 69) and decrementing, 64 + j gives the ASCII value of the characters 'A' to 'E'.
end="" parameter is used to prevent print() from adding a newline character after each print, so the characters are printed horizontally.
print():

This print() statement is outside the inner loop. It's used to move to the next line after each inner loop iteration, creating a new line for the next set of characters.
print_pattern():

This line outside the function definition calls the print_pattern() function, causing the pattern to be printed according to the code within the function.

0 Comments:

Post a Comment

Popular Posts

Categories

AI (27) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (115) C (77) C# (12) C++ (82) Course (62) Coursera (178) coursewra (1) Cybersecurity (22) data management (11) Data Science (91) Data Strucures (6) Deep Learning (9) Django (6) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (5) flutter (1) FPL (17) Google (19) Hadoop (3) HTML&CSS (46) IBM (25) IoT (1) IS (25) Java (92) Leet Code (4) Machine Learning (44) Meta (18) MICHIGAN (5) microsoft (3) Pandas (3) PHP (20) Projects (29) Python (747) Python Coding Challenge (202) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (40) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses