Skip to main content

print indices

0 likes • Nov 18, 2022 • 0 views
Python
Loading...

More Python Posts

radians to degrees

0 likes • Nov 19, 2022 • 0 views
Python
from math import pi
def rads_to_degrees(rad):
return (rad * 180.0) / pi
rads_to_degrees(pi / 2) # 90.0

Binary search

0 likes • Sep 22, 2023 • 11 views
Python
# Python binary search function
def binary_search(arr, target):
left = 0
right = len(arr) - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1
# Usage
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
target = 7
result = binary_search(arr, target)
if result != -1:
print(f"Element is present at index {result}")
else:
print("Element is not present in array")

Print pyramid pattern

0 likes • Nov 19, 2022 • 0 views
Python
def print_pyramid_pattern(n):
# outer loop to handle number of rows
# n in this case
for i in range(0, n):
# inner loop to handle number of columns
# values changing acc. to outer loop
for j in range(0, i+1):
# printing stars
print("* ",end="")
# ending line after each row
print("\r")
print_pyramid_pattern(10)

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)

integer to roman numeral

0 likes • Nov 19, 2022 • 0 views
Python
def to_roman_numeral(num):
lookup = [
(1000, 'M'),
(900, 'CM'),
(500, 'D'),
(400, 'CD'),
(100, 'C'),
(90, 'XC'),
(50, 'L'),
(40, 'XL'),
(10, 'X'),
(9, 'IX'),
(5, 'V'),
(4, 'IV'),
(1, 'I'),
]
res = ''
for (n, roman) in lookup:
(d, num) = divmod(num, n)
res += roman * d
return res
to_roman_numeral(3) # 'III'
to_roman_numeral(11) # 'XI'
to_roman_numeral(1998) # 'MCMXCVIII'

Calculator

0 likes • Nov 19, 2022 • 0 views
Python
""" Calculator
----------------------------------------
"""
def addition ():
print("Addition")
n = float(input("Enter the number: "))
t = 0 //Total number enter
ans = 0
while n != 0:
ans = ans + n
t+=1
n = float(input("Enter another number (0 to calculate): "))
return [ans,t]
def subtraction ():
print("Subtraction");
n = float(input("Enter the number: "))
t = 0 //Total number enter
sum = 0
while n != 0:
ans = ans - n
t+=1
n = float(input("Enter another number (0 to calculate): "))
return [ans,t]
def multiplication ():
print("Multiplication")
n = float(input("Enter the number: "))
t = 0 //Total number enter
ans = 1
while n != 0:
ans = ans * n
t+=1
n = float(input("Enter another number (0 to calculate): "))
return [ans,t]
def average():
an = []
an = addition()
t = an[1]
a = an[0]
ans = a / t
return [ans,t]
// main...
while True:
list = []
print(" My first python program!")
print(" Simple Calculator in python by Malik Umer Farooq")
print(" Enter 'a' for addition")
print(" Enter 's' for substraction")
print(" Enter 'm' for multiplication")
print(" Enter 'v' for average")
print(" Enter 'q' for quit")
c = input(" ")
if c != 'q':
if c == 'a':
list = addition()
print("Ans = ", list[0], " total inputs ",list[1])
elif c == 's':
list = subtraction()
print("Ans = ", list[0], " total inputs ",list[1])
elif c == 'm':
list = multiplication()
print("Ans = ", list[0], " total inputs ",list[1])
elif c == 'v':
list = average()
print("Ans = ", list[0], " total inputs ",list[1])
else:
print ("Sorry, invilid character")
else:
break