Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 16 Online
OpenStudy (anonymous):

Could someone help me with this Write a recursive function interleave, which takes a pair of two integer lists as input and returns a list which contains all the items of the input lists, but in alternating order

OpenStudy (anonymous):

I'll be on again later tonight and I'll try to do it.

OpenStudy (anonymous):

What problems are you having with the function? What do you have so far?

OpenStudy (anonymous):

I can'tget it to output the right thing

OpenStudy (anonymous):

post the code and we can try to help, and should it be two lists or one list that is then broken int two ?

OpenStudy (anonymous):

ok it should output this >>>interleave ([1,2,3], [4,5,6,7]) [1,4,2,5,3,6,7]

OpenStudy (anonymous):

Why does it specifically have to be recursive?

OpenStudy (anonymous):

what should happen in the event of a >>>interleave ([1,2,3], [4,5,6,7,8,9,10])?

OpenStudy (anonymous):

That cant happen because I wrote the code for here last night that it splits them in half or if it is an odd number it makes the second list bigger by 1.

OpenStudy (anonymous):

Here was my code lis=list(raw_input('Please enter a list:')) x=len(lis)/2 lis1=lis[:x] lis2=lis[x:] print lis1,lis2

OpenStudy (anonymous):

if interleave([1,2,3],[4,5,6,7,8,9,10]) would output [1,4,2,5,3,6,7,8,9,10]

OpenStudy (anonymous):

interleave is the opposite of the function u just wrote onyx, I tried zip with two list n I end up with a list of tuples

OpenStudy (anonymous):

o ok I thought we were adding on to mine maybe as a simple shuffling method?

OpenStudy (anonymous):

well the split function that u wrote onyx is just say a subscript of a bigger method shuffle which calls interleave and split

OpenStudy (anonymous):

Yea so you would run split first and then interleave and you would do that repeatedly which would give you a shuffled deck. but since split is run first there would be no way to get a lisst with len 3 and one with length 7

OpenStudy (anonymous):

thats good, leaves me to write less fail safes

OpenStudy (anonymous):

ok this is shuffle:Define a function shuffle() which repeatedly splits a list in half, then interleaves the two resulting lists together. Your function should also take an additional integer parameter to control the number of split/interleave steps i.e.

OpenStudy (anonymous):

ok so do you want me to change mine to take an addition integer?

OpenStudy (anonymous):

additional*

OpenStudy (anonymous):

ok so onyx are you interleaving or shuffling?, and what am i taking?

OpenStudy (anonymous):

shuffle output is:>>>shuffle (1, [1 ,2 ,3 ,4 ,5 ,6 ,7]) 1 ,4 ,2 ,5 ,3 ,6 ,7] >>>shuffle (2, [1 ,2 ,3 ,4 ,5 ,6 ,7]) (intermediate step - shuffle (1, [1 ,4 ,2 ,5 ,3 ,6 ,7]) ) [1 ,5 ,4 ,3 ,2 ,6 ,7]

OpenStudy (anonymous):

also reckk, are we doing your work for you? because if you give us a better layout of the work we're doing for you, that'd be nice

OpenStudy (anonymous):

Reckk sorry but your thing didn't make any sense to me. Nessman I already did split but now it seems they want something new so I might have to do it over

OpenStudy (anonymous):

Onyx split is alright

OpenStudy (anonymous):

no, it looks like he wants split, one function, interleave, another function, and shuffle a function that takes them both in order in an attempt to shuffle

OpenStudy (anonymous):

also rekk, do you need a shuffle program, my dealing program is random, so you know

OpenStudy (anonymous):

Reck I'm not trying to be mean here but why try to make a go fish game if you can't code it yourself what practice is taking all our code copying and pasting it and seeing if output is right.

OpenStudy (anonymous):

no Onyx I compare ur codes with mine's

OpenStudy (anonymous):

and try to make mine shorter

OpenStudy (anonymous):

um then y don't you post the codes of what you have and we can make any necessary changes.

OpenStudy (anonymous):

ok def split(b): c=list(b) x=len(c)/2 a=c[:x] d=c[x:] return a,d

OpenStudy (anonymous):

ok what about the shuffle one you need post what you have so far.

OpenStudy (anonymous):

that's where I am at now I tried interleave using zip but it didnt work

OpenStudy (anonymous):

that's why I ask for u guys help

OpenStudy (anonymous):

hey onyx, this if fun, and it doesn't hurt us. maybe this is part of a dumb pre req course he never plans to peruse. rekk i'll help you if you want to come clean that we are doing you work, i'll help you regardless, not sure about onyx, seems to have a problem with doing others work

OpenStudy (anonymous):

I will too I'm fine with helping I just want to know what were doing it for that's what I was trying to get to

OpenStudy (anonymous):

