Question

Task 2: SecretWord class:

Download and save a copy of LinkedLists.py (found at the bottom of this assignment page on eClass). In this file, you have been given the complete code for a LinkedList class. Familiarize yourself with this class, noticing that it uses the Node class from Task 1 and is almost identical to the SLinkedList class given in the lectures. However, it also has a complete insert(pos, item) method (which should be similar to the insert method you implemented in Lab 7) and a getHead() method (which is just a getter for the self.__head attribute). In this same file, you should also find the skeleton code for a SecretWord class. An instance of the SecretWord class represents the randomly chosen word that is being guessed in the Guess the Word game. The SecretWord class contains the private attribute self.__wordLinkedList, which is an instance of the LinkedList class (described above). It also has a private attribute self.__numVisible, which is the number of letters correctly guessed in the word. For example, if the secret word is "YELLOW" and only the letter "L" has been correctly guessed, then self.__numVisible would be 2.

Complete the SecretWord class according to the ADT described below:

 setWord(word) – adds the uppercase letters in the input word to self.__wordLinkedList, so that each node contains one uppercase letter, and the order of the nodes matches the order of the letters in the word. i.e. the head node of self.__wordLinkedList should contain the first letter in the word. Nothing is returned.

 isSolved() – returns True if all of the letters in the secret word have been guessed by the player. Otherwise, returns False.

 update(guess) – updates the isVisible attribute of any node(s) containing data that matches the input guess, so that the letter will be displayed the next time the word is printed to the screen. Returns True if at least one node was updated, False otherwise.

 printProgress() – prints the current status of the secret word. For example, if the secret word is "YELLOW" and only the letter "O" has been correctly guessed, then this method prints "_ _ _ _ O _ ".

 __str__() – creates and returns a string to represent the SecretWord object. This method has been completed for you. You may create additional methods to help you complete the methods above, and add private attributes if needed. The additional methods can be called within the SecretWord class, but NOT outside of it. Only the given methods form the public interface, exactly as presented above. Test your SecretWord class thoroughly before moving on, and be sure to leave those tests under if __name__=='__main__': for the markers to see.

