Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- item = eval(input("What number are you looking for? ")) #prompt the user for an input
- inputList = [2, 5, 10, 14, 18, 23, 23, 23, 25, 26, 29, 31, 35, 37] #this is the input list.
- #Created a basic list based on the assignment
- #but this can easily be adjusted to a user
- #input generated list
- class Node: #This is the node class that lets us adjust the ordered list
- def __init__(self,initdata):
- self.data = initdata
- self.next = None
- def getData(self): #gets the current value
- return self.data
- def getNext(self): #gets the next value
- return self.next
- def setData(self,newdata): #sets the current value
- self.data = newdata
- def setNext(self,newnext): #gets the next value
- self.next = newnext
- class orderedList:
- def __init__(self):
- self.head = None
- def add(self,item): #This allows us to add items to the ordered list
- temp = Node(item)
- temp.setNext(self.head)
- self.head = temp
- def search(self,item): #This searches the list, while increasing a variable for counting
- current = self.head
- amount = 0 #counting variable
- while current != None: #While you haven't reached the end of the list, continue iterating the list
- if current.getData() == item:
- amount = amount + 1
- current = current.getNext() #move to the next iteration
- else:
- current = current.getNext()
- return amount
- def count(self,item): #check the amount of times the user input was found
- if self.search(item) > 0: #if the value was found at least once, print this
- print (str(item) + " appearances : " + str(self.search(item)))
- if self.search(item) > 1: #If the value was found more than once, it's a duplicate, so print this.
- print (str(item) + " contains at least one duplicate entry")
- else:
- print (str(item) + " was not found") #If the value was not found at all, let the user know.
- myList = orderedList() #create an empty user list
- for num in inputList: #iterate through our inputList and add each entry to the ordered list
- myList.add(num)
- print(myList.count(item))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement