コード例 #1
0
def replace_last(node_chain, target_val, replacement_val):
    """
    Purpose:
        Replaces the last occurrence of target data value with the new_value. The chain should at most have 1 data value changed.
    Pre-conditions:
        :param node_chain: a node chain, possibly None
        :param target_val: the target data value we are searching to replace the last instance of
        :param replacement_val: the data value to replace the target_val that we found
    Post-conditions:
        The node-chain is changed, by replacing the last occurrence of target_val. If target_val is not present, then the node_chain returns unaltered.
    Return:
        :return: The altered node chain where any data occurrences of target_val has been replaced with replacement_val.
    """
    if node_chain == None:
        return None
    anode = node_chain  #step through all nodes but last
    target_node = None
    while node.get_next(
            anode
    ) != None:  #referece the node with matching value while stepping through all nodes
        if node.get_data(anode) == target_val:
            target_node = anode
        anode = node.get_next(anode)
    if node.get_data(anode) == target_val:  #check last node
        target_node = anode

    if target_node == None:  #if no match, return list
        return node_chain
    else:  #if found, replace and return
        node.set_data(target_node,
                      replacement_val)  #if found, replace and return
        return node_chain
コード例 #2
0
ファイル: LList.py プロジェクト: knownastron/cmpt145
def set_data_at_index(alist, idx, val):
    """
    Purpose
        Store val into alist at the index idx
    Preconditions:
        :param alist: a list created by create()
        :param val:   a value of any kind
        :param idx:   a non-negative integer
    Post-conditions:
        The value stored at index idx changes to val
    Return:
        :return True if the index was valid, False otherwise
    """

    cur_node = alist['head']
    cur_idx = 0

    #check if target index is within scope of alist
    if alist['size'] - idx < 1:
        return False
    else:
        while cur_node is not None:
            if cur_idx == idx:
                node.set_data(cur_node, val)
                return True
            else:
                cur_idx += 1
                cur_node = node.get_next(cur_node)
コード例 #3
0
ファイル: a5q2.py プロジェクト: knownastron/cmpt145
def replace_last(node_chain, target_val, replacement_val):
    """
    Purpose:
        Replaces the last occurrence of target data value with the new_value. The chain should at most have 1 data value changed.
    Pre-conditions:
        :param node_chain: a node chain, possibly None
        :param target_val: the target data value we are searching to replace the last instance of
        :param replacement_val: the data value to replace the target_val that we found
    Post-conditions:
        The node-chain is changed, by replacing the last occurrence of target_val. If target_val is not present, then the node_chain returns unaltered.
    Return:
        :return: The altered node chain where any data occurrences of target_val has been replaced with replacement_val.
    """

    cur_node = node_chain
    target_node = None

    #increment through the node chain to find the last example that matches target_val
    while cur_node != None:
        cur_value = node.get_data(cur_node)
        if cur_value == target_val:
            target_node = cur_node
        cur_node = node.get_next(cur_node)

    #if there is a target node, change its data value
    if target_node != None:
        node.set_data(target_node, replacement_val)

    return node_chain
コード例 #4
0
ファイル: a5q2.py プロジェクト: knownastron/cmpt145
def replace(node_chain, target, value):
    """
    Purpose:
        Replace every occurrence of data target in node_chain with data value
        The chain's data may change, but the node structure will not.
    Pre-Conditions:
        :param node_chain: a node-chain, possibly empty
        :param target: a data value
        :param value: a data value
    Post-conditions:
        The node-chain is changed, by replacing target with value everywhere.
    Return:
        :return: None
    """
    walker = node_chain
    while walker is not None:
        if node.get_data(walker) == target:
            node.set_data(walker, value)
        walker = node.get_next(walker)
コード例 #5
0
def replace(node_chain, target, replacement):
    """
    purpose: replace data in node chain if data matches target
    Preconditions:
    node_chain: a node chain
    post-conditions: data in node chain possibly altered
    return: reference to the node_chain
    """
    if node_chain == None:
        return None
    if node.get_next(node_chain) == None:  #base case
        if node.get_data(node_chain) == target:
            node.set_data(node_chain, replacement)
        return node_chain
    if node.get_data(node_chain) == target:
        node.set_data(node_chain, replacement)
        replace(node.get_next(node_chain), target, replacement)
    else:
        replace(node.get_next(node_chain), target, replacement)
    return node_chain
コード例 #6
0
ファイル: a7q6.py プロジェクト: knownastron/cmpt145
def subst(anchor, t, r):
    """
    Purpose
        Replace every occurrence of t in the chain with the value r
    Preconditions:
        :param anchor: a node-chain
        :param t: the target value to be replaced
        :param r: the replacement value
    Post-conditions:
        Every occurrence of t is replaced by r
    Return:
        :return: None
    """

    if anchor is None:
        return
    else:
        data = node.get_data(anchor)
        if data == t:
            node.set_data(anchor, r)
        subst(node.get_next(anchor), t, r)
コード例 #7
0
def set_data_at_index(alist, idx, val):
    """
    Purpose
        Store val into alist at the index idx
    Preconditions:
        :param alist: a list created by create()
        :param val:   a value of any kind
        :param idx:   a non-negative integer
    Post-conditions:
        The value stored at index idx changes to val
    Return:
        :return True if the index was valid, False otherwise
    """
    if idx < 0 or idx >= size(alist):
        return False
    anode = alist['head']
    i = 0
    while anode is not None and i < idx:
        anode = node.get_next(anode)
        i += 1
    node.set_data(anode, val)
    return True
コード例 #8
0
def set_data_at_index(alist, idx, val):
    """
    Purpose
        Store val into alist at the index idx
    Preconditions:
        :param alist: a list created by create()
        :param val:   a value of any kind
        :param idx:   a non-negative integer
    Post-conditions:
        The value stored at index idx changes to val
    Return:
        :return True if the index was valid, False otherwise
    """
    if alist["head"] is None or idx >= alist["size"]:
        return False
    walker = alist["head"]
    index = 0
    while index < idx:
        walker = node.get_next(walker)
        index += 1
    node.set_data(walker, val)
    return True