ok it's basically an assignment , like the cesar cipher I sent u Nessman

OpenStudy (anonymous):

I am far from being a pro in python programming so I take or look for help wherever I can

OpenStudy (anonymous):

Ok that's fine is it for college,high school, or just a for fun assignment? And I'm working on it right now.

OpenStudy (anonymous):

Nessman did you already do interleave()?

OpenStudy (anonymous):

no i have not, looking over his cipher functions, impressive method (thus far)

OpenStudy (anonymous):

it's for college the cesar cipher was the 1st assignment

OpenStudy (anonymous):

this one is the 2nd,Nessman the deal() function that u wrote gave me a syntax error about the attribute of pop

OpenStudy (anonymous):

try this code, its the final version, i uploaded the wrong one last night

OpenStudy (anonymous):

oh I forgot here's a hint for interleave:[Hint: If a list is longer than the other then the additional elements should be placed at the end of the new list.]

OpenStudy (anonymous):

i don't understand what your nested if loops in encode and decode do, and my style would have let me put the key as an argument so you need to know the magic number to unlock the data

OpenStudy (anonymous):

and i don't like gen_decode and gen_encode, they don't tell you the key and don't let you input the key, you can't use one to reverse the other, its a little too random

OpenStudy (anonymous):

well the lecturer asked for the opposite of all that u said, she let key=2 be a constant value in encode and decode so for a string encode('mad') would output 'ocf' and decode('ocf') would output 'mad'

OpenStudy (anonymous):

gen_decode and gen_encode took gen_key which return a random key and the decode or encode the string but I messed up with the last part so it didn't work properly gen_decode should give u back what was gen_encode

OpenStudy (anonymous):

well the problem is that you have no way of making sure they have the same key if its randomly generated, its a giant problem unless you can save the key with the output, then it becomes no more secure then encode-decode if you want to shave lines remove the key = 2/gencode lines, you only use the variable once, and it never changes, this only removes 5 lines total, but those can obviously be cut

OpenStudy (anonymous):

Hey reckk does it really have to be recursive because if not I think I'd be done by now. haha I never completely understood recursive functions so it takes me a while to write them.

OpenStudy (anonymous):

give me what you have onyx, i might be able to help recur it (recursive is not what i would choose in this situation)

OpenStudy (anonymous):

oh for the encode decode thing, if you can import specific words instead of the whole module, its just good style

OpenStudy (anonymous):

ok Onyx show Nessman

OpenStudy (anonymous):

def interleave(lis1,lis2): interWove=[lis1[0],lis2[0]] if len(lis1)>1 and len(lis2)>1: return interWove.append(interleave(lis1[1:],lis2[1:])) elif len(lis2)>1: return interWove.append(interleave(None,lis2[1:])) else: return interWove.append(interleave(lis1[1:],None)) print interleave([1,3,5,7],[2,4,6,8]) What I was trying to do was everytime it is called it will append just the first numbers(numbers in lis1[0],lis2[0]. but like I said I'm not good at recursive haha.

OpenStudy (anonymous):

This one works but only if they are the same length I have to go to bed so you can try and mod it to work with different lengths I know how, you can use if statements but I was trying to figure out a shorter way since you said you wanted short codes.

OpenStudy (anonymous):

def interleave(lis1,lis2): if len(lis1)==0 and len(lis2)==0: return [] interWove=[lis1[0],lis2[0]] print interWove return interWove+interleave(lis1[1:],lis2[1:]) print interleave([1,3,5,7],[2,4,6,8])

OpenStudy (anonymous):

ok thanx onyx have a gud night

OpenStudy (anonymous):

Lol I always say I'm about to get off but i can't stop without finishing something haha here it is where it works for both. def interleave(lis1,lis2): if len(lis1)==0 and len(lis2)==0: return [] if len(lis1)==0: return lis2 if len(lis2)==0: return lis1 interWove=[lis1[0],lis2[0]] return interWove+interleave(lis1[1:],lis2[1:])

OpenStudy (anonymous):

finished mine too. not as short, but it gets the job done

OpenStudy (anonymous):

not sure what he means for both but mine will take uneven length lists without slowing down

OpenStudy (anonymous):

ok Ness

OpenStudy (anonymous):

okay so what is next on the docket?

OpenStudy (anonymous):

thats what I meant by both uneven and even and good night for real this time. haha

OpenStudy (anonymous):

lol ok Onyx

OpenStudy (anonymous):

Your code work perfectly I will try to mod it so it's concise but I like it Define a function shuffle() which repeatedly splits a list in half, then interleaves the two resulting lists together. Your function should also take an additional integer parameter to control the number of split/interleave steps i.e.

OpenStudy (anonymous):

Shuffle outputs: >>>shuffle (1, [1 ,2 ,3 ,4 ,5 ,6 ,7]) 1 ,4 ,2 ,5 ,3 ,6 ,7] >>>shuffle (2, [1 ,2 ,3 ,4 ,5 ,6 ,7]) (intermediate step - shuffle (1, [1 ,4 ,2 ,5 ,3 ,6 ,7]) ) [1 ,5 ,4 ,3 ,2 ,6 ,7]

