2017-07-29 77 views
0

這樣做的功課,不明白這將如何工作,所以與步驟的解釋將有很大幫助。 這個問題很難理解,所以一直無法理解和嘗試。 這裏是問題,分爲3個部分。 您被授予以下超類。不要修改這個。蟒蛇工作與類添加連接刪除項目

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 

編寫一個實現以下規範的類。不要重載Container的任何方法。

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 

•例如,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 

第二部分:

寫的方法如果b1和b2是袋子,那麼b1 + b2會給出代表兩個袋子結合的新袋子。

•例如,=袋()

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

第三部分:

編寫實現下面規格的類。不要重載Container的任何方法。

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

這聽起來更像是你的教授或助教的問題 - 你基本上只是拋棄了這裏設置的整個問題。如果你可以把它縮小到某個特定的範圍,並至少表現出解決這個問題的嘗試,那麼它可能就是主題。否則,這太寬泛了。 –

+0

你有沒有得到正確的第三部分? –

回答

1

如果你想寫一個子類,你需要做的第一件事就是理解你想要繼承的類。所以,你需要做的第一件事是瞭解Container做什麼。

它有兩種神奇的方法,__init____str__,以及一個普通的方法,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()電話。你能看到發生了什麼?當您插入4時,您會看到4:1。如果您第二次插入4,則看到4:2。換句話說,您看到的字符串表示形式爲:count

這是可行的,因爲Container的成員vals這是一本字典。字典中的每個項目都是的值,如同在字符串表示中一樣,值爲:count

您的第一項任務是編寫一個子類Bag,該子類完成Container所做的所有工作,但也有方法removecount

方法count只爲Container中的一個值產生相同的答案,即__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)) 

該位的另一半是寫remove這是insert相反。 insert做什麼?如果您要插入的值已經在vals中,它會增加計數,否則它會將計數設置爲1.因此,remove需要減少計數,並且如果它變爲零,則從字典中刪除該項。請注意,可以嘗試刪除不存在的值:在這種情況下忽略它。

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對齊。

既然您已經掌握了基本知識,那麼您的下一個任務就是編寫一個__add__方法,將兩個袋子放在一起。換句話說,給出Bag s ab,您需要組合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對齊。

您的問題的第三部分實際上是第一部分的重複。 remove方法是一樣的。 is_in方法與count相同,只是它返回TrueFalse而不是數字。

+0

在python 3中,你可以使用'__class __()'而不是'self .__ class __()' –

+0

你能給出is_in方法的確切代碼嗎? –

+0

甚至沒有準備嘗試,呃? 'return e in self.vals' – BoarGules