Ds 1
Ds 1
pop()
price=[] print("amount : ",popped_total)
total=0 total=0
popped_total=0 print("Item le in basket : ")
while(True) : for item in product:
item=input("Enter name (q to quit) : ") print(item)
if(item=="q") : for amount in price:
break total=total+amount
else : print("amount le for item in basket : ",total)
product.append(item)
amount=int(input("Enter a price : "))
price.append(amount)
total=total+amount
product.reverse()
print("Your list names : ")
x=len(product)
for item in product :
print(item)
print("Tot amount : ",total)
n=int(input("Enter no of items to pop : "))
product.reverse()
for item in product :
if((len(product)-n)==x):
break
else:
print("pop item is : ",product[len(product)-1])
print("price : ",price[len(price)-1])
popped_total=popped_total+price[len(price)-1]
product.pop()
2… class Node: return head
def __init__(self, data): def delete_at_first(head):
self.data = data if not head:
self.next = None return None
def insert_at_first(head, data): return head.next
new_node = Node(data) def delete_at_last(head):
new_node.next = head if not head or not head.next:
return new_node return None
def insert_at_last(head, data): current = head
new_node = Node(data) while current.next and current.next.next:
if not head: current = current.next
return new_node current.next = None
current = head return head
while current.next: def delete_in_middle(head, posi on):
current = current.next if not head or posi on == 0:
current.next = new_node return head.next
return head current = head
def insert_in_middle(head, data, posi on): for _ in range(posi on - 1):
new_node = Node(data) if not current.next:
if not head or posi on == 0: return head
new_node.next = head current = current.next
return new_node if not current.next:
current = head return head
for _ in range(posi on - 1): current.next = current.next.next
if not current.next: return head
break def search(head, key):
current = current.next current = head
new_node.next = current.next posi on = 0
current.next = new_node while current:
if current.data == key: data = input("Enter name to insert at end: ")
return posi on head = insert_at_last(head, data)
current = current.next elif choice == 3:
posi on += 1 data = input("Enter name to insert in middle: ")
return -1 posi on = int(input("Enter posi on to insert: "))
def display_list(head): head = insert_in_middle(head, data, posi on)
current = head elif choice == 4:
while current: head = delete_at_first(head)
print(current.data) elif choice == 5:
print() head = delete_at_last(head)
current = current.next elif choice == 6:
# Driver code posi on = int(input("Enter posi on to delete: "))
head = None head = delete_in_middle(head, posi on)
while True: elif choice == 7:
print("\nMenu:") key = input("Enter name to search: ")
print("1. Insert at first") posi on = search(head, key)
print("2. Insert at last") if posi on != -1:
print("3. Insert in Middle") print(f"Passenger found at posi on: {posi on}")
print("4. Delete at first") else:
print("5. Delete at last") print("Passenger not found")
print("6. Delete in Middle") elif choice == 8:
print("7. Search") display_list(head)
print("8. Display") elif choice == 9:
print("9. Exit") break
choice = int(input("Enter your choice: ")) else:
if choice == 1: print("Invalid choice, please try again.")
data = input("Enter name to insert at beginning: ")
head = insert_at_first(head, data)
elif choice == 2:
3… phone_directory = {} if choice == "1":
def add_contact(name, phone_number): name = input("Enter the name: ")
phone_directory[name] = phone_number phone_number = input("Enter phone number: ")
def search_contact(name): add_contact(name, phone_number)
if name in phone_directory: print("number added successfully!")
return phone_directory[name] elif choice == "2":
else: name = input("Enter the name to search: ")
return "Contact not found" result = search_contact(name)
def remove_contact(name): print("Phone number:", result)
if name in phone_directory: elif choice == "3":
del phone_directory[name] display_directory()
print("number removed successfully!") elif choice == "4":
else: name = input("Enter the name to remove: ")
print("Contact not found") remove_contact(name)
def display_directory(): elif choice == "5":
if not phone_directory: break
print("directory is empty.") else:
else: print("Invalid choice. Please try again.")
print("Phone Directory:")
for name, phone_number in phone_directory.items():
print(f"{name}: {phone_number}")
while True:
print("Phone Directory Menu:")
print("1.create new contact")
print("2.Search for a contact")
print("3.Display directory")
print("4.Remove a contact")
print("5.Exit")
choice = input("Enter choice: ")
4… def bubble_sort(grades): 5… import hashlib
n = len(grades) document = input("Enter the document you want to sign: ")
for i in range(n): private_key = input("Enter your private key: ")
swapped = False document_hash = hashlib.sha256(document.encode()).hexdigest()
for j in range(0, n - i - 1): signature = document_hash + private_key
if grades[j][1] < grades[j+1][1]: print("Document signed. Signature:", signature)
grades[j], grades[j+1] = grades[j+1], grades[j] document_to_verify = input("Enter the document for verifica on: ")
swapped = True private_key_for_verifica on = input("Enter the private key for verifica on: ")
if not swapped: verifica on_hash = hashlib.sha256(document_to_verify.encode()).hexdigest()
break expected_signature = verifica on_hash + private_key_for_verifica on
def sort_student_grades(): if expected_signature == signature:
student_grades = [] print("Signature is valid.")
num_students = int(input("Enter the number of students: ")) else:
for _ in range(num_students): print("Signature is invalid.")
name = input("Enter student name: ")
grade = int(input(f"Enter grade for {name}: "))
student_grades.append((name, grade))
bubble_sort(student_grades)
print("\nSorted student grades:")
for name, grade in student_grades:
print(f"{name}: {grade}")
if __name__ == "__main__":
sort_student_grades()
6… import heapq def get_shortest_path(self, previous_nodes, end_node):
class WeightedGraph: path = []
def __init__(self): current = end_node
self.graph = {} while current is not None:
def add_edge(self, node1, node2, weight): path.append(current)
if node1 not in self.graph: current = previous_nodes.get(current)
self.graph[node1] = [] return path[::-1]
if node2 not in self.graph: def main():
self.graph[node2] = [] g = WeightedGraph()
self.graph[node1].append((node2, weight)) g.add_edge("home", "store", 3)
self.graph[node2].append((node1, weight)) g.add_edge("store", "medical",2)
def start(self, start_node): g.add_edge("home", "atm", 10)
if start_node not in self.graph: g.add_edge("atm", "bank", 2)
return None, None g.add_edge("medical", "temple", 4)
distances = {node: float('inf') for node in self.graph} g.add_edge("temple", "park", 1)
distances[start_node] = 0 g.add_edge("park", "mall", 7)
priority_queue = [(0, start_node)] g.add_edge("home", "", 13)
previous_nodes = {node: None for node in self.graph} start = input("star ng node: ")
while priority_queue: end = input("des na on node: ")
current_distance, current_node = heapq.heappop(priority_queue) distances, previous_nodes = g.start(start)
if current_distance > distances[current_node]: if end in distances:
con nue path = g.get_shortest_path(previous_nodes, end)
for neighbor, weight in self.graph[current_node]: distance = distances[end]
distance = current_distance + weight print(f"Shortest path from {start} to {end}: {' -> '.join(path)}, Distance: {distance} KM")
if distance < distances[neighbor]: else:
distances[neighbor] = distance print(f"No path found {start} to {end}.")
heapq.heappush(priority_queue, (distance, neighbor)) if __name__ == "__main__":
previous_nodes[neighbor] = current_node main()
return distances, previous_nodes