Skip to main content

Calculate Square Root

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

More Python Posts

CSCE 2100 Question 3

0 likes • Nov 18, 2022 • 3 views
Python
# question3.py
from itertools import product
V='∀'
E='∃'
def tt(f,n) :
xss=product((0,1),repeat=n)
print('function:',f.__name__)
for xs in xss : print(*xs,':',int(f(*xs)))
print('')
# this is the logic for part A (p\/q\/r) /\ (p\/q\/~r) /\ (p\/~q\/r) /\ (p\/~q\/~r) /\ (~p\/q\/r) /\ (~p\/q\/~r) /\ (~p\/~q\/r) /\ (~p\/~q\/~r)
def parta(p,q,r) :
a=(p or q or r) and (p or q or not r) and (p or not q or r)and (p or not q or not r)
b=(not p or q or r ) and (not p or q or not r) and (not p or not q or r) and (not p or not q or not r)
c= a and b
return c
def partb(p,q,r) :
a=(p or q and r) and (p or not q or not r) and (p or not q or not r)and (p or q or not r)
b=(not p or q or r ) and (not p or q or not r) and (not p or not q or r) and (not p or not q or not r)
c= a and b
return c
print("part A:")
tt(parta,3)
print("part B:")
tt(partb,3)

delay time lambda

0 likes • Nov 19, 2022 • 0 views
Python
from time import sleep
def delay(fn, ms, *args):
sleep(ms / 1000)
return fn(*args)
delay(lambda x: print(x), 1000, 'later') # prints 'later' after one second

Binary search algorithm

0 likes • Nov 19, 2022 • 0 views
Python
""" Binary Search Algorithm
----------------------------------------
"""
#iterative implementation of binary search in Python
def binary_search(a_list, item):
"""Performs iterative binary search to find the position of an integer in a given, sorted, list.
a_list -- sorted list of integers
item -- integer you are searching for the position of
"""
first = 0
last = len(a_list) - 1
while first <= last:
i = (first + last) / 2
if a_list[i] == item:
return ' found at position '.format(item=item, i=i)
elif a_list[i] > item:
last = i - 1
elif a_list[i] < item:
first = i + 1
else:
return ' not found in the list'.format(item=item)
#recursive implementation of binary search in Python
def binary_search_recursive(a_list, item):
"""Performs recursive binary search of an integer in a given, sorted, list.
a_list -- sorted list of integers
item -- integer you are searching for the position of
"""
first = 0
last = len(a_list) - 1
if len(a_list) == 0:
return ' was not found in the list'.format(item=item)
else:
i = (first + last) // 2
if item == a_list[i]:
return ' found'.format(item=item)
else:
if a_list[i] < item:
return binary_search_recursive(a_list[i+1:], item)
else:
return binary_search_recursive(a_list[:i], item)

Multiply Two Matrices

0 likes • May 31, 2023 • 0 views
Python
# Function to multiply two matrices
def multiply_matrices(matrix1, matrix2):
# Check if the matrices can be multiplied
if len(matrix1[0]) != len(matrix2):
print("Error: The number of columns in the first matrix must be equal to the number of rows in the second matrix.")
return None
# Create the result matrix filled with zeros
result = [[0 for _ in range(len(matrix2[0]))] for _ in range(len(matrix1))]
# Perform matrix multiplication
for i in range(len(matrix1)):
for j in range(len(matrix2[0])):
for k in range(len(matrix2)):
result[i][j] += matrix1[i][k] * matrix2[k][j]
return result
# Example matrices
matrix1 = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
matrix2 = [[10, 11],
[12, 13],
[14, 15]]
# Multiply the matrices
result_matrix = multiply_matrices(matrix1, matrix2)
# Display the result
if result_matrix is not None:
print("Result:")
for row in result_matrix:
print(row)

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

Finding NULL values within set

0 likes • Oct 7, 2022 • 1 view
Python
x[cat_var].isnull().sum().sort_values(ascending=False)