OpenStudy (anonymous):

the file name says interleave but it has the entire interleave, split, and shuffle (split courtesy of onyx), it uses my version of interleave but will work with onyx's code, take her for a test drive, and once again my deal function is random, might cut out the entire shuffle stage on the final product

OpenStudy (anonymous):

ok so wat was ur output for the deal function?

OpenStudy (anonymous):

ok my deal file wasn't the right one last night as well, or what ever i messed up earlier, this one produces a tuple with two parts, the first part is a list of hands, each hand being a list of tuples, each tuple being a card pair, and the second part of hte first tuple is the remaining cards in teh deck the hands are randomized

OpenStudy (anonymous):

k interleave worked perfectly thanx agan

OpenStudy (anonymous):

so if you enter "print deal(deck(),5,2)" you'll see what i mean, i just load my deck function strait into the dealing function, its a nice program, thank the man who made the random module

OpenStudy (anonymous):

yeah they a very good a programming to come up wiith these module's

OpenStudy (anonymous):

can I attach a pdf file here?

OpenStudy (anonymous):

only one way to find out

OpenStudy (anonymous):

ok hehe

OpenStudy (anonymous):

it doesn't work it took forever

OpenStudy (anonymous):

n didn't work I have a cool python book that I wanted to share it has tons of python programs

OpenStudy (anonymous):

perhaps you can post it online, do you have dropbox, or regularly use a file uploading service?

OpenStudy (anonymous):

no I don't only use em for uploading game file

OpenStudy (anonymous):

ok try this link file:///C:/Documents%20and%20Settings/Maxin/My%20Documents/School%20work/Com%20sci/Python%20books/IYOCGwP_book1.pdf

OpenStudy (anonymous):

that's a link to your local file, that's definitely not going to work for me, i need like a " http://dl.dropbox.com/u/18955243/How%20to%20use%20the%20Public%20folder.rtf " a link to a web adress, this link contains a text file introducing the public folder on the service called dropbox, dropbox is a good service, has a free option, i suggest you look into it

OpenStudy (anonymous):

ok I am gonna look on it now, look at this final question for me please: Write a function askForcard() which takes a card and a hand and return a tuple where first part of the tuple is a Boolean value if the cardvalue is found in the hand. For example, if we are asking our opponent for a ‘J’ and the opponents hand is [('H', 'A'), ('D', 'J')] we are retuned tuple where first part is a boolean value indicating if the card is present and second part returns the position of the card in the opponents hand, if the card is not there just return 0 for position. E.g.

OpenStudy (anonymous):

it outputs: >>> askForCard('J', [('H', 'A'), ('D', 'J')]) (True, 1) >>> askForCard('4', [('H', '2'), ('D', 'J')]) (False, 0) 7. when I finish implement everything I am gonna send it to u and u give me ur idea r ideas on how u could make the level of the computer more difficult

OpenStudy (anonymous):

ok i'm tired, i'll work on it tomorrow? how far ahead of schedule are you?

OpenStudy (anonymous):

its due friday but go get some rest. hopefully u here 2marrow same time. I am gonna try the drop box thing b4 u leave so gimme 10-15mins I just read it

OpenStudy (anonymous):

http://dl.dropbox.com/u/26114070/IYOCGwP_book1.pdf

OpenStudy (anonymous):

there is the book

OpenStudy (anonymous):

hope u like

OpenStudy (anonymous):

thanks reckk, i'll read it when i'm not so tired

OpenStudy (anonymous):

ok have a great night am gonna get some zzzzzzzzz's also

OpenStudy (anonymous):

hola nessman did u check out the book

OpenStudy (anonymous):

Hey what do you still need done?

OpenStudy (anonymous):

a function askForcard() which takes a card and a hand and return a tuple where first part of the tuple is a Boolean value if the cardvalue is found in the hand. For example, if we are asking our opponent for a ‘J’ and the opponents hand is [('H', 'A'), ('D', 'J')] we are retuned tuple where first part is a boolean value indicating if the card is present and second part returns the position of the card in the opponents hand, if the card is not there just return 0 for position. E.g. >>> askForCard('J', [('H', 'A'), ('D', 'J')]) (True, 1) >>> askForCard('4', [('H', '2'), ('D', 'J')]) (False, 0)

OpenStudy (anonymous):

I will try but Idk if I can do that one sorry

OpenStudy (anonymous):

did u guys ever finish this assignment? because i need help with this same assignment and i can't access the files that was uploaded

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!