0

私の仕事は、リンクされたリストから最初に発生した値を削除することです。値はユーザーが入力します。次に、項目が削除されたかどうか (True) を示すブール値を返すか、値がリンクされたリストにない場合は False を返す必要もあります。これは私がこれまでに持っているものです。

def remove(lst, value):
    lst.head = removeRec(lst.head, value)

def removeRec(node, value):
    if isinstance(node, EmptyNode):
        print("Cannot remove value from an empty list")
    elif node.data == value:
        return node.next
    elif:
        node.next = removeRec(node.next, value)
        return node
    else:
        return False

これの問題は、アイテムが削除された場合に True を返さないことです。どうすればそれを実装できますか?また、同様の機能を繰り返し実装するにはどうすればよいですか。みんなありがとう。

4

4 に答える 4

2

要求されたことを達成できなかったことを報告する "Pythonic" の方法は、例外を発生させることです。これにより、コードがかなり単純になります。

def removeRec(node, value):
    if isinstance(node, EmptyNode):
        raise ValueError("Cannot remove value from an empty list")
    elif node.data == value:
        return node.next
    else:
        node.next = removeRec(node.next, value)
        return node

これは既存のremove関数で機能し、呼び出し元のコードで例外を処理するのは呼び出し元に任せます。removeただし、関数が成功または失敗を示すブール値を返すようにしたい場合は、そこで例外をキャッチできます。

def remove(lst, value):
    try:
        lst.head = removeRec(lst.head, value)
        return True # only reached if no exception was raised by the recursion
    except ValueError:
        return False

なんらかの理由 (まだ教えられていないクラスにいるなど) で例外の使用に反対している場合は、再帰呼び出しからの戻り値で削除の失敗をエンコードできますが、その場合はリストの損傷を避けるために、追加のチェックを行うには:

def removeRec(node, value):
    if isinstance(node, EmptyNode):
        print("Cannot remove value from an empty list")
        return None    # your code did this implicitly, I'm being explicit
    elif node.data == value:
        return node.next
    else:
        rec_result = removeRec(node.next, value)
        if rec_result is None:
            return rec_result
        else:
            node.next = rec_result
            return node

次に、非再帰関数の再帰ケースから同じチェックを行い、結果の値をブール値に変換できます。

def remove(lst, value):
    rec_result = removeRec(lst.head, value)
    if rec_result is None:
        return False
    else:
        lst.head = rec_result
        return True
于 2013-11-09T22:06:40.227 に答える
1

基本ケースから始める必要があります

  1. 値はリストの先頭にあります
  2. 値はリストの最後にあります
  3. 値はリストの中央にあります
  4. 値がリストにありません

ほとんどのリストの性質上、最後の値をリストの途中にあるかのように扱うことができます

本当に私たちは持っています

  1. 値はリストの先頭にあります
  2. 値がリストにありません
  3. 他のすべての場合

基本ケースとして、基本ケースを理解したので、関数を記述できます

def removeRec(node,value):
   if node.value == value: #only time this will happen is if the node is at the head
      node.value = node.next.value
      node.next = node.next.next
      #you cannot simply say node= node.next since then it will not propagate to the actual list
      return True # found and removed value
   if node.next == None: #case where value is not in the list
      return False #unable to remove
   if node.next.value == value:
       node.next = node.next.next 
       return True #successfully removed
   return removeRec(node.next,value)#recursively continue searching

ノードは変更可能なオブジェクトであるため、リストは更新されるだけです...新しいリストは返されません

于 2013-11-09T21:58:31.273 に答える
0

私はそれを次のようにします:

def remove(lst, value):
    remove.ret = False

    def removeRec(node, value):
        if isinstance(node, EmptyNode):
            print("Cannot remove value from an empty list")
        elif node.data == value:
            remove.ret = True
            return node.next
        else:
            node.next = removeRec(node.next, value)
            return node

    lst.head = removeRec(lst.head, value)
    return remove.ret
于 2013-11-09T22:12:06.367 に答える