Skip to main content

Create a Pascal’s Triangle

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

More Python Posts

print indices

0 likes • Nov 18, 2022 • 0 views
Python
# List
lst = [1, 2, 3, 'Alice', 'Alice']
# One-Liner
indices = [i for i in range(len(lst)) if lst[i]=='Alice']
# Result
print(indices)
# [3, 4]

Bitonic sort

0 likes • Nov 19, 2022 • 0 views
Python
# Python program for Bitonic Sort. Note that this program
# works only when size of input is a power of 2.
# The parameter dir indicates the sorting direction, ASCENDING
# or DESCENDING; if (a[i] > a[j]) agrees with the direction,
# then a[i] and a[j] are interchanged.*/
def compAndSwap(a, i, j, dire):
if (dire==1 and a[i] > a[j]) or (dire==0 and a[i] > a[j]):
a[i],a[j] = a[j],a[i]
# It recursively sorts a bitonic sequence in ascending order,
# if dir = 1, and in descending order otherwise (means dir=0).
# The sequence to be sorted starts at index position low,
# the parameter cnt is the number of elements to be sorted.
def bitonicMerge(a, low, cnt, dire):
if cnt > 1:
k = cnt/2
for i in range(low , low+k):
compAndSwap(a, i, i+k, dire)
bitonicMerge(a, low, k, dire)
bitonicMerge(a, low+k, k, dire)
# This funcion first produces a bitonic sequence by recursively
# sorting its two halves in opposite sorting orders, and then
# calls bitonicMerge to make them in the same order
def bitonicSort(a, low, cnt,dire):
if cnt > 1:
k = cnt/2
bitonicSort(a, low, k, 1)
bitonicSort(a, low+k, k, 0)
bitonicMerge(a, low, cnt, dire)
# Caller of bitonicSort for sorting the entire array of length N
# in ASCENDING order
def sort(a,N, up):
bitonicSort(a,0, N, up)
# Driver code to test above
a = [3, 7, 4, 8, 6, 2, 1, 5]
n = len(a)
up = 1
sort(a, n, up)
print ("\n\nSorted array is")
for i in range(n):
print("%d" %a[i]),

Propositional logic with itertools

0 likes • Nov 18, 2022 • 0 views
Python
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('')
# 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==y
tt(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==y
tt(prob2,3)
#~(p/\q)=(~p\/~q)
def prob3(p,q) :
x=not (p and q)
y=(not p) or (not q)
return x==y
tt(prob3,2)
#(~(p\/q))=((~p)/\~q)
def prob4(p, q):
x = not(p or q)
y = not p and not q
return x == y
tt(prob4, 2)
#(p/\(p=>q)=>q)
def prob5(p,q):
x= p and ( not p or q)
return not x or q
tt(prob5,2)
# (p=>q)=((p\/q)=q)
def prob6(p,q) :
x = (not p or q)
y=((p or q) == q)
return x==y
tt(prob6,2)
#((p=>q)=(p\/q))=q
def prob7(p,q):
if ((not p or q)==(p or q))==q:
return 1
tt(prob7,2)
#(p=>q)=((p/\q)=p)
def prob8(p,q):
if (not p or q)==((p and q)==p):
return 1
tt(prob8,2)
#((p=>q)=(p/\q))=p
def 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 x
tt(prob10, 3)
# (p = q) /\ (q => r) => (p => r)
#answer 1
def prob11(p,q,r) :
x = not((p is q) and (not q or r)) or (not p or r)
return x
tt(prob11, 3)
#(p=q)/\(q=>r)=>(p=>r)
#answer 2
def prob11(p,q,r):
x=(p==q) and (not q or r)
y=not p or r
return not x or y
tt(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 r
return not x or y
tt(prob12,3)
#(p=>q)=>((p/\r)=>(q/\r))
def prob13(p,q,r):
x=not p or q
y=(not(p and r) or ( q and r))
return not x or y
tt(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==y
tt(prob14,3)
def prob15(p, q):
x = not(p and q)
y = not p and not q
return x == y
tt(prob15, 2)
def prob16(p, q):
x = not(p or q)
y = not p or not q
return x == y
tt(prob16, 2)
def prob17(p):
x = p
y = not p
return x == y
tt(prob17, 1)

read file contents into a list

0 likes • Jun 1, 2023 • 0 views
Python
filename = "data.txt"
with open(filename, "r") as file:
file_contents = file.readlines()
file_contents = [line.strip() for line in file_contents]
print("File contents:")
for line in file_contents:
print(line)

Factorial of N

0 likes • Nov 19, 2022 • 0 views
Python
import math
def factorial(n):
print(math.factorial(n))
return (math.factorial(n))
factorial(5)
factorial(10)
factorial(15)

Plotting Fibonacci

0 likes • Nov 19, 2022 • 0 views
Python
# Python program for Plotting Fibonacci
# spiral fractal using Turtle
import turtle
import math
def fiboPlot(n):
a = 0
b = 1
square_a = a
square_b = b
# Setting the colour of the plotting pen to blue
x.pencolor("blue")
# Drawing the first square
x.forward(b * factor)
x.left(90)
x.forward(b * factor)
x.left(90)
x.forward(b * factor)
x.left(90)
x.forward(b * factor)
# Proceeding in the Fibonacci Series
temp = square_b
square_b = square_b + square_a
square_a = temp
# Drawing the rest of the squares
for i in range(1, n):
x.backward(square_a * factor)
x.right(90)
x.forward(square_b * factor)
x.left(90)
x.forward(square_b * factor)
x.left(90)
x.forward(square_b * factor)
# Proceeding in the Fibonacci Series
temp = square_b
square_b = square_b + square_a
square_a = temp
# Bringing the pen to starting point of the spiral plot
x.penup()
x.setposition(factor, 0)
x.seth(0)
x.pendown()
# Setting the colour of the plotting pen to red
x.pencolor("red")
# Fibonacci Spiral Plot
x.left(90)
for i in range(n):
print(b)
fdwd = math.pi * b * factor / 2
fdwd /= 90
for j in range(90):
x.forward(fdwd)
x.left(1)
temp = a
a = b
b = temp + b
# Here 'factor' signifies the multiplicative
# factor which expands or shrinks the scale
# of the plot by a certain factor.
factor = 1
# Taking Input for the number of
# Iterations our Algorithm will run
n = int(input('Enter the number of iterations (must be > 1): '))
# Plotting the Fibonacci Spiral Fractal
# and printing the corresponding Fibonacci Number
if n > 0:
print("Fibonacci series for", n, "elements :")
x = turtle.Turtle()
x.speed(100)
fiboPlot(n)
turtle.done()
else:
print("Number of iterations must be > 0")