Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.23 KB | None | 0 0
  1.  
  2. item = eval(input("What number are you looking for? ")) #prompt the user for an input
  3.  
  4. inputList = [2, 5, 10, 14, 18, 23, 23, 23, 25, 26, 29, 31, 35, 37]  #this is the input list.
  5.                                                                     #Created a basic list based on the assignment
  6.                                                                     #but this can easily be adjusted to a user
  7.                                                                     #input generated list
  8.  
  9. class Node:                                                         #This is the node class that lets us adjust the ordered list
  10.     def __init__(self,initdata):
  11.         self.data = initdata
  12.         self.next = None
  13.        
  14.     def getData(self):                                              #gets the current value
  15.         return self.data
  16.        
  17.     def getNext(self):                                              #gets the next value
  18.         return self.next
  19.  
  20.     def setData(self,newdata):                                      #sets the current value
  21.         self.data = newdata
  22.  
  23.     def setNext(self,newnext):                                      #gets the next value
  24.         self.next = newnext
  25.  
  26. class orderedList:
  27.  
  28.     def __init__(self):
  29.         self.head = None
  30.        
  31.     def add(self,item):                                             #This allows us to add items to the ordered list
  32.         temp = Node(item)
  33.         temp.setNext(self.head)
  34.         self.head = temp
  35.    
  36.     def search(self,item):                                          #This searches the list, while increasing a variable for counting
  37.         current = self.head
  38.         amount = 0                                                  #counting variable
  39.         while current != None:                                      #While you haven't reached the end of the list, continue iterating the list
  40.             if current.getData() == item:
  41.                 amount = amount + 1
  42.                 current = current.getNext()                         #move to the next iteration
  43.             else:
  44.                 current = current.getNext()
  45.         return amount
  46.    
  47.     def count(self,item):                                           #check the amount of times the user input was found
  48.         if self.search(item) > 0:                                   #if the value was found at least once, print this
  49.             print (str(item) + " appearances : " + str(self.search(item)))
  50.             if self.search(item) > 1:                               #If the value was found more than once, it's a duplicate, so print this.
  51.                 print (str(item) + " contains at least one duplicate entry")
  52.         else:
  53.             print (str(item) + " was not found")                    #If the value was not found at all, let the user know.
  54.    
  55.  
  56. myList = orderedList()                                              #create an empty user list
  57. for num in inputList:                                               #iterate through our inputList and add each entry to the ordered list
  58.     myList.add(num)
  59.  
  60.  
  61. print(myList.count(item))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement