Thursday, 21 March 2024

Python pattern challenge - Day 4

 



n = 7

d = n // 2 + 1


for x in range(1, n + 1):

    for y in range(1, n + 1):

        if x == n // 2 + 1 or y == d:

            print("*", end="")

        else:

            print(" ", end="")

    if x <= n // 2:

        d += 1

    else:

        d -= 1

    print()


Let's break down the code step by step:

n = 7: This line initializes a variable n with the value 7. This value represents the size of the pattern, specifically the number of rows and columns.

d = n // 2 + 1: This line calculates the starting position for printing the asterisks in each row. Since the asterisks form a cross pattern, the distance from the left edge to the vertical center of the cross (d) is set to half of the size of the pattern plus 1. This calculation ensures that the cross is properly centered horizontally.

The nested loops:

for x in range(1, n + 1):: This outer loop iterates over each row of the pattern.

for y in range(1, n + 1):: This inner loop iterates over each column of the pattern within the current row.

Inside the nested loops, there's an if-else statement:

if x == n // 2 + 1 or y == d:: This condition checks if the current position (x, y) is on the horizontal center line (x == n // 2 + 1) or the vertical center line (y == d). If the condition is true, an asterisk is printed.

else:: If the condition is false (i.e., the current position is not on the center lines), a space is printed.

After printing each row, there's an adjustment to the variable d:

if x <= n // 2:: This condition checks if we are still in the upper half of the cross. If true, it means we need to move the vertical center (d) downwards for the next row.

else:: If we are in the lower half of the cross, we need to move the vertical center (d) upwards for the next row.

The print() statement at the end of the outer loop prints a newline character, moving to the next row in the pattern.

Overall, this code generates a cross pattern made of asterisks (*) with the specified size (n) and ensures that the cross is properly centered both horizontally and vertically.

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (53) AI (34) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (173) C (77) C# (12) C++ (82) Course (67) Coursera (226) Cybersecurity (24) data management (11) Data Science (128) Data Strucures (8) Deep Learning (20) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (59) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (3) Pandas (4) PHP (20) Projects (29) Python (932) Python Coding Challenge (363) Python Quiz (25) Python Tips (2) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (3) Software (17) SQL (42) UX Research (1) web application (8) Web development (2) web scraping (2)

Followers

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