2013-10-11 1 views
-1

countInt 함수에 문제가 있습니다. 사실 그것은 countINT라는 레이블이 붙어 있고 '-'을 인수로 넣는 것 외에도. 연결된 목록 작성을 테스트 한 결과 제대로 작동하는 것 같습니다. 그래서 나는 안전하게 문제를 해결할 수 있다고 생각합니다. 그러나 NoneType 객체에는 속성 값 오류가 없으므로 어디서 잘못되었는지 확신 할 수 없습니다. 누군가 나를위한 또 다른 눈 집합이 될 수 있었고 오류를 찾아 내고 그것을 바로 잡도록 안내 할 수 있었습니까? 감사합니다.연결된 목록의 값 개수를 계산하십시오.

출력 :

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "linked_list.py", line 63, in <module> 
    print countInt(r,1) 
    File "linked_list.py", line 25, in countInt 
    countInt(head.next,n) 
    File "linked_list.py", line 25, in countInt 
    countInt(head.next,n) 
    File "linked_list.py", line 25, in countInt 
    countInt(head.next,n) 
    File "linked_list.py", line 25, in countInt 
    countInt(head.next,n) 
    File "linked_list.py", line 25, in countInt 
    countInt(head.next,n) 
    File "linked_list.py", line 25, in countInt 
    countInt(head.next,n) 
    File "linked_list.py", line 25, in countInt 
    countInt(head.next,n) 
    File "linked_list.py", line 25, in countInt 
    countInt(head.next,n) 
    File "linked_list.py", line 25, in countInt 
    countInt(head.next,n) 
    File "linked_list.py", line 25, in countInt 
    countInt(head.next,n) 
    File "linked_list.py", line 25, in countInt 
    countInt(head.next,n) 
    File "linked_list.py", line 25, in countInt 
    countInt(head.next,n) 
    File "linked_list.py", line 23, in countInt 
    if head.next.value == n: 
AttributeError: 'NoneType' object has no attribute 'value' 

예상 출력 :

2 
2 

내 코드 :

class Node: 
    def __init__(self,value): 
     self.next = None 
     self.value = value 

def createLinkedList(root, node): 
    if root.next is None: 
     root.next = node 
    else: 
     createLinkedList(root.next, node) 

def countInt(head, n, count= 0):  #create a dictionary with keys as the values of the linked list and count up if the value occurs again 
    count = 0 
    if head.value is None: 
     return None 
    else: 
     if head.next.value == n: 
      count += 1 
     countInt(head.next, n, count) 
     return count 


# root 
r = Node(1) 

# nodes 
a = Node(4) 
b = Node(1) 
c = Node(5) 
d = Node('-') 
e = Node(4) 
f = Node(1) 
g = Node(2) 
h = Node('-') 
i = Node(8) 
j = Node(9) 
k = Node(8) 
l = Node(3) 

createLinkedList(r,a) 
createLinkedList(r,b) 
createLinkedList(r,c) 
createLinkedList(r,d) 
createLinkedList(r,e) 
createLinkedList(r,f) 
createLinkedList(r,g) 
createLinkedList(r,h) 
createLinkedList(r,i) 
createLinkedList(r,j) 
createLinkedList(r,k) 
createLinkedList(r,l) 


print countInt(r,1) 
print countInt(r,'-') 

답변

2

변경 라인 :

if head.value is None: 
,

if head.next is None: 

에 라인의 목적은있는 다음 노드가 None 될 것입니다 포인트 목록이 중지해야 할 때 알아야하는 것입니다. 노드에 저장된 값은 그 노드와 아무 관계가 없습니다. 마지막 노드에는 여전히 값이 있습니다 (반대로 값의 None을 목록에 먼저 저장하려고 할 수 있습니다).


별도의 문제로, 함수는 항상 실제로 변수 count에 아무것도하지 않는 0 선 countInt(head.next, n, count)를 반환합니다 파이썬은 값의 int를 전달합니다, 그래서 당신은 전달 된 변수가 증가되지 않습니다. 그리고 일단 문제를 해결해도 항상 head.next을 확인한다는 사실은 목록의 첫 번째 요소를 건너 뛰는 것을 의미합니다. 이것은 실제로 당신이 (재귀를 구현하는 어색한 방법이다) 목록을 아래로 count 전달을 제거 할 수 있습니다

def countInt(head, n): 
    count = 0 
    if head.value == n: 
     count = 1 
    if head.next is None: 
     return count 
    return count + countInt(head.next, n) 

: 대신 귀하의 기능을 설정해야합니다.

+0

감사합니다. 내 코드에 결함이 있음을 알 수 있습니다. – Liondancer

+0

그러나 값으로 n 값을 가진 노드를 하나만 입력하면 어떻게됩니까? – Liondancer

+1

@Liondancer : 좋은 지적 : 제안 된 코드의 순서가 잘못되어이 경우 작동하지 않습니다. 결정된. –

관련 문제