Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Python

Name: Anonymous 2010-10-28 16:16

I am trying to write a program using a for or while loop that takes in two lists ([10,20,30,40] and [0,0,1,2]) for instance and the values of the second list are supposed to be the indexes of the first list that are supposed to be doubled

output: [40, 40, 60, 40]
the original string isn't supposed to be changed
does anyone have any suggestions?  I've gotten:

def double(A,B):
  i = 0
  a = []
  while i < len(B):
    a = A[B[i]] * 2
    i += 1
  return A

Name: Anonymous 2010-10-28 17:56

Destructive version (add an &aux sequence (copy-seq sequence) to the lambda-list, if you want it to be non-distructive):

(defun double-sequence-by-indices (sequence indexes)
  (dolist (index indexes sequence)
    (symbol-macrolet ((element (elt sequence index)))
      (incf element element))))

Example:
CL-USER> (double-sequence-by-indices '(10 20 30 40) '(0 0 1 2))
(40 40 60 40)

Works on any kind of sequence (list, array or user-defined sequences (only some implementations support those)).

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List