Ds - With - Pyton - FinalPrint
Ds - With - Pyton - FinalPrint
OUTPUT
class date:
def init (self,a,b,c):
self.d=a
self.m=b
self.y=c
def day(self):
print("Day = ", self.d)
def month(self):
print("Month = ", self.m)
def year(self):
print("year=",self.y)
def monthName(self):
months = ["Unknown","January","Febuary","March","April","May","June","July",
"August","September","October","November","December"]
print("Month Name:",months[self.m])
def isLeapYear(self):
if (self.y % 400 == 0) and (self.y % 100 =0):
print("It is a Leap year")
elif (self.y % 4 == 0) and (self.y % 100 != 0):
print("It is a Leap year")
else:
print("It is not a Leapyear")
d1 = date(3,8,2000)
d1.day()
d1.month()
d1.year()
d1.monthName()
d1.isLeapYear()
OUTPUT
OUTPUT
import time
import matplotlib.pyplot as plt
start=time.time()
def linearsearch(a, key):
n = len(a)
for i in range(n):
if a[i] == key:
return i;
return -1
a = [13,24,35,46,57,68,79]
start = time.time()
print("the array elements are:",a)
k = int(input("enter the key element to search:"))
i = linearsearch(a,k)
if i == -1:
print("Search UnSuccessful")
else:
print("Search Successful key found at location:",i+1)
end = time.time()
print("Runtime of the program is", end-start)
x=list(range(1,1000))
plt.plot(x ,[y for y in x] )
plt.title("Linear Search- Time Complexity is O(n)")
plt.xlabel("Input")
plt.ylabel("Time")
plt.show()
OUTPUT
import time
import matplotlib.pyplot as plt
def bubblesort(a):
n = len(a)
for i in range(n-1):
for j in range(n-1-i):
if a[j]>a[j+1]:
temp = a[j]
a[j] = a[j+1]
a[j+1] = temp
x = [34,46,43,27,57,41,45,21,70]
print("Before sorting:",x)
bubblesort(x)
print("After sorting:",x)
x=list(range(1,10000))
plt.plot(x,[y*y for y in x] )
plt.title(”Bubble Sort- time complexity is 0(n2)")
p1t.x1abel("Input")
p1t.ylabel("Time")
p1t.show()
OUTPUT
OUTPUT
import time
import matplotlib.pyplot as plt
def insertionsort(a):
n = len(a)
for i in range(1,n-1):
v=a[i]
j = i-1
while j>=0 and a[j]>v:
a[j+1] = a[j]
j=j-1
a[j+1] = v
x = [34,46,43,27,57,41,45,21,70]
print("Before sorting:",x)
insertionsort(x)
print("After sorting:",x)
x=list(range(1,10000))
plt.plot(x,[y for y in x] )
p1t.title("Insertion Sort- time complexity is O(n)")
p1t.x1abel("Input")
p1t.ylabel("Time")
plt.show()
OUTPUT
OUTPUT
OUTPUT
import time
import matplotlib.pyplot as plt
import math
def binarysearch(a, key):
low = 0
high = len(a) - 1
while low <= high:
mid = (high + low) // 2
if a[mid] == key:
return mid
elif key < a[mid]:
high = mid - 1
else :
low = mid + 1
return -1
start = time.time()
a = [13,24,35,46,57,68,79]
print("the array elements are:",a)
k = int(input("enter the key element to search:"))
r = binarysearch(a,k)
if r == -1:
print("Search UnSuccessful")
else:
print("Search Successful key found at location:",r+1)
end = time.time()
print("Runtime of the program is:", end-start)
x=list(range(1,500))
plt.plot(x ,[y*math.log(y,2) for y in x] )
plt.title("Binary Search- Time Complexity is O(log n)")
plt.show()
OUTPUT
def fib(n):
if n<=1:
return n
f = [0, 1]
for i in range(2, n+1):
f.append(f[i-1] + f[i-2])
print("The Fibonacci sequence is:",f)
return f[n]
n=int(input("Enter the term:"))
print("The Fibonacci value is:",fib(n))
OUTPUT
class Node:
def _init _(self, data =None):
self.data = data
self.next = None
class SinglyLinkedList:
def _init (self):
self.first = None
def removeFirst(self):
if(self.first== None):
print("list is empty")
else:
cur=self.first
self.first=self.first.next
print("the deleted item is",cur.data)
def display(self):
if(self.first== None):
print("list is empty")
return
current = self.first
while(current):
print(current.data, end = " ")
current = current.next
def search(self,item):
if(self.first== None):
print("list is empty")
return
current = self.first
found = False
while current != None and not found:
if current.data == item:
found = True
else:
current = current.next
if(found):
print("Item is present in the linkedlist")
else:
print("Item is not present in the linked list")
OUTPUT
class Node:
def__init__(self,data=None):
self.data = data
self.next = None
class LinkedList:
def init (self):
self.first = None
def insert(self,data):
temp = Node(data)
if(self.first):
current = self.first
while(current.next):
current = current.next
current.next = temp
else:
self.first = temp
def__iter__(self):
current = self.first
while current:
yield current.data
current = current.next
s = []
def push():
if len(s) == size:
print("Stack is Full")
else:
item = input("Enter the element:")
s.append(item)
def pop():
if(len(s) == 0):
print("Stack is Empty")
else:
item = s[-1]
del(s[-1])
print("The deleted element is:",item)
def display():
if(len(s)== 0):
print("Stack is Empty")
else:
for i in reversed(s):
print(i)
size=int(input("Enter the size of Stack:"))
while(True):
choice = int(input("1-Push 2-POP 3-DISPLAY 4-EXIT Enter your choice:"))
if(choice == 1):
push()
elif(choice == 2):
pop()
elif(choice == 3):
display()
else:
break
OUTPUT
def bracketmatching(expr):
stack = []
for char in expr:
if char in ["(", "{", "["]:
stack.append(char)
else:
if not stack:
return False
current_char = stack.pop()
if current_char == '(':
if char != ")":
return False
if current_char == '{':
if char != "}":
return False
if current_char == '[':
if char != "]":
return False
if stack:
return False
return True
expr = "{()}[]"
if bracketmatching(expr):
print("Matching")
else:
print("Not Matching")
OUTPUT
def fact(n):
if n == 1:
return 1
else:
return (n * fact(n-1))
n=int(input("Enter the number:"))
print("The factorial of a number is:",fact(n))
OUTPUT
b) Fibonacci
def fib(n):
if n<=1:
return n
return fib(n-1) + fib(n-2)
n=int(input("Enter the range:"))
print("The fibonacci value is:",fib(n))
OUTPUT
OUTPUT
q=[]
def enqueue():
if len(q)==size:
print("Queue is Full")
else:
item=input("Enter the element:")
q.append(item)
def dequeue():
if not q:
print("Queue is Empty")
else:
item=q.pop(0)
print("Element removed is:",item)
def display():
if not q:
print("Queue is Empty")
else:
print(q)
size=int(input("Enter the size of Queue:"))
while True:
choice=int(input("1.Insert 2.Delete 3. Display 4. Quit Enter your choice:"))
if choice==1:
enqueue()
elif choice==2:
dequeue ()
elif choice==3:
display()
else:
break
OUTPUT
class BSTNode:
def __init__(self,data):
self.data = data
self.leftChild = None
self.rightChild = None
def insertNode(root,value):
if root.data == None:
root.data = value
elif value < root.data:
if root.leftChild is None:
root.leftChild = BSTNode(value)
else:
insertNode(root.leftChild,value)
elif value >= root.data:
if root.rightChild is None:
root.rightChild = BSTNode(value)
else:
insertNode(root.rightChild,value)
return str(value)," The node has been successfully inserted "
def searchNode(root,value):
if value < root.data:
if root.leftChild is None:
return str(value)," NOT found in the tree"
return searchNode(root.leftChild,value)
elif value > root.data:
if root.rightChild == None:
return str(value)," NOT found in the tree"
return searchNode(root.rightChild,value)
else:
return str(value)," found in the tree"
def inOrder(root):
if not root:
return
inOrder(root.leftChild)
print(root.data,end="->")
inOrder(root.rightChild)
def preOrder(root):
if not root:
return
print(root.data,end="->")
preOrder(root.leftChild)
preOrder(root.rightChild)
def postOrder(root):
if not root:
return
postOrder(root.leftChild)
postOrder(root.rightChild)
print(root.data,end="->")
newTree = BSTNode(None)
print( insertNode(newTree,70) )
print( insertNode(newTree,50) )
print( insertNode(newTree,90) )
print( insertNode(newTree,30) )
print( insertNode(newTree,80) )
print( insertNode(newTree,100) )
print( insertNode(newTree,20) )
print( insertNode(newTree,40) )
print( insertNode(newTree,60) )
print(searchNode(newTree,20) )
print(searchNode(newTree,100))
print(searchNode(newTree,200))
OUTPUT
Q.put(source)
visited_vertices.append(source)
OUTPUT
stack.append(source)
visited_list.append(source)
while stack:
vertex=stack.pop()
print(vertex,end="->")
for u in input_graph[vertex]:
if u not in visited_list:
stack.append(u)
visited_list.append(u)
OUTPUT
OUTPUT