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

Pages: 1-

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 16:27


double xs []     = xs
double xs (y:ys) = double ys $ (take y xs) ++ (xs !! y * 2) ++ (drop (y + 1) xs)

Name: Anonymous 2010-10-28 16:31

that isn't python :(

Name: Anonymous 2010-10-28 16:46

>>3
No [code] tags, so mailto:sage, no python.

Name: Anonymous 2010-10-28 16:46

s/so/no

Name: Anonymous 2010-10-28 16:54

how does anyone tell the difference between italian, spanish and french uh

Name: Anonymous 2010-10-28 16:58

>>6
if you speak one language, it may be easier, but otherwise generalizations about the people may be the only way

Name: Anonymous 2010-10-28 17:21


def double(A, B):
    R = A

    for i in B:
        R[i] = R[i] * 2

    return R

Name: Anonymous 2010-10-28 17:38

>>8
Modifies the original list


def ListIndexedDoubler(src, indexes):
    res = list(src)
    for i in indexes:
        res[i] *= 2
    return res

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)).

Name: Anonymous 2010-10-28 18:06

>>10
indicesindexes
:ーO

Name: Anonymous 2010-10-28 18:16

>>2


import Data.Sequence
import Data.Foldable as F

double :: (Num n) => Seq n -> Seq Int -> Seq n
double = F.foldl (flip (adjust (*2)))

-- ghci> double (fromList [10, 20, 30, 40]) (fromList [0, 0, 1, 2])
-- fromList [40, 40, 60, 40]

Name: Anonymous 2010-10-28 23:05

>>11
Upvote for use of U+2026 Horizontal Ellipsis

Name: Anonymous 2010-10-28 23:46

>>11
Downvote for use of faggot characters. Fuck, I might as well just write an OCR to approximate your faggot shit to proper ASCII that will render correctly in my /dev/tty0.

Name: Anonymous 2010-10-28 23:46

>>11
Downvote for use of faggot characters. Fuck, I might as well just write an OCR to approximate your faggot shit to proper ASCII that will render correctly in my /dev/tty0.

Name: Anonymous 2010-10-29 0:39

The world has moved beyond ASCII, >>14-15.

Name: Anonymous 2010-10-29 1:56

∴…

Name: PHP FAGGOT 2010-10-29 5:10

[spoilerr]BAD PHP ``FUNCTIONAL'' PROGRAMMING[/spoiler]

function faggot ($a, $b) {
    array_map(function ($c) use (&$a) { $a[$c] *= 2; }, $b);
    return $a;
    }


It works.

Name: Anonymous 2010-11-26 20:47

Name: Anonymous 2011-02-03 7:28

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