anonymous
  • 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
MIT 6.00 Intro Computer Science (OCW)
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
katieb
  • katieb
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
anonymous
  • anonymous
I'll be on again later tonight and I'll try to do it.
anonymous
  • anonymous
What problems are you having with the function? What do you have so far?
anonymous
  • anonymous
I can'tget it to output the right thing

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

anonymous
  • 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 ?
anonymous
  • anonymous
ok it should output this >>>interleave ([1,2,3], [4,5,6,7]) [1,4,2,5,3,6,7]
anonymous
  • anonymous
Why does it specifically have to be recursive?
anonymous
  • anonymous
what should happen in the event of a >>>interleave ([1,2,3], [4,5,6,7,8,9,10])?
anonymous
  • 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.
anonymous
  • 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
anonymous
  • 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]
anonymous
  • 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
anonymous
  • anonymous
o ok I thought we were adding on to mine maybe as a simple shuffling method?
anonymous
  • anonymous
well the split function that u wrote onyx is just say a subscript of a bigger method shuffle which calls interleave and split
anonymous
  • 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
anonymous
  • anonymous
thats good, leaves me to write less fail safes
anonymous
  • 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.
anonymous
  • anonymous
ok so do you want me to change mine to take an addition integer?
anonymous
  • anonymous
additional*
anonymous
  • anonymous
ok so onyx are you interleaving or shuffling?, and what am i taking?
anonymous
  • 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]
anonymous
  • 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
anonymous
  • 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
anonymous
  • anonymous
Onyx split is alright
anonymous
  • 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
anonymous
  • anonymous
also rekk, do you need a shuffle program, my dealing program is random, so you know
anonymous
  • 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.
anonymous
  • anonymous
no Onyx I compare ur codes with mine's
anonymous
  • anonymous
and try to make mine shorter
anonymous
  • anonymous
um then y don't you post the codes of what you have and we can make any necessary changes.
anonymous
  • anonymous
ok def split(b): c=list(b) x=len(c)/2 a=c[:x] d=c[x:] return a,d
anonymous
  • anonymous
ok what about the shuffle one you need post what you have so far.
anonymous
  • anonymous
that's where I am at now I tried interleave using zip but it didnt work
anonymous
  • anonymous
that's why I ask for u guys help
anonymous
  • 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
anonymous
  • 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
anonymous
  • anonymous
ok it's basically an assignment , like the cesar cipher I sent u Nessman
anonymous
  • anonymous
I am far from being a pro in python programming so I take or look for help wherever I can
anonymous
  • 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.
anonymous
  • anonymous
Nessman did you already do interleave()?
anonymous
  • anonymous
no i have not, looking over his cipher functions, impressive method (thus far)
anonymous
  • anonymous
it's for college the cesar cipher was the 1st assignment
anonymous
  • anonymous
this one is the 2nd,Nessman the deal() function that u wrote gave me a syntax error about the attribute of pop
anonymous
  • anonymous
try this code, its the final version, i uploaded the wrong one last night
1 Attachment
anonymous
  • 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.]
anonymous
  • 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
anonymous
  • 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
anonymous
  • 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'
anonymous
  • 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
anonymous
  • 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
anonymous
  • 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.
anonymous
  • 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)
anonymous
  • anonymous
oh for the encode decode thing, if you can import specific words instead of the whole module, its just good style
anonymous
  • anonymous
ok Onyx show Nessman
anonymous
  • 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.
anonymous
  • 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.
anonymous
  • 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])
anonymous
  • anonymous
ok thanx onyx have a gud night
anonymous
  • 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:])
anonymous
  • anonymous
finished mine too. not as short, but it gets the job done
1 Attachment
anonymous
  • anonymous
not sure what he means for both but mine will take uneven length lists without slowing down
anonymous
  • anonymous
ok Ness
anonymous
  • anonymous
okay so what is next on the docket?
anonymous
  • anonymous
thats what I meant by both uneven and even and good night for real this time. haha
anonymous
  • anonymous
lol ok Onyx
anonymous
  • 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.
anonymous
  • 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]
anonymous
  • 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
1 Attachment
anonymous
  • anonymous
ok so wat was ur output for the deal function?
anonymous
  • 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
1 Attachment
anonymous
  • anonymous
k interleave worked perfectly thanx agan
anonymous
  • 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
anonymous
  • anonymous
yeah they a very good a programming to come up wiith these module's
anonymous
  • anonymous
can I attach a pdf file here?
anonymous
  • anonymous
only one way to find out
anonymous
  • anonymous
ok hehe
anonymous
  • anonymous
it doesn't work it took forever
anonymous
  • anonymous
n didn't work I have a cool python book that I wanted to share it has tons of python programs
anonymous
  • anonymous
perhaps you can post it online, do you have dropbox, or regularly use a file uploading service?
anonymous
  • anonymous
no I don't only use em for uploading game file
anonymous
  • anonymous
ok try this link file:///C:/Documents%20and%20Settings/Maxin/My%20Documents/School%20work/Com%20sci/Python%20books/IYOCGwP_book1.pdf
anonymous
  • 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
anonymous
  • 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.
anonymous
  • 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
anonymous
  • anonymous
ok i'm tired, i'll work on it tomorrow? how far ahead of schedule are you?
anonymous
  • 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
anonymous
  • anonymous
http://dl.dropbox.com/u/26114070/IYOCGwP_book1.pdf
anonymous
  • anonymous
there is the book
anonymous
  • anonymous
hope u like
anonymous
  • anonymous
thanks reckk, i'll read it when i'm not so tired
anonymous
  • anonymous
ok have a great night am gonna get some zzzzzzzzz's also
anonymous
  • anonymous
hola nessman did u check out the book
anonymous
  • anonymous
Hey what do you still need done?
anonymous
  • 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)
anonymous
  • anonymous
I will try but Idk if I can do that one sorry
anonymous
  • 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

Looking for something else?

Not the answer you are looking for? Search for more explanations.