0

hey!!

this is my first time to post question in this board. i learn a lots from this place.

now , i really got a problem for this programming.

anybody can help me!!!! i done "Add" part but i don't konw right or wrong

and for "pass" i really need some help!! thank you !!!

#!/usr/bin/env python
"""
A simple program that plays Shannon's Game.
"""

class Frequency(object):
    """
    Stores a letter:frequency pair.

    >>> f = Frequency('c', 2)
    >>> f.letter
    'c'
    >>> f.frequency
    2
    >>> f
    {c: 2}
    """
    def __init__(self, letter, frequency):
        self.letter = letter
        self.frequency = frequency
        # The next Frequency object when stored as part of a linked list
        self.next = None

    def __repr__(self):
        return '{%s: %d}' % (self.letter, self.frequency)

class FrequencyList(object):
    """Stores a collection of Frequency objects as a linked list."""
    def __init__(self):
        self.head = None

    def add(self, letter, frequency):
        """
        Adds the given `letter`:`frequency` combination as a Frequency object
        to the end of the list. If the given `letter` is already in the list, 
        the given `frequency` is added to its frequency.

        >>> f = FrequencyList()
        >>> f.add('a', 3)
        >>> f
        [{a: 3}]
        >>> f.add('b', 2)
        >>> f
        [{a: 3}, {b: 2}]
        """
        letter = letter.lower()
        if self.head is None:
            self.head = Node(Frequency(letter, 1))
        else:
            current = self.head
            while current.next is not None and current.data.letter != letter:
                current = current.next
            if current.data.letter != letter:
                current.next = Node(Frequency(letter, 1))
            else:
                current.data.increment()
        

    def remove(self, letter):
        """
        Removes the Frequency object with the given `letter` from the list.
        Does nothing if `letter` is not in the list.

        >>> f = FrequencyList()
        >>> f.add('a', 3)
        >>> f.add('b', 2)
        >>> f.add('c', 3)
        >>> f
        [{a: 3}, {b: 2}, {c: 3}]
        >>> f.remove('b')
        >>> f
        [{a: 3}, {c: 3}]
        """
        pass

    def find(self, letter):
        """
        Returns the Frequency object for the given `letter` in the list, or
        None if the `letter` doesn't appear in the list.

        >>> f = FrequencyList()
        >>> f.add('a', 3)
        >>> f.find('a')
        {a: 3}
        >>> f.find('b')
        """
        pass

    def __contains__(self, item):
        return self.find(item) is not None
    
    def __repr__(self):
        item_strs = []
        current = self.head
        while current is not None:
            item_strs.append(repr(current))
            current = current.next
        return '[%s]' % ', '.join(item_strs)
3
Contributors
10
Replies
11
Views
5 Years
Discussion Span
Last Post by CoscNinja
0

No you haven't got it yet:

>>> f = FrequencyList()
>>> f
[]
>>> f.add('a',3)

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    f.add('a',3)
  File "K:/Tony/shannon_game.py", line 48, in add
    self.head = Node(Frequency(letter, 1))
NameError: global name 'Node' is not defined
>>>

I fixed it but it got lot of strange stuff like 'Node', 'increment' and 'data' to remove.

Edited by pyTony: n/a

0

chees!!! man, if possible can you show your work for me .

thank you for your help!!!

0

I gave you hint. Also wouldn't you think that parameter frequency should be used somewhere?

0
class FrequencyList(object):
    """Stores a collection of Frequency objects as a linked list."""
    def __init__(self):
        self.head = None

    def add(self, letter, frequency):
        """
        Adds the given `letter`:`frequency` combination as a Frequency object
        to the end of the list. If the given `letter` is already in the list, 
        the given `frequency` is added to its frequency.

        >>> f = FrequencyList()
        >>> f.add('a', 3)
        >>> f
        [{a: 3}]
        >>> f.add('b', 2)
        >>> f
        [{a: 3}, {b: 2}]
        """
        self.letter = letter
        self.frequency = frequency
        if self.head is None:
            self.head = Frequency(letter, frequency)
        else:
            current = self.head
            while current.next is not None and current.letter != letter:
                current = current.next
            if current.letter != letter:
                current.next = Frequency(letter, frequency)
            return

i fix it , cheers!!!man

but i still can't got 'remove' and 'find' part!!!

will you got some idea or help me !!!

again thank you for help

0

Right direction, but not yet fixed until end:

>>> f = FrequencyList()
>>> f.add('a',3)
>>> f
[{a: 3}]
>>> f.add('b',5)
>>> f
[{a: 3}, {b: 5}]
>>> f.add('b',2)
>>> f
[{a: 3}, {b: 5}]
>>>
0

I recommend that you add this doctest runner at the end of the module:

def _test():
    import doctest
    doctest.testmod()

if __name__ == "__main__":
    _test()

All cases have not tests though, like the incrementing of frequency case, you can copy run from my previous post to the docstring to add that.

