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)