DSPLAb
DSPLAb
print(l1)
d1={"a":134,"b":266,"c":343}
print(d1)
t1=(10,20,30,40,50,40)
print (t1)
s1={10,30,20,40}
print(s1)
Program:
print("List")
print(l1)
print("Dictionary")
d1={"a":134,"b":266,"c":343}
print(d1)
print("Tuples")
t1=(10,20,30,40,50,40)
print (t1)
print("Sets")
s1={10,30,20,40}
print(s1)
Output:
List
Dictionary
Tuples
Sets
Algorithm:
Program:
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])
return f[n]
Output:-
Step2: else
f=return(n*fact(n-1))
Step3: return f
Program:
def fact(n):
if n == 1:
return 1
else:
return(n*fact(n-1))
Output:
Algorithm:
Step1: Given a sorted list of a[] of n elements, search a given element x in list.
Step2: Search a sorted list by repeatedly dividing the search interval in half. Begin with an
interval
Step3: If the search key is less than the item in the middle item, then narrow the interval to the
Step4: Repeat the procedure until the value is found or the interval is empty
Program:
import time
def binarysearch(a,key):
low = 0
high= len(a)-1
mid = (high+low)//2
if a[mid] == key:
return mid
high = mid - 1
else:
low = mid + 1
return - 1
start = time.time()
a = [13,24,35,46,57,68,79]
r = binarysearch(a,k)
if r == -1:
print("Search Unsuccessful")
else:
end = time.time()
Output:-
'the array elements are:', [13, 24, 35, 46, 57, 68, 79])
Algorithm:
Step1: Given a list of n elements and search a given element x in the list using linear search.
Step2: Start from the leftmost element of list a[] and one by one compare x with each element of
list a[].
Program:
for i in range(len(arr)):
if arr[i] == n:
return i
return -1
n = 15
r = linear_search(arr, n)
if(r == -1):
else:
Output:
Algorithm:
compare to the element before, Move to the greater elements one position up to make space for
the swapped element.
Program:
def insertion_sort(list1):
value = list1[i]
j=i-1
list1[j + 1] = list1[j]
j -= 1
list1[j + 1] = value
return list1
Output:
Stack Operations:
Algorithm:
Step1: Stack is a linear data structure which works under the principle of last in first out. Basic
operations: push, pop, display.
Otherwise printing the element at the top of the stack and decrementing the top value by doing
the top.
Step4: DISPLAY: If (top==0), display Stack is empty. Otherwise printing the elements in the
stack from stack
Program:
s = []
def push():
if len(s) == size:
print("Stack is Full")
else:
def pop():
if(len(s) == 0):
print("Stack is Empty")
else:
item = s[-1]
del(s[-1])
def display():
size = len(s)
if(size == 0):
print("Stack is Empty")
else:
for i in reversed(s):
print(i)
while(True):
if(choice == 1):
push()
elif(choice == 2):
pop()
elif(choice == 3):
display()
else:
break
Output:
40
30
20
10
Algorithm:
queue = []
queue.append('a')
queue.append('b')
queue.append('c')
print(queue.pop(0))
print(queue.pop(0))
print(queue.pop(0))
# Initializing a queue
queue = []
queue.append('a')
queue.append('b')
queue.append('c')
print("Initial queue")
print(queue)
print(queue.pop(0))
print(queue.pop(0))
print(queue.pop(0))
print(queue)
Output:
Initial queue
[]
9.Python Program To Implement solution for towers of Hanoi.
Rules for towers of Hanoi.
2.Each move consists of taking the upper disk from one of the stacks and placing it on top of
another stack
Algorithm:
Program:
def towerofhanoi(n,source,destination,auxiliary):
if n == 1:
return
towerofhanoi(n-1,source,auxiliary,destination)
towerofhanoi(n-1,auxiliary,destination,source)
n=4
towerofhanoi(n,'A','B','C')
Output:-
Step2: count=0;
Step5: count=count+1;
Step8: END
Program:
class Node:
self.dataval = dataval
self.nextval = None
class SLinkedList:
def __init__(self):
self.headval = None
def listprint(self):
printval = self.headval
print (printval.dataval)
printval = printval.nextval
list = SLinkedList()
list.headval = Node("Mon")
e2 = Node("Tue")
e3 = Node("Wed")
list.headval.nextval = e2
e2.nextval = e3
list.listprint()
Output:
Mon
Tue
Wed