2017-07-29 1 views
0

宿題のためにこれをやっていますが、これがどのように機能するのか理解していないので、手順を説明すると多くの助けになります。 質問は分かりにくいので、理解して試してみることはできませんでした。 ここに3つの部分に分かれた質問があります。 次のスーパークラスが指定されています。これを変更しないでください。Pythonはクラスを使って作業をしています。

class Container(object): 
    """ Holds hashable objects. Objects may occur 0 or more times """ 
    def __init__(self): 
     """ Creates a new container with no objects in it. I.e., any object 
      occurs 0 times in self. """ 
     self.vals = {} 
    def insert(self, e): 
     """ assumes e is hashable 
      Increases the number times e occurs in self by 1. """ 
     try: 
      self.vals[e] += 1 
     except: 
      self.vals[e] = 1 
    def __str__(self): 
     s = "" 
     for i in sorted(self.vals.keys()): 
      if self.vals[i] != 0: 
       s += str(i)+":"+str(self.vals[i])+"\n" 
     return s 

以下の仕様を実装するクラスを作成します。コンテナのメソッドをオーバーライドしないでください。第二の部分、例えば、D1 =バッグ()

d1.insert(4) 
d1.insert(4) 
print(d1) 
d1.remove(2) 
print(d1) 
prints 4:2 
4:2 

•例えば、D1 =バッグ()

d1.insert(4) 
d1.insert(4) 
d1.insert(4) 
print(d1.count(2)) 
print(d1.count(4)) 
prints 0 
3 

class Bag(Container): 
    def remove(self, e): 
     """ assumes e is hashable 
      If e occurs one or more times in self, reduces the number of 
      times it occurs in self by 1. Otherwise does nothing. """ 
     # write code here 
def count(self, e): 
    """ assumes e is hashable 
     Returns the number of times e occurs in self. """ 
    # write code here 

•:

における方法を書きますb1とb2がバッグであれば、b1 + b2は2つのバッグの結合を表す新しいバッグを与える。例えば

•、=バッグ()

a.insert(4) 
a.insert(3) 
b = Bag() 
b.insert(4) 
print(a+b) 
prints 3:1 
4:2 

第三部分:

以下の仕様を実装するクラスを記述します。コンテナのメソッドをオーバーライドしないでください。

例えば
class ASet(Container): 
    def remove(self, e): 
     """assumes e is hashable 
      removes e from self""" 
     # write code here 
def is_in(self, e): 
    """assumes e is hashable 
     returns True if e has been inserted in self and 
     not subsequently removed, and False otherwise.""" 
    # write code here 

•、D1 = ASET()

d1.insert(4) 
d1.insert(4) 

d1.remove(2) 
print(d1) 

d1.remove(4) 
print(d1) 
prints 4:2 # from d1.remove(2) print 

    # (empty) from d1.remove(4) print 
• For example, d1 = ASet() 
d1.insert(4) 
print(d1.is_in(4)) 
d1.insert(5) 
print(d1.is_in(5)) 
d1.remove(5) 
print(d1.is_in(5)) 
prints True 
True 
False 

ありがとうございました。

+0

インデントを修正します。 – Rahul

+0

これはあなたの教授やTAの質問によく似ています。基本的にはここで設定した問題全体をダンプするだけです。特定のものに絞って、これを解決しようとする試みを少なくとも示すことができれば、それはトピックになるかもしれません。それ以外の場合は、これは広すぎます。 –

+0

3番目の部分は正しいですか? –

答えて

1

サブクラスを作成する場合は、まずサブクラス化するクラスを理解する必要があります。だから、まずあなたがする必要があるのは、Containerのことを理解することです。

それは2つの魔方法、__init____str__と1つの通常の方法、insertを持っています。これを行うことにより、insert最初の探検:

d1 = Container() 
d1.insert(4) 
print(d1) 
d1.insert(2) 
print(d1) 
d1.insert(4) 
print(d1) 

あなたはこの出力を得る:

4:1 

2:1 
4:1 

2:1 
4:2 

応答の3セット、各print()呼び出しから1があります。あなたは何が起こっているのを見ることができますか? 4を挿入すると、4:1が表示されます。 4を2回挿入すると、4:2と表示されます。つまり、表示されている文字列表現は:カウントです。

これは、Containerのメンバーがvalsという辞書を持っているためです。辞書の各項目は、文字列表現と同様に:カウントです。

あなたの最初の仕事は、Containerのすべてを行うサブクラスBagを書くことですが、方法はremovecountです。

countメソッドは、Containerの1つの値に対してすべての値に対して同じ回答を生成するだけです。__str__辞書から対応する値を選択し、出現回数を返します。その場合は、返信0を返すことを忘れないでください。

class Bag(Container): 
    def count(self, e): 
     return self.vals.get(e,0) 

これが機能することを確認します

d1 = Bag() 
d1.insert(4) 
d1.insert(4) 
print(d1.count(4)) 

このビットの他の半分はinsertの反対であるremoveを書くことです。 insertは何をしていますか?挿入しようとしている値が既にvalsにある場合は、カウントが増加します。それ以外の場合は、カウントが1に設定されます。したがって、removeはカウントを減らす必要があり、0になると辞書から項目を削除します。その場合は値を削除しても問題ありませんが、その場合は無視してください。

def remove(self, e): 
     if e not in self.vals: 
      return 
     self.vals[e] -= 1 
     if self.vals[e] < 1: 
      del(self.vals[e]) 

このコードを追加するときは注意してください。インデントはcountと一致する必要があります。

これで基本的なことができました。次の作業は、2つのバッグを一緒に追加する__add__メソッドを作成することです。つまり、Bagsabを指定すると、a.valsb.valsを組み合わせる必要があります。 aのコピーから開始し、内容をbに追加します。あなたはすでにメソッドを追加しています:メソッドinsertを使用します。

def __add__(self, other): 
     result = self.__class__() 
     result.vals.update(self.vals) 
     for value,count in other.vals.items(): 
      for _ in range(count): 
       result.insert(value) 
     return result 

このコードを追加するときは注意してください。インデントはcountと一致する必要があります。

質問の第3の部分は、実際に最初の部分の繰り返しです。 removeの方法は同じです。 is_inメソッドはcountと同じですが、数値の代わりにTrueまたはFalseを返します。

+0

Python 3では、 'self .__ class __()'の代わりに '__class __()'を使うことができます –

+0

is_inメソッドの正確なコードを与えることができますか? –

+0

試しても準備ができていませんか? 'return e in self.vals' – BoarGules