Skip to main content

Display Calendar

0 likes • May 31, 2023 • 0 views
Python
Loading...

More Python Posts

Distinct Primes Finder > 1000

0 likes • Nov 18, 2022 • 3 views
Python
primes=[]
products=[]
def prime(num):
if num > 1:
for i in range(2,num):
if (num % i) == 0:
return False
else:
primes.append(num)
return True
for n in range(30,1000):
if len(primes) >= 20:
break;
else:
prime(n)
for previous, current in zip(primes[::2], primes[1::2]):
products.append(previous * current)
print (products)

clamp number

0 likes • Nov 19, 2022 • 0 views
Python
def clamp_number(num, a, b):
return max(min(num, max(a, b)), min(a, b))
clamp_number(2, 3, 5) # 3
clamp_number(1, -1, -5) # -1

check prop lambda

0 likes • Nov 19, 2022 • 0 views
Python
def check_prop(fn, prop):
return lambda obj: fn(obj[prop])
check_age = check_prop(lambda x: x >= 18, 'age')
user = {'name': 'Mark', 'age': 18}
check_age(user) # True

Print "X" pattern

0 likes • Nov 19, 2022 • 0 views
Python
def print_x_pattern(size):
i,j = 0,size - 1
while j >= 0 and i < size:
initial_spaces = ' '*min(i,j)
middle_spaces = ' '*(abs(i - j) - 1)
final_spaces = ' '*(size - 1 - max(i,j))
if j == i:
print(initial_spaces + '*' + final_spaces)
else:
print(initial_spaces + '*' + middle_spaces + '*' + final_spaces)
i += 1
j -= 1
print_x_pattern(7)

Factorial of N

0 likes • Nov 19, 2022 • 0 views
Python
import math
def factorial(n):
print(math.factorial(n))
return (math.factorial(n))
factorial(5)
factorial(10)
factorial(15)

Create a Pascal’s Triangle

0 likes • May 31, 2023 • 0 views
Python
def generate_pascals_triangle(num_rows):
triangle = []
for row in range(num_rows):
# Initialize the row with 1
current_row = [1]
# Calculate the values for the current row
if row > 0:
previous_row = triangle[row - 1]
for i in range(len(previous_row) - 1):
current_row.append(previous_row[i] + previous_row[i + 1])
# Append 1 at the end of the row
current_row.append(1)
# Add the current row to the triangle
triangle.append(current_row)
return triangle
def display_pascals_triangle(triangle):
for row in triangle:
for number in row:
print(number, end=" ")
print()
# Prompt the user for the number of rows
num_rows = int(input("Enter the number of rows for Pascal's Triangle: "))
# Generate Pascal's Triangle
pascals_triangle = generate_pascals_triangle(num_rows)
# Display Pascal's Triangle
display_pascals_triangle(pascals_triangle)