Pattern Excercises
Pattern Excercises
as input
def sum_list(items): # Function to print full pyramid pattern
sum_numbers = 0 def full_pyramid(n):
for x in items: for i in range(1, n + 1):
sum_numbers += x # Print leading spaces
return sum_numbers for j in range(n - i):
print(sum_list([1, 2, -8])) print(" ", end="")
………………………………………………………………………….
# Define a function called multiply_list that takes a list # Print asterisks for the current row
'items' as input for k in range(1, 2*i):
def multiply_list(items): print("*", end="")
tot = 1 print()
# Iterate through each element 'x' in the input list
'items' full_pyramid(5)
for x in items: ………………………………………………………………………………….
tot *= x n=5
return tot alph = 65
print(multiply_list([1, 2, -8])) for i in range(0, n):
……………………………………………………………………………… print(" " * (n-i), end=" ")
# Define a function called smallest_num_in_list that takes for j in range(0, i+1):
a list 'list' as input print(chr(alph), end=" ")
def smallest_num_in_list(list): alph += 1
# Initialize a variable 'min' with the first element of the alph = 65
input list as the initial minimum print()
min = list[0] ………………………………………………………..
# Iterate through each element 'a' in the input list 'list' def print_number_pyramid(rows):
for a in list: for i in range(1, rows + 1):
# Check if the current element 'a' is smaller than the # Print spaces
current minimum 'min' for j in range(rows - i):
if a < min: print(" ", end="")
# If 'a' is smaller, update the minimum 'min' to 'a' # Print numbers
min = a for j in range(2 * i - 1):
# Return the final minimum value in the list print(j + 1, end="")
return min # Move to the next line after each row
print()
# Call the smallest_num_in_list function with the list [1,
2, -8, 0] as input and print the result # Example usage
print(smallest_num_in_list([1, 2, -8, 0])) num_rows = 5
………………………………………………………………………. print_number_pyramid(num_rows)
# Define a function called match_words that takes a list ……………………………………………………………………….
of words 'words' as input def print_hollow_inverted_half_pyramid(rows):
def match_words(words): for i in range(rows, 0, -1):
ctr = 0 for j in range(rows - i):
# Iterate through each word in the input list 'words' print(" ", end="")
for word in words: for j in range(i):
# Check if the word has a length greater than 1 and if j == 0 or j == i - 1 or i == rows:
its first and last characters are the same print("*", end="")
if len(word) > 1 and word[0] == word[-1]: else:
# If the condition is met, increment the counter print(" ", end="")
'ctr' print()
ctr += 1
# Return the final count of matching words # Example usage
return ctr num_rows = 5
# Call the match_words function with the list ['abc', 'xyz', print("Hollow Inverted Half Pyramid:")
'aba', '1221'] as input and print the result print_hollow_inverted_half_pyramid(num_rows)
print(match_words(['abc', 'xyz', 'aba', '1221']))