2012-11-29 3 views
1

내 Road 개체 목록에서 항목을 삭제하지 않는 이유는 무엇입니까?내 도로 목록에서 도로를 삭제할 수없는 이유는 무엇입니까?

도로 객체 (자기, city1, 시티 2, 길이) 가지고 가고,시 (자기, 이름, 인구) 걸릴 객체 관련 정보;

나는 이러한 개체를 _cities 및 _roads 목록에 저장하여 수정할 수 있습니다.

이 정의는 도시에 연결된 모든 도로를 삭제 한 다음 도시를 삭제하기로되어 있습니다.

그러나 내 코드는 내 도로를 삭제하고 싶지 않으며 오류가 발생하지 않으므로 내 논리에 결함이 있어야합니다.

도와 주시겠습니까?

class Network: 

    def __init__(self): 

     self._cities = [] # list of City objects in this network 
     self._roads = [] # list of Road objects in this network 

    def hasCity(self, name): 

     for x in self._cities: 
      if x.name == name: 
       return True 
     return False 

    def hasRoad(self, road): 

     for x in self._roads: 
      if x.city1 == road[0] and x.city2 == road[1]: 
       return True 
      elif x.city1 == road[1] and x.city2 == road[0]: 
       return True 
      else: 
       return False 

    def addCity(self, name, pop): 
     if self.hasCity(name) == True: 
      return False 
     else: 
      self._cities.append(City(name, pop)) 
      return True 

    def addRoad(self, road, length): 

     if self.hasRoad(road) == True: 
      return False 
     else: 
      self._roads.append(Road(road[0], road[1], length)) 
      return True 

    def delRoad(self, road): 
     if self.hasRoad(road) == False: 
      return False 
     else: 
      for x in self._roads: 
       if x.city1 == road[0] and x.city2 == road[1]: 
        self._roads.remove(x) 
        return True 
       elif x.city1 == road[1] and x.city2 == road[0]: 
        self._roads.remove(x) 
        return True 
       else: 
        return False 


    def delCity(self, city): 

     if self.hasCity(city) == False: 
      return False 
     else: 
      for x in self._cities: 
       if x.name == city: 
        for j in self._roads: 
         if j.city1 == x.name: 
          self.delRoad((j.city1, j.city2)) 
          self.delRoad((j.city2, j.city1)) 
         elif j.city2 == x.name: 
          self.delRoad((j.city1, j.city2)) 
          self.delRoad((j.city2, j.city1)) 
        self._cities.remove(x) 
        return True 
+1

전체 수업을들을 수 있습니까? –

+0

"무향 그래프의 가장자리를 지우는 법"이라는 말을 다시 할 수 있습니다. – asheeshr

+0

깊게 중첩 된'if' 블록은 중복 된 것으로 보입니다. 그들은 j.city1 == x.name 또는 j.city2 == x.name :'절에 함께있을 수 있습니다. –

답변

2

아마 당신이 반복하는 목록의 요소를 삭제할 수 있습니다. 이것은 일반적으로 나쁜 습관입니다.

+1

일반적으로 나쁜 습관이지만 그 길은 삭제됩니다. 도시 제거가 시도되기 전에 도로가 삭제됩니다. –

+0

Vkontori, 나는 그것을 점검했다. 그리고 나는 @Joshua D. Boyd가 옳다고 생각한다 - 나는 당신이하지 않는 한 내가 이것을했을 것 같은 곳을 보지 못한다 (당신이하지 않으면?!) –

+0

당신의 버그는 hasRoad에있다. 첫 번째 것이 일치하지 않으면 false를 반환합니다. – vkontori

0

delCity가 향상 될 수 있지만 정상적으로 작동하는 것 같습니다. 나는 당신이 더 많은 코드를 게시 할 필요가 있다고 생각한다. delCity를 테스트하기 위해 샘플 코드를 작성했습니다. 나는 지금 여기

class Test(object): 
    def __init__(self, cities, roads): 
     self._cities = cities 
     self._roads = roads 

    def hasCity(self, city_name): 
     for c in self._cities: 
      if c.name == city_name: 
       return True 
     return False 

    def delRoad(self, pair): 
     for x in self._roads: 
      if x.city1 == pair[0] and x.city2 == pair[1]: 
       self._roads.remove(x) 

    def delCity(self, city): 
     if self.hasCity(city) == False: #checks to see if city isn't in list 
      return False 
     else: 
      for x in self._cities: 
       if x.name == city: 
        for j in self._roads: 
         if j.city1 == x.name: 
          self.delRoad((j.city1, j.city2)) ##delRoad takes a tuple 
          self.delRoad((j.city2, j.city1)) 
         elif j.city2 == x.name: 
          self.delRoad((j.city1, j.city2)) 
          self.delRoad((j.city2, j.city1)) 
        self._cities.remove(x) 
        return True 

그리고 다음은

class City(object): 
    def __init__(self, name, population): 
     self.name = name 
     self.population = population 

    def __repr__(self): 
     return "City(%r, %r)" % (self.name, self.population) 

도로 클래스입니다 : 여기

class Road(object): 
    def __init__ (self, city1, city2, length): 
     self.city1 = city1 
     self.city2 = city2 
     self.length = length 

    def __repr__(self): 
     return "Road(%r, %r, %r)" % (self.city1, self.city2, self.length) 

내가로 당신에게 delCity 방법을 붙여 테스트 클래스입니다 여기에 도시 클래스입니다 코드를 테스트하십시오 :

>>> t = Test([City('x', 1), City('y', 1), City('z', 1)], 
      [Road('x', 'y', 1), Road('y', 'z', 1)]) 
>>> t.delCity('x') 

>>> print t._cities 
[City('y', 1), City('z', 1)] 
>>> print t._roads 
[Road('y', 'z', 1)] 

보시다시피 그 도시로 간 단일 도로와 도시가 삭제되었습니다.

+0

흠, 계속 디버깅을하겠습니다. 그걸 확인해 줘서 고마워.내가 머리를 벽에 다시 부딪 힐 때 다시 게시 할 것입니다. –

관련 문제