# Declaring Node class
class Node:
def __init__(self, node_address):
self.address = node_address
self.next = None
# Creating empty list
forwarding_list = Node()
# Recurssive Method for add address
def addAddress(node):
if node.address == old_address: # Address is same
if node.next == None: # Next is None
node.next = Node(new_address) # Add Next
print("Added") # Addedd
else: # Next is not none
print("Entry already exists")
elif node.next != None: # Check Next Node
addAddress(node.next, old_address, new_address)
def addEntry(forwarding_list):# Method for add entry
old_address = input() # old address
new_address = input() # New address
if forwarding_list.address == None: # If list is new
forwarding_list.address = old_address
forwarding_list.next = Node(new_address)
print("Added")
else: # Add using recurssive method
addAddress(forwarding_list, old_address, new_address)
def deleteAddress(node, old_address): # Recurssive method for
delete
if node.address == old_address: # This is old address node
node.next = None; # Set next node as None
print("Deleted")
elif node.next != None: # Try next Node
deleteAddress(node.next, old_address)
def deleteEntry(forwarding_list):
old_address = input()
if forwarding_list != None:
deleteAddress(forwarding_list, old_address)
def sendMail(node, address): # Recurssive method for send
mail
if node.next == None: # This is end of Node
print("Send to", node.address)
else: # Move to next Node
sendMail(node.next, address)
def sendMailEntry(forwarding_list): # Send Mail
address = input() # Take Address
sendMail(forwarding_list, address)
# Print Menu
while(True): # Infinty Loop
menu = input() # Take Input
if menu == "ADD":
addEntry(forwarding_list)
elif menu == "DELETE":
deleteEntry(forwarding_list)
elif menu == "MAIL":
sendMailEntry(forwarding_list)
elif menu == "QUIT":
print("Goodbye")
break
else:
print("Wrong entry, Try Again!")
I'm having trouble with certain inputs
Input
ADD X Y ADD C Z ADD P Q ADD P Q QUIT
Your output
Added
Goodbye
Expected output
Added
Added
Added
Entry already exists Goodbye
Input
MAIL X ADD P Q MAIL P QUIT
Your output
Send to None
Added
Send to Q
Goodbye
Expected output
Send to X
Added
Send to Q
Goodbye
Please help in python
I updated the code according to your requirement as it had some semantic and syntactical errors/bugs. I will explain the changes I made so that you could manipulate it the way you want. First of all I made some assumptions while playing around with the code:
1) forwarding_list is a python list which will store all forwarding routes. e.g. [ 'A'=>'B' , 'X'=>'Y' , ...,].
2) Each route will only contain a linked list of two entries. 'old_address' => 'new_address'.
3) New address is not being forwarded to some another new address. If this assumption does not hold, linked list has to be updated accordingly. Please update the code in this case.
4) If an entry does not exist in forwarding_list then mail is directly sent to that address assuming it is correct.
5) I have added another SHOW option in main menu to show the contents of forwarding_list for better visualization.
6) The input output format provided in question does not match with the input taken in code. So below I am attaching screenshot of updated I/O format.
Sample Input 1:
Sample Input 2:
class Node:
def __init__(self, node_address):
self.address = node_address
self.next = None
def addAddress(forwarding_list,old_address,new_address):
for node in forwarding_list:
if node.address==old_address:
if
node.next==None:
node.next=Node(new_address)
print("Added")
return
else:
print("Entry already exists")
return
n=Node(old_address)
n.next=Node(new_address)
forwarding_list.append(n)
print("Added")
return
def addEntry(forwarding_list):# Method for add entry
old_address = input("Enter old address: ") # old
address
new_address = input("Enter new address: ") # New
address
if len(forwarding_list) == 0: # If list is new
n=Node(old_address)
n.next=Node(new_address)
forwarding_list.append(n)
print("Added")
else: # Add using recurssive method
addAddress(forwarding_list,old_address,new_address)
return
def deleteAddress(forwarding_list, old_address): # Recurssive
method for delete
for node in forwarding_list:
if node.address == old_address: #
This is old address node
node.next =
None; # Set next node as None
print("Deleted")
return
return
def deleteEntry(forwarding_list):
old_address = input("Enter old address: ")
if len(forwarding_list) != 0:
deleteAddress(forwarding_list,
old_address)
return
def sendMail(forwarding_list, address): # Recurssive method for
send mail
for node in forwarding_list:
if node.address==address:
if
node.next!=None:
print("Send to",node.next.address)
return
else:
print("Send to",node.address)
return
print("Send to",address)
return
def sendMailEntry(forwarding_list): # Send Mail
address = input("Enter address to mail to: ") # Take
Address
sendMail(forwarding_list, address)
# Creating empty list
forwarding_list = []
#Display Menu
print("\n1.ADD\n2.DELETE\n3.MAIL\n4.QUIT\n5.SHOW\nEnter your
choice!\n")
while(True): # Infinty Loop
menu = input().strip() # Take Input
if menu == "ADD":
addEntry(forwarding_list)
elif menu == "DELETE":
deleteEntry(forwarding_list)
elif menu == "MAIL":
sendMailEntry(forwarding_list)
elif menu == "QUIT":
print("Goodbye")
break
elif menu=="SHOW":
for node in forwarding_list:
if node!=None
and node.next!=None:
print(node.address,"->",node.next.address)
else:
print("Wrong entry, Try
Again!")
print("\nEnter your choice\n")
Below I am attaching screenshots of code for Indentation purpose:
Thank You, Hope that helps!!
# Declaring Node class class Node: def __init__(self, node_address): self.address = node_address self.next = None #...
PYTHON
--------------------------------------------------------
class LinkedList:
def __init__(self):
self.__head = None
self.__tail = None
self.__size = 0
# Return the head element in the list
def getFirst(self):
if self.__size ==
0:
return
None
else:
return
self.__head.element
# Return the last element in the list
def getLast(self):
if self.__size ==
0:
return
None
else:
return
self.__tail.element
# Add an element to the beginning of the
list
def addFirst(self, e):
newNode = Node(e) #
Create a new node
newNode.next =
self.__head # link...
Previous code:
class BinarySearchTree:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def search(self, find_data):
if self.data == find_data:
return self
elif find_data < self.data and self.left != None:
return self.left.search(find_data)
elif find_data > self.data and self.right != None:
return self.right.search(find_data)
else:
return None
def get_left(self):
return self.left
def get_right(self):
return self.right
def set_left(self, tree):
self.left = tree
def set_right(self, tree):
self.right = tree
def set_data(self, data):
self.data = data
def get_data(self):
return self.data
def traverse(root,order):...
class BinaryTree:
def __init__(self, data, left=None, right=None):
self.__data = data
self.__left = left
self.__right = right
def insert_left(self, new_data):
if self.__left == None:
self.__left = BinaryTree(new_data)
else:
t = BinaryTree(new_data, left=self.__left)
self.__left = t
def insert_right(self, new_data):
if self.__right == None:
self.__right = BinaryTree(new_data)
else:
t = BinaryTree(new_data, right=self.__right)
self.__right = t
def get_left(self):
return self.__left
def get_right(self):
return self.__right
def set_data(self, data):
self.__data = data
def get_data(self):
return self.__data
def set_left(self, left):
self.__left = left
def set_right(self, right):
self.__right...
COMPLETE THE _CONSTRUCT() AND CONSTRUCTTREE() FUNCTIONS class expressionTree: class treeNode: def __init__(self, value, lchild, rchild): self.value, self.lchild, self.rchild = value, lchild, rchild def __init__(self): self.treeRoot = None #utility functions for constructTree def mask(self, s): nestLevel = 0 masked = list(s) for i in range(len(s)): if s[i]==")": nestLevel -=1 elif s[i]=="(": nestLevel += 1 if nestLevel>0 and not (s[i]=="(" and nestLevel==1): masked[i]=" " return "".join(masked) def isNumber(self, expr): mys=s.strip() if len(mys)==0 or not isinstance(mys, str): print("type mismatch error: isNumber")...
class Node(object): def __init__(self, data, next=None): self.data = data self.next = next class List(object): def __init__(self): self.head = None self.tail = None Implement the following functions for Linked List in Python and use the constructors above : Copy(lList) Builds and returns a copy of list ItemAt(List,i) Returns the data item at position i in list Pop(List,i=0) Remove item at position i in list. If i is not specified, it removes the first item in list Count(List,x) Returns the number...
Python question class LinkNode: def __init__(self,value,nxt=None): assert isinstance(nxt, LinkNode) or nxt is None self.value = value self.next = nxt Question 2.1. Empty Node In some cases in it convenient to have a notion of an empty linked list. Usually it means that the linked list does not have any elements in it. In order to keep things simple (for now) we will assume that the list is empty, if it has a single node and its value is None. Add...
python programming: Can you please add comments to describe in detail what the following code does: import os,sys,time sl = [] try: f = open("shopping2.txt","r") for line in f: sl.append(line.strip()) f.close() except: pass def mainScreen(): os.system('cls') # for linux 'clear' print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%") print(" SHOPPING LIST ") print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%") print("\n\nYour list contains",len(sl),"items.\n") print("Please choose from the following options:\n") print("(a)dd to the list") print("(d)elete from the list") print("(v)iew the...
I am trying to finish the code below. class DVD: def __init__(self, title, dvd_type, cost): self.title = title self.dvd_type = dvd_type self.cost = cost def setTitle(self, netTitle): self.title = newTitle def getTitle(self): return self.title def getType(self): return self.dvd_type def getCost(self): return self.cost def setCost(self, newCost): self.cost = newCost def setType(self, newdvd_Type): validTypes = ['Game', 'Word', 'Compiler', 'Spreadsheet', 'dBase', 'Presentation'] while True: self.dvd_type = input( 'Please enter the valid type: (Game, Word, Compiler, Spreadsheet, dBase, Presentation)') if self.dvd_type() not in validTypes:...
in
python and according to this
#Creating class for stack
class My_Stack:
def __init__(self):
self.items = []
def isEmpty(self):
return self.items == []
def Push(self, d):
self.items.append(d)
def Pop(self):
return self.items.pop()
def Display(self):
for i in reversed(self.items):
print(i,end="")
print()
s = My_Stack()
#taking input from user
str = input('Enter your string for palindrome checking:
')
n= len(str)
#Pushing half of the string into stack
for i in range(int(n/2)):
s.Push(str[i])
print("S",end="")
s.Display()
s.Display()
#for the next half checking the upcoming string...
PYTHON 3.8 class student(): def __init__(self, name = 'Bill', grade = 9, subjects = ['math','science']): self.name = name self.grade = grade self.subjects = subjects def add_subjects(self): print('Current subjects:') print(self.subjects) more_subjects = True while more_subjects == True: subject_input = input('Enter a subject to add: ') if subject_input == '': more_subjects = False print(self.subjects) else: self.subjects.append(subject_input) print(self.subjects) m = student()...