a=input("Enter any file name with extension : ") ''' Accept from user '''
e=' ' ''' e is empty string '''
flag=False
if '.' in a:
for i in a:
if i=='.': ''' if (.) is found in filename then add characters in e '''
flag=True
elif flag==True:
e=e+i
print(e) ''' print extension '''
''' defining/creating a function '''
def table(n):
for i in range(1,11,1):
mul=n*i
print(n,end="")
print("*",end="")
print(i,end="")
print("=",end="")
print(mul) ''' printing multiplication '''
s=input("Enter number: ") ''' Accept number from user '''
table(int(s)) ''' function called '''
''' user can enter values many times. If user wants to stop finding minimum and maximum then please enter done '''
while(1): ''' loops runs '''
n1=input('Enter first no: ') ''' Accept number from user '''
if n1=='done': ''' if user enter done while loop breaks otherwise it will ask 2nd number '''
print('Good Bye........')
break
else:
n2=input('Enter second no: ')
print(' largest no is: ')
print(max(n1,n2)) ''' max is use to find miximum between 2 numbers '''
print(' smallest no is: ')
print(min(n1,n2)) ''' min is use to find minimum between 2 numbers '''
''' The solutions of the quadratic equation ax2 + bx + c = 0 correspond to the roots of the function f(x) = ax2 + bx + c, since they are the values of x for which f(x) = 0. '''
''' note: Enter valid numbers '''
import math ''' import library '''
a=int(input('Enter value of a: ')) // Accept values from user
b=int(input('Enter value of b: '))
c=int(input('Enter value of c: '))
print('quadratic equation is: ',a,'x**2 + ',b,'x + ',c,sep='')
''' calculate result '''
d = (b**2) - (4*a*c)
s1 = (-b-math.sqrt(d))/(2*a)
s2 = (-b+math.sqrt(d))/(2*a)
''' printing result '''
print('first value is: ',s1)
print('second value is: ',s2)
''' In NCR and NPR, C stands for Combinations, and P stands for permutations. Now for combinations, it is the number of ways you can pick r objects out of n. In permutations, it is the number of ways you can pick r objects out of n and then arrange them. '''
n=int(input('Enter value of n : ')) ''' Accept values from user '''
r=int(input('Enter value of r: '))
''' calculate nPr '''
c=n
r1=n-r
for i in range(1,n,1):
n=n*i
for i in range(1,r1,1):
r1=r1*i
print('nPr = ',n/r1)
''' calculate nCr '''
for i in range(1,r,1):
r=r*i
print('nCr = ',n/r1*r)
''' a series of numbers in which each number ( Fibonacci number ) is the sum of the two preceding numbers. The simplest is the series 0, 1, 1, 2, 3, 5, 8, etc. '''
n=int(input('Enter number: ')) '''Accpt number from user'''
n1=0
n2=1
print('Fibonacci series up to given number :')
print(n1, end=" ") '''print value of n1'''
print(n2,end=" ") '''print value of n2'''
n3=n1+n2
print(n3,end=" ") '''print value of n3'''
while n3<n: '''loop runs until n3 becomes less than given number(n)'''
n1=n2
n2=n3
n3=n1+n2
print(n3,end=" ")
Circular Doubly Linked List
class node:
//To initialize values
def __init__(self,data=None):
self.data=data
self.next=None
self.prev=None
class linking: //To create Linked List
def __init__(self):
n=int(input("Enter How many Nodes You want: "))
for i in range(1,n+1):
if i==1:
self.head = node(input(f"Enter {i} Data: "))
mur = self.head
mur.next = mur
mur.prev = mur
else:
newnode = node(input(f"Enter {i} data: "))
cur = self.head
mur = self.head
while cur.next != mur:
cur = cur.next
cur.next = newnode
newnode.next = mur
newnode.prev = cur
//To display elements in Linked List def display(self):
cur=self.head
mur=cur
while cur.next != mur:
print(cur.data,end=" ")
cur = cur.next
print(cur.data)
//To add node at specific position in Linked List def add(self,data):
pos=int(input("Enter Position: "))
newnode=node(data)
cur=self.head
mur=cur
c=1
while cur.next!=mur:
c+=1
cur=cur.next
print(c)
if pos==1:
cur=self.head
mur=cur
self.head=newnode
#newnode.prev=None
newnode.next=cur
cur.prev=newnode
while cur.next!=mur:
cur=cur.next
cur.next=newnode
newnode.prev=cur
if pos==c+1:
cur=self.head
mur=self.head
while cur.next!=mur:
cur=cur.next
cur.next=newnode
newnode.prev=cur
newnode.next=mur
mur.prev=newnode
else:
cur=self.head
mur=cur.next
pt=cur
cn=1
while cur.next!=pt:
if pos-1==cn:
cur.next=newnode
newnode.next=mur
mur.prev=newnode
newnode.prev=cur
cur=cur.next
mur=cur.next
cn += 1
//To remove node from specific position in Linked List
def remove(self,pos):
cur = self.head
mur=cur
c = 1
while cur.next != mur:
c += 1
cur = cur.next
if pos==1:
cur=self.head
mur=cur.next
pt=mur
self.head=mur
while mur.next!=cur:
mur=mur.next
mur.next=pt
pt.prev=mur
cur.next=None
cur.prev=None
if pos==c:
cur=self.head
pt=cur
mur=cur.next
while mur.next!=pt:
mur=mur.next
cur=cur.next
cur.next=pt
pt.prev=cur
mur.prev=None
mur.next=None
else:
cur=self.head
pt=cur
mur=cur.next
cn=1
while cur.next!=pt:
if pos-1==cn:
cur.next=mur.next
mur=mur.next
mur.prev=cur
cur=cur.next
mur=cur.next
cn+=1
//To Find data in Linked List
def find(self,data):
cur=self.head
i=0
f=0
while cur.next != None:
if int(cur.data)==data:
f=1
break
cur = cur.next
i+=1
if int(cur.data) == data:
f = 1
if f==1:
print(f"Your Data Found at {i+1}")
else:
print("Your Data Not Found......")
//To create menu
while True:
choose = (int(input("\n1)create\n2)Add node\n3)Display\n4)Delete node\n5)Find\n6)Exit.\nEnter your choice:")))
if choose == 1:
mylist = linking()
if choose == 3:
mylist.display()
if choose==6:
break
if choose == 2:
mylist.add(input("Enter Data:"))
if choose == 4:
mylist.remove(int(input("Enter Position:")))
if choose == 5:
data = int(input("Enter Data: "))
mylist.find(data)
Singly Linked List
class node:
//To initialize values
def __init__(self,data=None):
self.data=data
self.next=None
class linking:
//To create Linked List
def __init__(self):
n=int(input('Enter how many nodes:'))
for i in range(1,n+1):
if i==1:
self.head = node(input(f"Enter {i} data: "))
else:
newnode = node(input(f"Enter {i} data: "))
cur = self.head
while cur.next != None:
cur = cur.next
cur.next = newnode
//To add node at specific position in Linked List
def add(self,data):
pos=int(input("Enter position: "))
cur = self.head
c = 1
while cur.next != None:
cur = cur.next
c = c + 1
if pos==c:
newnode = node(data)
cur = self.head
while cur.next != None:
cur = cur.next
cur.next = newnode
if pos==1:
cur=self.head
newnode = node(data)
newnode.next=cur
self.head=newnode
else:
cn=1
cur=self.head
mur=cur.next
newnode = node(data)
while cur.next!=None:
if pos-1==cn:
cur.next=newnode
newnode.next=mur
cur=cur.next
mur=cur.next
cn+=1
//To display elements in Linked List
def display(self):
cur=self.head
while cur.next!=None:
print(cur.data,end=" ")
cur=cur.next
print(cur.data,end=" ")
//To remove a node from a specific position in Linked List
def remove(self,pos):
cur = self.head
c = 1
while cur.next != None:
cur = cur.next
c = c + 1
if pos==1:
mur=self.head
mur=mur.next
self.head=mur
if pos==c:
cur = self.head
mur=cur.next
while mur.next!=None:
cur=mur
mur=cur.next
cur.next=None
else:
cur=self.head
mur=cur.next
c = 1
while cur.next != None:
if pos-1==c:
cur.next=mur.next
cur = cur.next
mur = cur.next
c = c + 1
//To Find data in Linked List
def find(self,data):
cur=self.head
f=0
index=1
while cur.next!=None:
if int(cur.data)==data:
f=1
break
cur=cur.next
index+=1
if int(cur.data) == data:
f = 1
if f==1:
print(f"Your Data Found at {index}")
else:
print("Your Data Not Found!!!!")
//To create menu
while True:
choose = (int(input("\n1)create\n2)Add node\n3)Display\n4)Delete node\n5)Find\n6)Exit.\nEnter your choice:")))
if choose == 1:
mylist = linking()
if choose==2:
mylist.add(input("Enter Data:"))
if choose==3:
mylist.display()
if choose==4:
mylist.remove(int(input("Enter Position:")))
if choose==5:
data=int(input("Enter Data: "))
mylist.find(data)
if choose==6:
break