Loading...
More Python Posts
import copybegining = [False,False,False,False,False,None,True,True,True,True,True]#False = black True = whiteits = [0]def swap(layout, step):layoutCopy = copy.deepcopy(layout)layoutCopy[(step[0]+step[1])], layoutCopy[step[1]] = layoutCopy[step[1]], layoutCopy[(step[0]+step[1])]return layoutCopydef isSolved(layout):for i in range(len(layout)):if(layout[i] == False):return (i >= (len(layout)/2))def recurse(layout, its, steps = []):if isSolved(layout):its[0] += 1print(layout,list(x[0] for x in steps))returnstep = Nonefor i in range(len(layout)):if(layout[i] == None):if(i >= 1): #If the empty space could have something to the leftif(layout[i - 1] == False):step = [-1,i]recurse(swap(layout,step), its, (steps+[step]))if(i > 1): #If the empty space could have something 2 to the leftif(layout[i - 2] == False):step = [-2,i]recurse(swap(layout,step), its, (steps+[step]))if(i < (len(layout)-1)): #If the empty space could have something to the rightif(layout[i + 1] == True):step = [1,i]recurse(swap(layout,step), its, (steps+[step]))if(i < (len(layout)-2)): #If the empty space could have something to the rightif(layout[i + 2] == True):step = [2,i]recurse(swap(layout,step), its, (steps+[step]))its[0] += 1#return Nonerecurse(begining,its,[])print(its[0])
# Function to multiply two matricesdef multiply_matrices(matrix1, matrix2):# Check if the matrices can be multipliedif 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 zerosresult = [[0 for _ in range(len(matrix2[0]))] for _ in range(len(matrix1))]# Perform matrix multiplicationfor 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 matricesmatrix1 = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]matrix2 = [[10, 11],[12, 13],[14, 15]]# Multiply the matricesresult_matrix = multiply_matrices(matrix1, matrix2)# Display the resultif result_matrix is not None:print("Result:")for row in result_matrix:print(row)
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)
# Python Program to calculate the square rootnum = float(input('Enter a number: '))num_sqrt = num ** 0.5print('The square root of %0.3f is %0.3f'%(num ,num_sqrt))
from functools import partialdef curry(fn, *args):return partial(fn, *args)add = lambda x, y: x + yadd10 = curry(add, 10)add10(20) # 30
# Python program for implementation of Radix Sort# A function to do counting sort of arr[] according to# the digit represented by exp.def countingSort(arr, exp1):n = len(arr)# The output array elements that will have sorted arroutput = [0] * (n)# initialize count array as 0count = [0] * (10)# Store count of occurrences in count[]for i in range(0, n):index = (arr[i]/exp1)count[int((index)%10)] += 1# Change count[i] so that count[i] now contains actual# position of this digit in output arrayfor i in range(1,10):count[i] += count[i-1]# Build the output arrayi = n-1while i>=0:index = (arr[i]/exp1)output[ count[ int((index)%10) ] - 1] = arr[i]count[int((index)%10)] -= 1i -= 1# Copying the output array to arr[],# so that arr now contains sorted numbersi = 0for i in range(0,len(arr)):arr[i] = output[i]# Method to do Radix Sortdef radixSort(arr):# Find the maximum number to know number of digitsmax1 = max(arr)# Do counting sort for every digit. Note that instead# of passing digit number, exp is passed. exp is 10^i# where i is current digit numberexp = 1while max1/exp > 0:countingSort(arr,exp)exp *= 10# Driver code to test abovearr = [ 170, 45, 75, 90, 802, 24, 2, 66]radixSort(arr)for i in range(len(arr)):print(arr[i]),