From 52452ab756450471665adb546f74dd6a4e68fc8a Mon Sep 17 00:00:00 2001 From: Kiran Bade Date: Sat, 31 Aug 2024 00:48:40 +0530 Subject: [PATCH 1/3] Array Declaration in Python --- PythonArrays/ArrayDeclaration.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 PythonArrays/ArrayDeclaration.py diff --git a/PythonArrays/ArrayDeclaration.py b/PythonArrays/ArrayDeclaration.py new file mode 100644 index 0000000..717e730 --- /dev/null +++ b/PythonArrays/ArrayDeclaration.py @@ -0,0 +1,13 @@ +def arrayDeclare(n): + arr = [0] * n + for i in range(n): + num = int(input('Enter array element : ')) + arr[i] = num + print(arr[0]) + print(arr[1]) + print(arr[2]) + +def main(): + n = 3 + arrayDeclare(n) +main() \ No newline at end of file From b370d85bce7caa487a0bb2202bc506f5a9407eb2 Mon Sep 17 00:00:00 2001 From: Kiran Bade Date: Sat, 27 Sep 2025 14:56:28 +0530 Subject: [PATCH 2/3] Add Python implementations for pattern problems --- .../PatternPart1/CharacterPattern.py | 53 +++++++++++++++ PythonPatterns/PatternPart1/HalfPyramid.py | 49 +++++++++++++ PythonPatterns/PatternPart1/InvertedStar.py | 51 ++++++++++++++ PythonPatterns/PatternPart2/ButterFly.py | 68 +++++++++++++++++++ PythonPatterns/PatternPart2/DiamondPattern.py | 59 ++++++++++++++++ PythonPatterns/PatternPart2/FloydTriangle.py | 53 +++++++++++++++ 6 files changed, 333 insertions(+) create mode 100644 PythonPatterns/PatternPart1/CharacterPattern.py create mode 100644 PythonPatterns/PatternPart1/HalfPyramid.py create mode 100644 PythonPatterns/PatternPart1/InvertedStar.py create mode 100644 PythonPatterns/PatternPart2/ButterFly.py create mode 100644 PythonPatterns/PatternPart2/DiamondPattern.py create mode 100644 PythonPatterns/PatternPart2/FloydTriangle.py diff --git a/PythonPatterns/PatternPart1/CharacterPattern.py b/PythonPatterns/PatternPart1/CharacterPattern.py new file mode 100644 index 0000000..7422c88 --- /dev/null +++ b/PythonPatterns/PatternPart1/CharacterPattern.py @@ -0,0 +1,53 @@ +# Character Pyramid: Prints a half pyramid of consecutive letters starting from 'A' + +def character_pattern(n): + + """ + Prints a character pattern in the form of a half pyramid. + + """ + ch = ord('A') # get ASCII code of 'A' + + # Outer loop for number of rows + for i in range(1, n + 1): + + # Inner loop for printing characters in each row + for j in range(1, i + 1): + + print(chr(ch), end="") # Convert ASCII to character and print + ch += 1 # Move to the next character in sequence + + print() # Print a newline after each row + +def main(): + """ + Main function to take user input and handle errors. + """ + + try: + # Take input from user + n = int(input("Enter Number : ")) + character_pattern(n) + + except ValueError: + # Handle non-integer inputs gracefully + print("Please enter a valid integer.") + +main() + + +''' +Output-: + +Enter Number : 4 +A +BC +DEF +GHIJ + +#Cross Check with Invalid Integer-: + +Enter Number : g +Please enter a valid integer. + +''' diff --git a/PythonPatterns/PatternPart1/HalfPyramid.py b/PythonPatterns/PatternPart1/HalfPyramid.py new file mode 100644 index 0000000..c945128 --- /dev/null +++ b/PythonPatterns/PatternPart1/HalfPyramid.py @@ -0,0 +1,49 @@ +# Half Pyramid with Row Numbers: Prints a half pyramid where each row shows numbers from 1 to row number + +def half_pyramid(n): + """ + Prints a half pyramid with numbers from 1 to the current row number in each row. + """ + + # Outer loop for rows + for i in range(1, n + 1): + # Inner loop for printing numbers in each row + + for j in range(1, i + 1): + print(j, end="") # Print numbers in the same line without space + + print() # Print newline after each row + + +def main(): + """ + Main function to take user input and handle invalid cases. + """ + try: + # Take integer input from user + n = int(input("Enter number : ")) + half_pyramid(n) + except ValueError: + # Handle non-integer inputs gracefully + print("Please enter a valid integer.") + + +# Program execution starts here +main() + +''' + +Output -: +Enter number : 5 +1 +12 +123 +1234 +12345 + +#Cross check with Invalid Value + +Enter number : # +Please enter a valid integer. + +''' \ No newline at end of file diff --git a/PythonPatterns/PatternPart1/InvertedStar.py b/PythonPatterns/PatternPart1/InvertedStar.py new file mode 100644 index 0000000..77bca2e --- /dev/null +++ b/PythonPatterns/PatternPart1/InvertedStar.py @@ -0,0 +1,51 @@ +# Inverted Star Pattern: Prints a half pyramid of stars in inverted order + +def inverted_star(n): + """ + Prints an inverted star pattern. + + """ + + # Outer loop for number of rows + for i in range(1, n + 1): + + # Inner loop for printing stars in each row + for j in range(1, n - i + 2): + print("*", end="") # Print star without newline + + print() # Print newline after each row + + +def main(): + """ + Main function to take user input and handle invalid cases. + """ + + try: + # Take integer input from user + n = int(input("Enter number to print inverted star pattern: ")) + inverted_star(n) + + except ValueError: + # Handle non-integer inputs gracefully + print("Please enter a valid integer.") + +main() + + +''' + +output-: + +Enter number to print inverted star pattern: 5 +***** +**** +*** +** +* + +#Cross check with Invalid Integer-: +Enter number to print inverted star pattern: @ +Please enter a valid integer. + +''' \ No newline at end of file diff --git a/PythonPatterns/PatternPart2/ButterFly.py b/PythonPatterns/PatternPart2/ButterFly.py new file mode 100644 index 0000000..e4edf57 --- /dev/null +++ b/PythonPatterns/PatternPart2/ButterFly.py @@ -0,0 +1,68 @@ +# Butterfly Pattern: Prints a symmetric butterfly-shaped star pattern + +def butterfly_pattern(n): + """ + Prints a butterfly pattern of stars. + + """ + # Upper half of the butterfly + for i in range(1, n + 1): + # Print stars on left wing + for j in range(1, i + 1): + print("*", end="") + # Print spaces between wings + for j in range(1, 2 * (n - i) + 1): + print(" ", end="") + # Print stars on right wing + for j in range(1, i + 1): + print("*", end="") + print() # Newline after each row + + # Lower half of the butterfly + for i in range(n, 0, -1): + # Print stars on left wing + for j in range(1, i + 1): + print("*", end="") + # Print spaces between wings + for j in range(1, 2 * (n - i) + 1): + print(" ", end="") + # Print stars on right wing + for j in range(1, i + 1): + print("*", end="") + print() # Newline after each row + +def main(): + """ + Main function to take user input and handle invalid cases. + """ + try: + n = int(input("Enter number of rows for butterfly pattern: ")) + butterfly_pattern(n) + except ValueError: + print("Please enter a valid integer.") + +# Program execution starts here +main() + + +''' +Output-: + +Enter number of rows for butterfly pattern: 5 +* * +** ** +*** *** +**** **** +********** +********** +**** **** +*** *** +** ** +* * + + +# Cross check with Invalid Input + +Enter number of rows for butterfly pattern: g +Please enter a valid integer. +''' \ No newline at end of file diff --git a/PythonPatterns/PatternPart2/DiamondPattern.py b/PythonPatterns/PatternPart2/DiamondPattern.py new file mode 100644 index 0000000..48274b2 --- /dev/null +++ b/PythonPatterns/PatternPart2/DiamondPattern.py @@ -0,0 +1,59 @@ +# Diamond Pattern: Prints a symmetric diamond-shaped star pattern + +def diamond_pattern(n): + # Upper triangle of the diamond + for i in range(1, n + 1): + # Print leading spaces + for j in range(1, n - i + 1): + print(" ", end="") + # Print stars + for j in range(1, 2 * i): + print("*", end="") + print() # Newline after each row + + # Lower triangle of the diamond + for i in range(n, 0, -1): + # Print leading spaces + for j in range(1, n - i + 1): + print(" ", end="") + # Print stars + for j in range(1, 2 * i): + print("*", end="") + print() # Newline after each row + + +def main(): + """ + Main function to take user input and handle invalid cases. + """ + try: + n = int(input("Enter number of rows for diamond pattern: ")) + diamond_pattern(n) + except ValueError: + print("Please enter a valid integer.") + + +# Program execution starts here +main() + + +''' +Output-: + + * + *** + ***** + ******* +********* +********* + ******* + ***** + *** + * + +# Cross check with Invalid Input + +Enter number of rows for diamond pattern: g +Please enter a valid integer. + +''' \ No newline at end of file diff --git a/PythonPatterns/PatternPart2/FloydTriangle.py b/PythonPatterns/PatternPart2/FloydTriangle.py new file mode 100644 index 0000000..6095556 --- /dev/null +++ b/PythonPatterns/PatternPart2/FloydTriangle.py @@ -0,0 +1,53 @@ +## Floyd's Triangle: Prints a triangle where numbers continue across rows sequentially + +def Floyd_Trianngle(n): + + """ + Prints Floyd's Triangle: a half pyramid with numbers continuing sequentially across rows. + """ + + num = 1 # Initialize starting number + + # Outer loop for rows + for i in range(1, n + 1): + + # Inner loop for printing numbers in each row + for j in range(1, i + 1): + + print(num, end=" ") # Print number followed by a space + num+=1 # Increment number for next print + + print() # Print newline after each row + + +""" +Main function to take user input and handle invalid input gracefully. +""" +def main(): + try: + n = int(input("Enter number : ")) + Floyd_Trianngle(n) + except ValueError: + print("Please enter a valid integer.") + +# Program execution starts here +main() + + +''' + +Output -: +Enter number : 5 +1 +2 3 +4 5 6 +7 8 9 10 +11 12 13 14 15 + +#Cross check with Invalid Value + +Enter number : # +Please enter a valid integer. + + +''' \ No newline at end of file From 3d6b86e948482db9c165fa83a8ed99881e4fed0a Mon Sep 17 00:00:00 2001 From: Kiran Bade Date: Mon, 29 Sep 2025 00:13:08 +0530 Subject: [PATCH 3/3] Added remaining patterns of Python version of JavaDSAseries with docstrings and comments --- .../PatternPart2/HallowRectangle.py | 49 ++++++++++++++++++ PythonPatterns/PatternPart2/HallowRhombus.py | 51 +++++++++++++++++++ .../PatternPart2/InvertedHalfPyramid.py | 49 ++++++++++++++++++ .../PatternPart2/InvertedHalfPyramid1.py | 44 ++++++++++++++++ .../PatternPart2/InvertedHalfPyramidNum.py | 44 ++++++++++++++++ .../PatternPart2/ZeroOneTriangle.py | 50 ++++++++++++++++++ 6 files changed, 287 insertions(+) create mode 100644 PythonPatterns/PatternPart2/HallowRectangle.py create mode 100644 PythonPatterns/PatternPart2/HallowRhombus.py create mode 100644 PythonPatterns/PatternPart2/InvertedHalfPyramid.py create mode 100644 PythonPatterns/PatternPart2/InvertedHalfPyramid1.py create mode 100644 PythonPatterns/PatternPart2/InvertedHalfPyramidNum.py create mode 100644 PythonPatterns/PatternPart2/ZeroOneTriangle.py diff --git a/PythonPatterns/PatternPart2/HallowRectangle.py b/PythonPatterns/PatternPart2/HallowRectangle.py new file mode 100644 index 0000000..fe71de8 --- /dev/null +++ b/PythonPatterns/PatternPart2/HallowRectangle.py @@ -0,0 +1,49 @@ +# 🔹 Hollow Rectangle Pattern + +def hollow_rectangle(n: int) -> None: + """ + Prints a hollow rectangle pattern of size n. + The rectangle has stars (*) on the borders and spaces inside. + """ + for i in range(n + 1): + # Loop through columns + for j in range(n + 1): + if i == 0 or i == n or j == 0 or j == n: + print("*", end="") # Print border stars + else: + print(" ", end="") # Print spaces inside + print() # Move to next line after each row + + +def main(): + """ + Main function to take user input and print the hollow rectangle pattern. + """ + try: + n = int(input("Enter a number: ")) + hollow_rectangle(n) + except ValueError: + print("Please enter a valid integer.") + + +# Run program +main() + + +''' +Output-> + +Enter a number: 5 +****** +* * +* * +* * +* * +****** + +#Cross check with invalid integer. + +Enter a number: - +Please enter a valid Integer + +''' \ No newline at end of file diff --git a/PythonPatterns/PatternPart2/HallowRhombus.py b/PythonPatterns/PatternPart2/HallowRhombus.py new file mode 100644 index 0000000..44c1fe5 --- /dev/null +++ b/PythonPatterns/PatternPart2/HallowRhombus.py @@ -0,0 +1,51 @@ +# 🔹 Hollow Rhombus Pattern + +def hollow_rhombus(n: int) -> None: + """ + Prints a hollow rhombus pattern of size n. + The rhombus is hollow, with stars (*) only on the borders and spaces inside. + """ + for i in range(n + 1): + # Print leading spaces to form rhombus shape + for j in range(n - i): + print(" ", end="") + + # Print stars and spaces inside the rhombus + for j in range(n + 1): + if i == 0 or i == n or j == 0 or j == n: + print("*", end="") # Print border stars + else: + print(" ", end="") # Print spaces inside + print() # Move to next line after each row + + +def main(): + """ + Main function to take user input and print the hollow rhombus pattern. + """ + try: + n = int(input("Enter a number: ")) + hollow_rhombus(n) + except ValueError: + print("Please enter a valid integer.") + + +# Run program +main() + + +''' +Output-> +Enter a Number: 5 + ****** + * * + * * + * * + * * +****** + +#Cross check with invalid integer. + +Enter a number: - +Please enter a valid Integer +''' \ No newline at end of file diff --git a/PythonPatterns/PatternPart2/InvertedHalfPyramid.py b/PythonPatterns/PatternPart2/InvertedHalfPyramid.py new file mode 100644 index 0000000..f81ccb9 --- /dev/null +++ b/PythonPatterns/PatternPart2/InvertedHalfPyramid.py @@ -0,0 +1,49 @@ +# 🔹 Right-Aligned Inverted Half Pyramid of Stars + +def inverted_half_pyramid(n: int) -> None: + """ + Prints a right-aligned inverted half pyramid pattern using stars (*). + The first row has 1 star at the rightmost position, and each subsequent row + increases stars while maintaining right alignment. + """ + for i in range(n): + # Print leading spaces to align stars to the right + for j in range(n - i): + print(" ", end="") + + # Print stars for current row + for j in range(i + 1): + print("*", end="") + + print() # Move to next line after each row + + +def main(): + """ + Main function to take user input and print the right-aligned inverted half pyramid. + """ + try: + n = int(input("Enter a Number: ")) + inverted_half_pyramid(n) + except ValueError: + print("Please enter a valid integer.") + + +# Run program +main() + + +''' +Output-> +Enter a Number: 5 + * + ** + *** + **** + ***** + +#Cross check with invalid integer. + +Enter a number: - +Please enter a valid Integer +''' \ No newline at end of file diff --git a/PythonPatterns/PatternPart2/InvertedHalfPyramid1.py b/PythonPatterns/PatternPart2/InvertedHalfPyramid1.py new file mode 100644 index 0000000..aa7f0d3 --- /dev/null +++ b/PythonPatterns/PatternPart2/InvertedHalfPyramid1.py @@ -0,0 +1,44 @@ +# 🔹 Inverted Half Pyramid of Stars + +def inverted_half_pyramid(n: int) -> None: + """ + Prints an inverted half pyramid pattern using stars (*). + The first row has n stars, decreasing by one star per row. + """ + for i in range(n - 1, -1, -1): + # Print stars for current row + for j in range(i + 1): + print("*", end=" ") # Print star followed by a space + print() # Move to next line after each row + + +def main(): + """ + Main function to take user input and print the star pattern. + """ + try: + n = int(input("Enter a number: ")) + inverted_half_pyramid(n) + except ValueError: + print("Please enter a valid integer.") + + +# Run program +main() + + + +''' +Output-> +Enter a Number: 5 +* * * * * +* * * * +* * * +* * +* + +#Cross check with invalid integer. + +Enter a number: - +Please enter a valid Integer +''' \ No newline at end of file diff --git a/PythonPatterns/PatternPart2/InvertedHalfPyramidNum.py b/PythonPatterns/PatternPart2/InvertedHalfPyramidNum.py new file mode 100644 index 0000000..ac40f8e --- /dev/null +++ b/PythonPatterns/PatternPart2/InvertedHalfPyramidNum.py @@ -0,0 +1,44 @@ +# 🔹 Inverted Half Pyramid of Numbers + +def inverted_half_pyramid_num(n: int) -> None: + """ + Prints an inverted half pyramid pattern of numbers. + Each row starts from 1 and decreases the number of elements as rows increase. + """ + for i in range(n + 1): + # Print numbers in decreasing length per row + for j in range(n - i + 1): + print(j + 1, end=" ") # Print number with space + print() # Move to next line after each row + + +def main(): + """ + Main function to take user input and print the pattern. + """ + try: + n = int(input("Enter a number: ")) + inverted_half_pyramid_num(n) + except ValueError: + print("Please enter a valid integer.") + + +# Run program +main() + + +''' +Output-> +Enter a Number: 5 +1 2 3 4 5 6 +1 2 3 4 5 +1 2 3 4 +1 2 3 +1 2 +1 + +#Cross check with invalid integer. + +Enter a number: - +Please enter a valid Integer +''' \ No newline at end of file diff --git a/PythonPatterns/PatternPart2/ZeroOneTriangle.py b/PythonPatterns/PatternPart2/ZeroOneTriangle.py new file mode 100644 index 0000000..a6430f7 --- /dev/null +++ b/PythonPatterns/PatternPart2/ZeroOneTriangle.py @@ -0,0 +1,50 @@ +# 🔹 Inverted Half Pyramid Number Pattern (Binary: 1s and 0s) + +def inverted_half_pyramid_num(n: int) -> None: + """ + Prints an inverted half pyramid number pattern using 1s and 0s. + The value at each position is determined by (row_index + col_index) % 2. + """ + for i in range(n + 1): + for j in range(i + 1): + sum1 = i + j # sum of row and column indices + + # Print '1' if sum is even, else '0' + if sum1 % 2 == 0: + print("1 ", end="") + else: + print("0 ", end="") + + print() # Move to next line after each row + + +def main(): + """ + Main function to take user input and print the pattern. + """ + try: + n = int(input("Enter a number: ")) + inverted_half_pyramid_num(n) + except ValueError: + print("Please enter a valid integer.") + + +# Run program +main() + + +''' +Output-> +Enter a Number: 5 +1 +0 1 +1 0 1 +0 1 0 1 +1 0 1 0 1 +0 1 0 1 0 1 + +#Cross check with invalid integer. + +Enter a number: - +Please enter a valid Integer +''' \ No newline at end of file