class Node: def __init__(self, initData, initNext, initVisible = False): Constructs a new node and initializes it to contain  from Node import Node class Linkedlist: def __init__(self): self.__head = None self.__size = 0 def isEmpty(self): return self  def index(self, item): current = self.__head found = False index = 0 while current != None and not found: if current.getData(

def append(self, item): temp = Node(item, None) if self.__head == None: self.__head = temp else: current = self.__head while  self.__size self.__size + 1 def pop(self): current = self.__head previous = None while current.getNext() != None: previous =  class SecretWord: def __init__(self): self.__wordLinkedList = LinkedList self.__numVisible = 0 def setWord(self, word): # TO

0 0
Add a comment Improve this question Transcribed image text
Answer #1
 class SecretWord: def __init__(self): self.__wordLinkedList = LinkedList() self.__numVisible = 0 def setWord(self, word): letters = list(word.upper()) for letter in letters: if(self.__wordLinkedList.isEmpty()): self.__wordLinkedList.add(letter) else: self.__wordLinkedList.append(letter) def isSolved(self): current = self.__wordLinkedList.getHead() solved = True while current != None and solved: if(not current.getVisible()): solved = False else: current = current.getNext() return solved def update(self, guess): current = self.__wordLinkedList.getHead() updated = False while current != None: if(current.getData()==guess.upper()): current.setVisible() updated = True return updated def printProgress(self): current = self.__wordLinkedList.getHead() status = '' while current != None: if(current.getVisible()): status += str(current.getData()) else: status += '_' print(status)

Note: Implementation details of "Node" class is not given hence the following two implementations of required methods are assumed for "isVisible" attribute of "Node" class. The Variable "isVisible" is assumed to have binary value only.

1. Node().getVisible() : It return the current value of "isVisible" attribute of instance Node().

2. Node().setVisible() : It sets "isVisible" attribute of instance Node() to True value.

Add a comment
Know the answer?
Add Answer to:
Task 2: SecretWord class: Download and save a copy of LinkedLists.py (found at the bottom of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PYTHON -------------------------------------------------------- class LinkedList:    def __init__(self):        self.__head = None        self.__tail = 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...

  • PYTHON. Continues off another code. I don't understand this. Someone please help! Comment the lines please...

    PYTHON. Continues off another code. I don't understand this. Someone please help! Comment the lines please so I can understand LinkedList ADT: class myLinkedList:     def __init__(self):         self.__head = None         self.__tail = None         self.__size = 0     def insert(self, i, data):         if self.isEmpty():             self.__head = listNode(data)             self.__tail = self.__head         elif i <= 0:             self.__head = listNode(data, self.__head)         elif i >= self.__size:             self.__tail.setNext(listNode(data))             self.__tail = self.__tail.getNext()         else:             current = self.__getIthNode(i - 1)             current.setNext(listNode(data,...

  • PYTHON. Continues off another code(other code is below). I don't understand this. Someone please help! Comment...

    PYTHON. Continues off another code(other code is below). I don't understand this. Someone please help! Comment the lines please so I can understand. There are short and med files lengths for each the list of names/ids and then search id file. These are the input files: https://codeshare.io/aVQd46 https://codeshare.io/5M3XnR https://codeshare.io/2W684E https://codeshare.io/5RJwZ4 LinkedList ADT to store student records(code is below). Using LinkedList ADT instead of the Python List. You will need to use the Student ADT(code is below) Imports the Student class...

  • Q1. Write a program to simulate a grocery waiting queue. Your program should ask the user...

    Q1. Write a program to simulate a grocery waiting queue. Your program should ask the user if they want to add a customer to the queue, serve the next customer in the queue, or exit. When a customer is served or added to the queue, the program should print out the name of that customer and the remaining customers in the queue. The store has two queues: one is for normal customers, another is for VIP customers. Normal customers can...

  • I need help with todo line please public class LinkedList { private Node head; public LinkedList()...

    I need help with todo line please public class LinkedList { private Node head; public LinkedList() { head = null; } public boolean isEmpty() { return head == null; } public int size() { int count = 0; Node current = head; while (current != null) { count++; current = current.getNext(); } return count; } public void add(int data) { Node newNode = new Node(data); newNode.setNext(head); head = newNode; } public void append(int data) { Node newNode = new Node(data);...

  • Python 3: Write a LinkedList method named contains, that takes a value as a parameter and...

    Python 3: Write a LinkedList method named contains, that takes a value as a parameter and returns True if that value is in the linked list, but returns False otherwise. class Node: """ Represents a node in a linked list """ def __init__(self, data): self.data = data self.next = None class LinkedList: """ A linked list implementation of the List ADT """ def __init__(self): self.head = None def add(self, val): """ Adds a node containing val to the linked list...

  • Python 3: Python 3: Write a LinkedList class that has recursive implementations of the add, display,...

    Python 3: Python 3: Write a LinkedList class that has recursive implementations of the add, display, remove methods. You may use default arguments and/or helper functions. class Node: """ Represents a node in a linked list """ def __init__(self, data): self.data = data self.next = None class LinkedList: """ A linked list implementation of the List ADT """ def __init__(self): self.head = None def add(self, val): """ Adds a node containing val to the linked list """ if self.head is...

  • Finish each function python 3 LList.py class node(object): """ A version of the Node class with...

    Finish each function python 3 LList.py class node(object): """ A version of the Node class with public attributes. This makes the use of node objects a bit more convenient for implementing LList class.    Since there are no setters and getters, we use the attributes directly.    This is safe because the node class is defined in this module. No one else will use this version of the class. ''' def __init__(self, data, next=None): """ Create a new node for...

  • In PYTHON 3- Implement a subclass (described below) of "Word Guess", a variant of the game...

    In PYTHON 3- Implement a subclass (described below) of "Word Guess", a variant of the game Hangman. In this game, a word is first randomly chosen. Initially, the letters in the word are displayed represented by "_”.   For example, if the random word is "yellow”, the game initially displays "_ _ _ _ _ _”. Then, each turn, the player guesses a single letter that has yet to be guessed. If the letter is in the secret word, then the...

  • When compiling the LinkedList and Iterator class, the following error is being produced: Note: LinkedList.java uses...

    When compiling the LinkedList and Iterator class, the following error is being produced: Note: LinkedList.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. Any suggestions? public class LinkedList<T> {    Node<T> itsFirstNode;    Node<T> itsLastNode;    private int size;    public LinkedList() {        itsFirstNode = null;        itsLastNode = null;          size = 0;    }    public Iterator<T> getIterator() {        return new Iterator(this);    }    // THIS WILL NEED...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT