Skip to main content

Multiply Two Matrices

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

More Python Posts

Using logic with sets

0 likes • Nov 18, 2022 • 0 views
Python
#Sets
U = {0,1,2,3,4,5,6,7,8,9}
P = {1,2,3,4}
Q = {4,5,6}
R = {3,4,6,8,9}
def set2bits(xs,us) :
bs=[]
for x in us :
if x in xs :
bs.append(1)
else:
bs.append(0)
assert len(us) == len(bs)
return bs
def union(set1,set2) :
finalSet = set()
bitList1 = set2bits(set1, U)
bitList2 = set2bits(set2, U)
for i in range(len(U)) :
if(bitList1[i] or bitList2[i]) :
finalSet.add(i)
return finalSet
def intersection(set1,set2) :
finalSet = set()
bitList1 = set2bits(set1, U)
bitList2 = set2bits(set2, U)
for i in range(len(U)) :
if(bitList1[i] and bitList2[i]) :
finalSet.add(i)
return finalSet
def compliment(set1) :
finalSet = set()
bitList = set2bits(set1, U)
for i in range(len(U)) :
if(not bitList[i]) :
finalSet.add(i)
return finalSet
def implication(a,b):
return union(compliment(a), b)
###########################################################################################
###################### Problems 1-6 #######################################
###########################################################################################
#p \/ (q /\ r) = (p \/ q) /\ (p \/ r)
def prob1():
return union(P, intersection(Q,R)) == intersection(union(P,Q), union(P,R))
#p /\ (q \/ r) = (p /\ q) \/ (p /\ r)
def prob2():
return intersection(P, union(Q,R)) == union(intersection(P,Q), intersection(P,R))
#~(p /\ q) = ~p \/ ~q
def prob3():
return compliment(intersection(P,R)) == union(compliment(P), compliment(R))
#~(p \/ q) = ~p /\ ~q
def prob4():
return compliment(union(P,Q)) == intersection(compliment(P), compliment(Q))
#(p=>q) = (~q => ~p)
def prob5():
return implication(P,Q) == implication(compliment(Q), compliment(P))
#(p => q) /\ (q => r) => (p => r)
def prob6():
return implication(intersection(implication(P,Q), implication(Q,R)), implication(P,R))
print("Problem 1: ", prob1())
print("Problem 2: ", prob2())
print("Problem 3: ", prob3())
print("Problem 4: ", prob4())
print("Problem 5: ", prob5())
print("Problem 6: ", prob6())
'''
Problem 1: True
Problem 2: True
Problem 3: True
Problem 4: True
Problem 5: True
Problem 6: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
'''

screencap.py

0 likes • Jan 23, 2021 • 0 views
Python
"""
Take screenshots at x interval - make a movie of doings on a computer.
"""
import time
from datetime import datetime
import ffmpeg
import pyautogui
while True:
epoch_time = int(time.time())
today = datetime.now().strftime("%Y_%m_%d")
filename = str(epoch_time) + ".png"
print("taking screenshot: {0}".format(filename))
myScreenshot = pyautogui.screenshot()
myScreenshot.save(today + "/" + filename)
time.sleep(5)
#
# and then tie it together with: https://github.com/kkroening/ffmpeg-python/blob/master/examples/README.md#assemble-video-from-sequence-of-frames
#
"""
import ffmpeg
(
ffmpeg
.input('./2021_01_22/*.png', pattern_type='glob', framerate=25)
.filter('deflicker', mode='pm', size=10)
.filter('scale', size='hd1080', force_original_aspect_ratio='increase')
.output('movie.mp4', crf=20, preset='slower', movflags='faststart', pix_fmt='yuv420p')
.run()
)
"""

return multiple values from a function

0 likes • Jun 1, 2023 • 0 views
Python
def calculate_values():
value1 = 10
value2 = 20
return value1, value2
result1, result2 = calculate_values()
print("Result 1:", result1)
print("Result 2:", result2)

Calculate the Area of a Triangle

0 likes • May 31, 2023 • 0 views
Python
# Prompt user for base and height
base = float(input("Enter the base of the triangle: "))
height = float(input("Enter the height of the triangle: "))
# Calculate the area
area = (base * height) / 2
# Display the result
print("The area of the triangle is:", area)

primes numbers finder

0 likes • Mar 12, 2021 • 0 views
Python
prime_lists=[] # a list to store the prime numbers
def prime(n): # define prime numbers
if n <= 1:
return False
# divide n by 2... up to n-1
for i in range(2, n):
if n % i == 0: # the remainder should'nt be a 0
return False
else:
prime_lists.append(n)
return True
for n in range(30,1000): # calling function and passing starting point =30 coz we need primes >30
prime(n)
check=0 # a var to limit the output to 10 only
for n in prime_lists:
for x in prime_lists:
val= n *x
if (val > 1000 ):
check=check +1
if (check <10) :
print("the num is:", val , "=",n , "* ", x )
break

Reverse a linked list

0 likes • Nov 19, 2022 • 0 views
Python
# Python program to reverse a linked list
# Time Complexity : O(n)
# Space Complexity : O(n) as 'next'
#variable is getting created in each loop.
# Node class
class Node:
# Constructor to initialize the node object
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
# Function to initialize head
def __init__(self):
self.head = None
# Function to reverse the linked list
def reverse(self):
prev = None
current = self.head
while(current is not None):
next = current.next
current.next = prev
prev = current
current = next
self.head = prev
# Function to insert a new node at the beginning
def push(self, new_data):
new_node = Node(new_data)
new_node.next = self.head
self.head = new_node
# Utility function to print the linked LinkedList
def printList(self):
temp = self.head
while(temp):
print temp.data,
temp = temp.next
# Driver program to test above functions
llist = LinkedList()
llist.push(20)
llist.push(4)
llist.push(15)
llist.push(85)
print "Given Linked List"
llist.printList()
llist.reverse()
print "\nReversed Linked List"
llist.printList()