Loading...
More Python Posts
# Listlst = [1, 2, 3, 'Alice', 'Alice']# One-Linerindices = [i for i in range(len(lst)) if lst[i]=='Alice']# Resultprint(indices)# [3, 4]
def generate_floyds_triangle(num_rows):triangle = []number = 1for row in range(num_rows):current_row = []for _ in range(row + 1):current_row.append(number)number += 1triangle.append(current_row)return triangledef display_floyds_triangle(triangle):for row in triangle:for number in row:print(number, end=" ")print()# Prompt the user for the number of rowsnum_rows = int(input("Enter the number of rows for Floyd's Triangle: "))# Generate Floyd's Trianglefloyds_triangle = generate_floyds_triangle(num_rows)# Display Floyd's Triangledisplay_floyds_triangle(floyds_triangle)
#SetsU = {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 bsdef 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 finalSetdef 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 finalSetdef compliment(set1) :finalSet = set()bitList = set2bits(set1, U)for i in range(len(U)) :if(not bitList[i]) :finalSet.add(i)return finalSetdef 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 \/ ~qdef prob3():return compliment(intersection(P,R)) == union(compliment(P), compliment(R))#~(p \/ q) = ~p /\ ~qdef 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: TrueProblem 2: TrueProblem 3: TrueProblem 4: TrueProblem 5: TrueProblem 6: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}'''
def calculate_values():value1 = 10value2 = 20return value1, value2result1, result2 = calculate_values()print("Result 1:", result1)print("Result 2:", result2)
bytes_data = b'Hello, World!'string_data = bytes_data.decode('utf-8')print("String:", string_data)
from itertools import productV='∀'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('')# p \/ (q /\ r) = (p \/ q) /\ (p \/ r)def prob1(p,q,r) :x=p or (q and r)y= (p or q) and (p or r)return x==ytt(prob1,3)# p/\(q\/r)=(p/\q)\/(p/\r)def prob2(p,q,r) :x=p and ( q or r )y=(p and q) or (p and r)return x==ytt(prob2,3)#~(p/\q)=(~p\/~q)def prob3(p,q) :x=not (p and q)y=(not p) or (not q)return x==ytt(prob3,2)#(~(p\/q))=((~p)/\~q)def prob4(p, q):x = not(p or q)y = not p and not qreturn x == ytt(prob4, 2)#(p/\(p=>q)=>q)def prob5(p,q):x= p and ( not p or q)return not x or qtt(prob5,2)# (p=>q)=((p\/q)=q)def prob6(p,q) :x = (not p or q)y=((p or q) == q)return x==ytt(prob6,2)#((p=>q)=(p\/q))=qdef prob7(p,q):if ((not p or q)==(p or q))==q:return 1tt(prob7,2)#(p=>q)=((p/\q)=p)def prob8(p,q):if (not p or q)==((p and q)==p):return 1tt(prob8,2)#((p=>q)=(p/\q))=pdef prob9(p,q):if ((not p or q)==(p and q))==p:return '1'tt(prob9,2)#(p=>q)/\(q=>r)=>(p=>r)def prob10(p,q,r) :x = not ((not p or q) and (not q or r)) or (not p or r)return xtt(prob10, 3)# (p = q) /\ (q => r) => (p => r)#answer 1def prob11(p,q,r) :x = not((p is q) and (not q or r)) or (not p or r)return xtt(prob11, 3)#(p=q)/\(q=>r)=>(p=>r)#answer 2def prob11(p,q,r):x=(p==q) and (not q or r)y=not p or rreturn not x or ytt(prob11,3)#((p=>q)/\(q=r))=>(p=>r)def prob12(p,q,r):x=(not p or q) and ( q==r )y=not p or rreturn not x or ytt(prob12,3)#(p=>q)=>((p/\r)=>(q/\r))def prob13(p,q,r):x=not p or qy=(not(p and r) or ( q and r))return not x or ytt(prob13,3)#Question#2----------------------------------------#(p=>q)=>r=p=>(q=>r)def prob14(p,q,r):x=(not(not p or q) or r)y=(not p or (not q or r))return x==ytt(prob14,3)def prob15(p, q):x = not(p and q)y = not p and not qreturn x == ytt(prob15, 2)def prob16(p, q):x = not(p or q)y = not p or not qreturn x == ytt(prob16, 2)def prob17(p):x = py = not preturn x == ytt(prob17, 1)