G; assign(L, index, val): Returns a new list identical to L, but in which the item at the index position in L is replaced by val.

From EECS 182
G; assign(L, index, val): Returns a new list identical to L, but in which the item at the index position in L is replaced by val.
Jump to: navigation, search
  1. Thinking approach: Don't see an obvious base case. So, probably not a recursive solution.
  2. Think of another way. Slicing is available to us. May be make use of that. Chop into
  3. three parts: items before the value, the value to be replaced, and the items after.
  4. replace the middle part with the new value, and reassemble the three parts. To
  5. reassemble, make use of the concat function that we already wrote.

def assign(L, index, val):

   if (len(L) > index+1):
       return concat(L[0..index-1].append(val),  L[index+1:])
   elif (len(L) == index):
       return L[0..index-1].append(val)
   else:
       return "error"
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox
EECS @ UM
Tools