Ammar Python Assignment
Ammar Python Assignment
Ammar Python Assignment
ENROLLMENT NO = 18021180065
PYTHON ASSIGNMENT
LIST AND LINKED LIST
CODE:
l=[3,2,1,4]
l.insert(1,6)
print(l)
l.remove(6)
print(l)
l.append(6)
print(l)
l.sort()
print(l)
l.pop()
print(l)
l.reverse()
print(l)
NAME = AMMAR AHSAN ADMISSION NO = 18SCSE1180067
ENROLLMENT NO = 18021180065
CODE:
class Node:
def __init__(self,data):
self.data=data
self.next=None
class LinkedList:
def __init__(self):
self.start=None
def insert_at_beg(self,value):
newNode = Node(value)
temp= self.start
self.start = newNode
self.start.next = temp
def insert_at_last(self,value):
newNode = Node(value)
if(self.start == None):
self.start = newNode
else:
temp = self.start
while(temp.next!=None):
temp = temp.next
temp.next= newNode
def insert_after_node(self,prev_node,data):
newNode = Node(data)
if(not prev_node):
print("Node not found")
else:
temp = prev_node.next
prev_node.next = newNode
newNode.next = temp
NAME = AMMAR AHSAN ADMISSION NO = 18SCSE1180067
ENROLLMENT NO = 18021180065
def delete_at_first(self):
if(self.start == None):
print("Linked List is empty")
else:
self.start = self.start.next
def delete_at_last(self):
if(self.start == None):
print("List is empty")
else:
temp= self.start
while(temp.next.next!=None):
temp= temp.next
temp.next= None
def viewList(self):
if(self.start==None):
print("List is empty")
else:
temp = self.start
while(temp!=None):
print(temp.data,end=" ")
temp = temp.next
def len_list_iterative(self):
temp = self.start
count = 0
while(temp!=None):
count = count+1
temp = temp.next
return count
def len_recursive(self,node):
if node is None:
return 0
return 1+ self.len_recursive(node.next)
def node_swap(self,key1,key2):
if(key1 == key2):
return
else:
temp = self.start
prev1 =None
prev2 = None
while(temp.next!= None):
NAME = AMMAR AHSAN ADMISSION NO = 18SCSE1180067
ENROLLMENT NO = 18021180065
if(temp.next.data == key1):
prev1 = temp
elif(temp.next.data == key2):
prev2 = temp
temp = temp.next
link = prev1.next.next
prev1.next.next = prev2.next.next
prev2.next.next = link
linked_list = LinkedList()
linked_list.insert_at_last(8)
linked_list.insert_at_last(10)
linked_list.insert_at_last(12)
linked_list.insert_at_last(13)
linked_list.viewList()
linked_list.delete_at_first()
print("\n")
linked_list.viewList()
print("\n")
linked_list.insert_at_beg(15)
linked_list.insert_at_beg(20)
linked_list.viewList()
print("\n")
print("\n")
prev_node = linked_list.start.next
linked_list.insert_after_node(prev_node,7)
linked_list.viewList()
print("\n")
print(linked_list.len_list_iterative())
print(linked_list.len_recursive(linked_list.start))
linked_list.node_swap(15,12)
linked_list.viewList()
linked_list.delete_at_last()
print("\n")
linked_list.viewList()
NAME = AMMAR AHSAN ADMISSION NO = 18SCSE1180067
ENROLLMENT NO = 18021180065
CODE:
marks=[89,78,99,76,77,67,99,98,90]
marks.insert(8,78)
print(marks)
print(marks[5])
print(marks[7])
NAME = AMMAR AHSAN ADMISSION NO = 18SCSE1180067
ENROLLMENT NO = 18021180065
4.Given two lists, both having String elements, write a python program
using python lists to create a new string as per the rule given below: The
first element in list1 should be merged with last element in list2, second
element in list1 should be merged with second last element in list2 and
so on. If an element in list1/list2 is None, then the corresponding
element in the other list should be kept as it is in the merged list.
Sample Inputlist1=['A', 'app','a', 'd', 'ke', 'th', 'doc', 'awa']
list2=['y','tor','e','eps','ay',None,'le','n']
Expected Output : “An apple a day keeps the doctor away”
CODE:
list1=['A', 'app','a', 'd', 'ke', 'th', 'doc', 'awa']
list2=['y','tor','e','eps','ay',None,'le','n']
for i in range(len(list1)):
if(list1[i]==None):
list1[i]=''
for i in range(len(list2)):
if(list2[i]==None):
list2[i]=''
st=''
for i in range(len(list1)):
st+=list1[i]+list2[::-1][i]+' '
print(st)
5.
Given two lists, both having integer elements, write a python program
using python lists to create and return a new list as per the rule given
below: If the double of an element in list1 is present in list2, then add it
to the new list. Sample Input list1 - [11, 8,23,7,25, 15] list2 – [6, 33,
50,31, 46, 78, 16,34] Expected Output : new_list – [8,23,25]
CODE:
NAME = AMMAR AHSAN ADMISSION NO = 18SCSE1180067
ENROLLMENT NO = 18021180065
Source Code:
import math
class Node:
def __init__(self, data):
self.data = data
self.next = None
def sum_node(head):
count = 0
sum = 0
while (head != None):
if (count % 2 == 0):
sum = sum + head.data
count = count + 1
head = head.next
return sum
new_node = Node(new_data)
new_node.data = new_data
new_node.next = head_ref
head_ref = new_node
return head_ref
if __name__=='__main__':
head = None
head = push(head, 11)
head = push(head, 21)
head = push(head, 31)
head = push(head, 41)
head = push(head, 51)
print(sum_node(head))
output:-
class LL:
def __init__(self):
self.head = None
def insert(self, data):
if self.head == None:
self.head = Node(data)
else:
p = Node(data)
p.next = self.head
self.head = p
p = self.head
q = None
max = -1
while p!=None:
if p.data>max:
max = p.data
q=p
p = p.next
q.data = k
def display(self):
p = self.head
while p!=None:
print(p.data)
p = p.next
l = LL()
l.insert(10)
l.insert(20)
l.insert(30)
l.insert(100)
l.insert(50)
l.insert(60)
print("Old List")
l.display()
l.replace_max(23)
print("\nNew List")
l.display()
output:-
NAME = AMMAR AHSAN ADMISSION NO = 18SCSE1180067
ENROLLMENT NO = 18021180065
Q.10 Write a python program to remove all duplicate elements from a sorted linked
list containing integer data. Use the LinkedList class and methods in it to implement
the above program. Example: Input LinkedList: 10 10 20 20 30 30 30 40 50 Output
LinkedList: 10 20 30 40 50
CODE:-
class Node:
def _init_(self, data):
self.data = data
self.next = None
class LinkedList:
def _init_(self):
self.head = None
self.last_node = None
def display(self):
current = self.head
while current:
print(current.data, end = ' ')
current = current.next
def remove_duplicates(llist):
current1 = llist.head
NAME = AMMAR AHSAN ADMISSION NO = 18SCSE1180067
ENROLLMENT NO = 18021180065
while current1:
data = current1.data
current2 = current1.next
while current2:
if current2.data == data:
llist.remove(current2)
current2 = current2.next
current1 = current1.next
a_llist = LinkedList()
data_list = input().split()
for data in data_list:
a_llist.append(int(data))
remove_duplicates(a_llist)
a_llist.display()
OUTPUT:-
SOURCE CODE:-
stack = []
stack.append('a')
stack.append('b')
stack.append('c')
print('Initial stack')
print(stack)
print('\nElements poped from stack:')
print(stack.pop())
print(stack.pop())
print(stack.pop())
while True:
do = input('What would you like to do? ').split()
operation = do[0].strip().lower()
if operation == 'push':
s.push(int(do[1]))
elif operation == 'pop':
ifs.is_empty():
print('Stack is empty.')
else:
print('Popped value: ', s.pop())
elif operation == 'disp':
s.display();
elif operation == 'quit':
break
OUTPUT:-
NAME = AMMAR AHSAN ADMISSION NO = 18SCSE1180067
ENROLLMENT NO = 18021180065
def __init__(self):
self.front = self.rear = None
def isEmpty(self):
return self.front == None
def EnQueue(self, item):
temp = Node(item)
if self.rear == None:
self.front = self.rear = temp
return
self.rear.next = temp
self.rear = temp
def DeQueue(self):
if self.isEmpty():
return
temp = self.front
self.front = temp.next
if(self.front == None):
self.rear = None
if __name__== '__main__':
q = Queue()
q.EnQueue(10)
q.EnQueue(20)
q.DeQueue()
q.DeQueue()
q.EnQueue(30)
q.EnQueue(40)
q.EnQueue(50)
q.DeQueue()
print("Queue Front " + str(q.front.data))
print("Queue Rear " + str(q.rear.data))
OUTPUT:-
NAME = AMMAR AHSAN ADMISSION NO = 18SCSE1180067
ENROLLMENT NO = 18021180065
NAME = AMMAR AHSAN ADMISSION NO = 18SCSE1180067
ENROLLMENT NO = 18021180065
print(q.deQueue())
print(q.deQueue())
print(q.deQueue())
OUTPUT:-
NAME = AMMAR AHSAN ADMISSION NO = 18SCSE1180067
ENROLLMENT NO = 18021180065
def pop(self):
if (self.q1.empty()):
return
self.q1.get()
self.curr_size -= 1
def top(self):
if (self.q1.empty()):
return -1
return self.q1.queue[0]
def size(self):
return self.curr_size
if __name__ == '__main__':
s = Stack()
s.push(1)
s.push(2)
s.push(3)
s.push(4)
OUTPUT:-
NAME = AMMAR AHSAN ADMISSION NO = 18SCSE1180067
ENROLLMENT NO = 18021180065
processes = [1, 2, 3]
n=3
burst_time = [10, 5,8]
arrival_time = [0, 0,0
]
findavgTime(processes, n, burst_time,arrival_time)
OUTPUT:-