def add(self, letter, frequency):
        """
        Adds the given `letter`:`frequency` combination as a Frequency object
        to the end of the list. If the given `letter` is already in the list, 
        the given `frequency` is added to its frequency.

        >>> f = FrequencyList()
        >>> f.add('a', 3)
        >>> f
        [{a: 3}]
        >>> f.add('b', 2)
        >>> f
        [{a: 3}, {b: 2}]
        >>> f.add('b',2)    
        >>> f
        [{a: 3}, {b: 4}]
        """

Here is successful run from command line verbosely:

Microsoft Windows XP [versio 5.1.2600]
(C) Copyright 1985 - 2001 Microsoft Corp.

ke 17.08.2011 13:33:49,85 K:\Tony
>PYTHON shannon_game.py -v
Trying:
    f = Frequency('c', 2)
Expecting nothing
ok
Trying:
    f.letter
Expecting:
    'c'
ok
Trying:
    f.frequency
Expecting:
    2
ok
Trying:
    f
Expecting:
    {c: 2}
ok
Trying:
    f = FrequencyList()
Expecting nothing
ok
Trying:
    f.add('a', 3)
Expecting nothing
ok
Trying:
    f
Expecting:
    [{a: 3}]
ok
Trying:
    f.add('b', 2)
Expecting nothing
ok
Trying:
    f
Expecting:
    [{a: 3}, {b: 2}]
ok
Trying:
    f.add('b',2)
Expecting nothing
ok
Trying:
    f
Expecting:
    [{a: 3}, {b: 4}]
ok
Trying:
    f = FrequencyList()
Expecting nothing
ok
Trying:
    f.add('a', 3)
Expecting nothing
ok
Trying:
    f.find('a')
Expecting:
    {a: 3}
ok
Trying:
    f.find('b')
Expecting nothing
ok
Trying:
    f = FrequencyList()
Expecting nothing
ok
Trying:
    f.add('a', 3)
Expecting nothing
ok
Trying:
    f.add('b', 2)
Expecting nothing
ok
Trying:
    f.add('c', 3)
Expecting nothing
ok
Trying:
    f
Expecting:
    [{a: 3}, {b: 2}, {c: 3}]
ok
Trying:
    f.remove('b')
Expecting nothing
ok
Trying:
    f
Expecting:
    [{a: 3}, {c: 3}]
ok
8 items had no tests:
    __main__
    __main__.Frequency.__init__
    __main__.Frequency.__repr__
    __main__.FrequencyList
    __main__.FrequencyList.__contains__
    __main__.FrequencyList.__init__
    __main__.FrequencyList.__repr__
    __main__._test
4 items passed all tests:
   4 tests in __main__.Frequency
   7 tests in __main__.FrequencyList.add
   4 tests in __main__.FrequencyList.find
   7 tests in __main__.FrequencyList.remove
22 tests in 12 items.
22 passed and 0 failed.
Test passed.

Edited by pyTony: n/a

0

well, i think i still need some help for this one

class Frequency(object):
    """
    Stores a letter:frequency pair.

    >>> f = Frequency('c', 2)
    >>> f.letter
    'c'
    >>> f.frequency
    2
    >>> f
    {c: 2}
    """
    def __init__(self, letter, frequency):
        self.letter = letter
        self.frequency = frequency
        # The next Frequency object when stored as part of a linked list
        self.next = None
        
    def getletter(self):
        return self.letter
   
    def getfrequency(self):
        return self.frequency
   
    def getNext(self):
        return self.next
       
    def setletter(self,newletter):
        self.letter = newletter
       
    def setNext(self,newnext):
        self.next = newnext
       
    def increment(self):
        self.frequency +=1

    def __repr__(self):
        return '{%s: %d}' % (self.letter, self.frequency)

i add some founcation in this first part and then i add some in the 'ADD' part

def add(self, letter, frequency):
        """
        Adds the given `letter`:`frequency` combination as a Frequency object
        to the end of the list. If the given `letter` is already in the list, 
        the given `frequency` is added to its frequency.

        >>> f = FrequencyList()
        >>> f.add('a', 3)
        >>> f
        [{a: 3}]
        >>> f.add('b', 2)
        >>> f
        [{a: 3}, {b: 2}]
        """
        current = self.head
        Found = False
        while current !=None:
            if current.getletter()==letter:
                current.frequency=current.frequency+frequency
                return
            else:
                current=current.getNext()
                
        temp=Frequency(letter,frequency)
        temp.setNext(self.head)
        self.head=temp
        pass

it works, but print out result is oppsite!!

like ,if i add "f.add('a', 3)","f.add('b', 3)" the result should be like [{a: 3}, {b: 3}]

but in my result is [{b: 2}, {a: 3}]

while can some one give some idea!!!

0

while good news is i can use this one to implememt the'REMOVE'part and'FIND' part, bad news is its still oppsite!!!

0

Haha, isn't this for the Cosc122 assignment?
Because we're not meant to discuss the assignment at code level, only at a higher level. If you have issues with your code you should discuss it with your tutor.
But thanks for the free tips :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.