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

Pages: 1-

Very simple python question

Name: Anonymous 2010-10-07 21:18

>>> prefixes("overall")
['', 'o', 'ov', 'ove', 'over', 'overa', 'overal', 'overall']

I need to be able to return the statement above, so far all I have is:

def prefixes(x):
    return [v for v in

I don't know where to go from here or if what I have so far is wrong, but I know that the statement should only be about 1-2 lines.  I was told that there should be a slicing statement

is anyone able to help me with this program?

Name: Anonymous 2010-10-07 21:25

Just promise to suck someone off if they do it for you, like you usually do, Leah.

Name: Anonymous 2010-10-07 22:02

def prefixes(s):
  return map(lambda x: s[0:x], range(len(s) + 1))


I don't even know Python.

Name: Anonymous 2010-10-07 22:09

Expected:
    ['', 'o', 'ov', 'ove', 'over', 'overa', 'overal', 'overall']
Got:
    <map object at 0x0000000003040898>

but oddly enough there were 3 examples, and one of them was correct with that line

Name: Anonymous 2010-10-07 22:12

>>> def prefixes(s):
...   return map(lambda x: s[0:x], range(len(s) + 1))
...
>>> prefixes('overall')
['', 'o', 'ov', 'ove', 'over', 'overa', 'overal', 'overall']
>>> prefixes([5,3,7,6,1,8,5,9,7,2,9])
[[], [5], [5, 3], [5, 3, 7], [5, 3, 7, 6], [5, 3, 7, 6, 1], [5, 3, 7, 6, 1, 8], [5, 3, 7, 6, 1, 8, 5], [5, 3, 7, 6, 1, 8, 5, 9], [5, 3, 7, 6, 1, 8, 5, 9, 7], [5, 3, 7, 6, 1, 8, 5, 9, 7, 2], [5, 3, 7, 6, 1, 8, 5, 9, 7, 2, 9]]
>>> prefixes(["your", "dum", "lol"])
[[], ['your'], ['your', 'dum'], ['your', 'dum', 'lol']]

Name: Anonymous 2010-10-07 22:17

I'm still getting <map object at 0x0000000003040898>
tried with 2.7 and 3.1
what does the lambda part do?

Name: Anonymous 2010-10-07 22:21

>>6
"lambda x: s[0:x]" is the function
def f(x):
  return s[0:x]

being passed to the map function.

http://en.wikipedia.org/wiki/Anonymous_function

Name: Anonymous 2010-10-07 22:35

nothing has worked so far...fuck

Name: Anonymous 2010-10-07 23:00

Cast it to a list, Jesus fucking Christ. You need to drop that class and switch to an English major or something.

Name: Anonymous 2010-10-07 23:02

I don't pay attention because I hate python, I only have to take the class because its a prerequisite because apparently its a good language to start people off in.  prior to it I have taken a year of VB, C and some C#, i'm in high school taking a fucking college class (that I should have been able to test out of)

Name: Anonymous 2010-10-07 23:16

string * prefixes(char * str)
{
    int length = 0;
    while(str[length] != 0)
    {
        ++length;
    }
    string temp = str;
    string * holder = new string[length+2];
    holder[length+1] = "$";
    holder[length] = str;
    for(register int index = length;index > 0;index--)
    {

        temp[index-1] = 0;
        holder[index-1] = temp;


    }
    return holder;
}

Name: Anonymous 2010-10-07 23:22

prefixes = scanl (++) [] . map (: [])

Name: Anonymous 2010-10-08 0:02

>>12
More like prefixes = Data.List.inits

Name: Anonymous 2010-10-08 0:03

>>10
(that I should have been able to test out of)
If you can't do this then no, you shouldn't have.

Name: Anonymous 2010-10-08 0:57

>>13
My implementation is twice as fast as the one used in Data.List.inits.

Name: Anonymous 2010-10-08 1:15


def prefixes(alist):
   s = [i for i in alist]
   for i in range(s.__len__()):
      print s[0:i+1]


your job is to make the list of strings according to your original output

Name: Anonymous 2010-10-08 3:35

DON'T HELP HIM!!!

Name: Anonymous 2010-10-08 3:56

(define (prefixes s)
    (let loop ((r '()) (s s))
        (if (equal? s "")
            (cons s r)
            (loop (cons s r)
                  (substring s 0 (- (string-length s) 1))))))

Name: Anonymous 2010-10-08 4:07

prefixes = lambda s: [s[:i] for i in range(len(s) + 1)]

Name: Anonymous 2010-10-08 4:59

>>19
This. >>16 is an idiot. Oh, ever heard of the len function, by the way?

Also, don't do his homework!

Name: Anonymous 2010-10-08 7:06

Easy enough:

(defun prefixes (sequence)
  (loop for i from 1 to (length sequence)
        collect (subseq sequence 0 i)))

;CL-USER> (prefixes "overall")
;("o" "ov" "ove" "over" "overa" "overal" "overall")
;CL-USER> (prefixes #(1 2 3))
;(#(1) #(1 2) #(1 2 3))
;CL-USER> (prefixes '(some list))
;((SOME) (SOME LIST))

Name: >>21 2010-10-08 7:08

Oh, and if you want the empty list to be always included (I thought it'd be pointless), just change from 1 to from 0.

Name: Anonymous 2010-10-08 9:25

>>1


def return_prefix (x):
    for v in x:
        for z in v:
            print z


This should get you a preview of what you should do darling.

Name: Anonymous 2010-10-08 9:52

>>19
prefixes = lambda s:
Why not just prefixes s =?

Name: Anonymous 2010-10-08 9:53

>>24
Wait, never mind. Forgot how Python functions are written.

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