Tuesday, 19 March 2024

Python pattern challenge - Day 3

 

# Method 1: Using a loop

def print_sequence_loop(n):

    for i in range(n+1):

        print(f"{i}{'*' * i}")


# Method 2: Using list comprehension

def print_sequence_list_comprehension(n):

    sequence = [f"{i}{'*' * i}" for i in range(n+1)]

    print('\n'.join(sequence))


# Method 3: Using a generator function

def generate_sequence(n):

    for i in range(n+1):

        yield f"{i}{'*' * i}"


def print_sequence_generator(n):

    sequence = generate_sequence(n)

    for item in sequence:

        print(item)


# Testing the functions

n = 5


print("Using loop:")

print_sequence_loop(n)

print("\nUsing list comprehension:")

print_sequence_list_comprehension(n)

print("\nUsing generator function:")

print_sequence_generator(n)



Let's break down each part of the code:


Method 1: Using a loop

This method uses a loop to generate and print each element of the sequence.

def print_sequence_loop(n):
    for i in range(n+1):
        print(f"{i}{'*' * i}")
  • print_sequence_loop is a function that takes an integer n as input.
  • It iterates over the range from 0 to n (inclusive) using a for loop.
  • Inside the loop, it prints a string composed of the current number i followed by a number of asterisks (*) corresponding to the value of i.

Method 2: Using list comprehension

This method uses list comprehension to generate the sequence and then prints it.

def print_sequence_list_comprehension(n):
    sequence = [f"{i}{'*' * i}" for i in range(n+1)]
    print('\n'.join(sequence))
  • print_sequence_list_comprehension is a function that takes an integer n as input.
  • It uses list comprehension to generate a list where each element is a string composed of the current number i followed by a number of asterisks (*) corresponding to the value of i.
  • It then joins the elements of the list with newline characters ('\n') and prints the resulting string.

Method 3: Using a generator function

This method uses a generator function to lazily generate the sequence, and then prints it.

def generate_sequence(n):
    for i in range(n+1):
        yield f"{i}{'*' * i}"

def print_sequence_generator(n):
    sequence = generate_sequence(n)
    for item in sequence:
        print(item)
  • generate_sequence is a generator function that yields each element of the sequence lazily. It takes an integer n as input.
  • Inside the function, it iterates over the range from 0 to n (inclusive) using a for loop, yielding a string composed of the current number i followed by a number of asterisks (*) corresponding to the value of i.
  • print_sequence_generator is a function that takes an integer n as input.
  • It creates a generator object sequence by calling generate_sequence(n).
  • It then iterates over the elements of the generator using a for loop, printing each element.

0 Comments:

Post a Comment

Popular Posts

Categories

AI (33) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (146) C (77) C# (12) C++ (82) Course (67) Coursera (198) Cybersecurity (24) data management (11) Data Science (106) Data Strucures (8) Deep Learning (13) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (21) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (46) Meta (18) MICHIGAN (5) microsoft (4) Nvidia (1) Pandas (3) PHP (20) Projects (29) Python (893) Python Coding Challenge (285) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

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