A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

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

  • This Question is Closed
  1. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  2. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  3. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I can'tget it to output the right thing

  4. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  5. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  6. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Why does it specifically have to be recursive?

  7. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  8. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  9. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  10. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  11. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  12. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  13. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  14. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  15. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    thats good, leaves me to write less fail safes

  16. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  17. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  18. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    additional*

  19. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  20. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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]

  21. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  22. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  23. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Onyx split is alright

  24. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  25. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  26. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  27. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    no Onyx I compare ur codes with mine's

  28. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    and try to make mine shorter

  29. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  30. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  31. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  32. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  33. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    that's why I ask for u guys help

  34. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  35. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  36. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  37. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  38. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  39. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Nessman did you already do interleave()?

  40. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  41. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  42. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  43. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

    1 Attachment
  44. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  45. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  46. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  47. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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'

  48. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  49. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  50. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  51. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  52. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  53. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    ok Onyx show Nessman

  54. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  55. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  56. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  57. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    ok thanx onyx have a gud night

  58. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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:])

  59. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

    1 Attachment
  60. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  61. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    ok Ness

  62. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    okay so what is next on the docket?

  63. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  64. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    lol ok Onyx

  65. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  66. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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]

  67. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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
  68. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    ok so wat was ur output for the deal function?

  69. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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
  70. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    k interleave worked perfectly thanx agan

  71. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  72. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  73. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    can I attach a pdf file here?

  74. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    only one way to find out

  75. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    ok hehe

  76. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    it doesn't work it took forever

  77. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  78. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  79. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  80. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  81. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  82. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  83. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  84. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  85. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  86. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  87. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    there is the book

  88. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    hope u like

  89. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  90. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  91. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    hola nessman did u check out the book

  92. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Hey what do you still need done?

  93. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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)

  94. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  95. anonymous
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

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

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.