anonymous
  • anonymous
could someone complete the recursive definition of this function: def interleave(lst_pair): (lst1, lst2)= lst_pair if lst1 == []: return Blank elif lst2== []: return Blank else: return Blank
MIT 6.00 Intro Computer Science (OCW)
katieb
  • katieb
See more answers at brainly.com
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.

Get this expert

answer on brainly

SEE EXPERT ANSWER

Get your free account and access expert answers to this
and thousands of other questions

carlsmith
  • carlsmith
You're referencing things that are not in the code you've posted. I'm assuming that `lst_pair` is a list containing two lists as you then convert it to a tuple containing two lists. (There's a method for that, but either way). Then you return `Blank`, but I can only assume you mean to put `None` or else `Blank` must be a name, but you haven't pointed out what it is. If you've not defined `Blank` elsewhere, i.e. Blank = 5 then it'll crash. There's also no recursive call in the function. You have two base cases, if either list is empty, return `Blank`, but no call to interleave() within interleave(). I'm guessing you'll be putting that in the else clause, but I've no idea what you'll want to pass with the recursive call as I've no idea what the function does. If you post more details, I'll be happy to look at it. You can post the Python file here by using the Attach File feature, that'll make it easier to read and edit. Sorry I couldn't be of more help.
anonymous
  • anonymous
poste your code to dpaste.com (:)) then post the link here
anonymous
  • anonymous
Hi reckkimini, what carlsmith stated is correct, you'll need to provide more detail before we can really answer the question. However, I took a guess at what you wanted, see attached link for code and test function. I'm assuming you are given two list and ask to return a single list. For example, if lst1 is ['a', 'b', 'c', 'd'] and lst2 is [1,2,3,4], you want a return list that's ['a', 1, 'b', 2, 'c', 3, 'd', 4]. Your problem statement does not indicate what to do in the case when one list is longer than the other, so I assume you just want the rest of the remaining list added to the end. I believe your "Blank" is just your way of saying some code needed here. As for testing if the list is empty or not, you should really use the len() function. http://dpaste.com/539237/

Looking for something else?

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

More answers

carlsmith
  • carlsmith
For testing if a list is empty, you can simply use it as an expression in a conditional. Anything empty in Python evaluates to False in a conditional ~ an empty string, list, tuple, dict etc.. Anything that would equal zero if you converted it to an int will also be False, including zero itself. Everything else is True. if some_list: print 'List is non-empty' else: print 'List is empty' This is handy if you have some input that you only want to do something to if it contains information. You might have written a function that checks a site to see if there's anyone about and returns a list of users currently online. a = people_online('www.openstudy.com', 'cs') if a: print 'There are', len(a), people online.' login('www.openstudy.com', 'carlsmith', 'password') else: print 'Everything is quiet at the moment.'
carlsmith
  • carlsmith
To put two sequences together, you can use the zip() built-in. letters = ['a', 'b', 'c', 'd'] numbers = [1, 2, 3, 4] nulist = zip(letters, numbers) this makes `nulist` equal to [('a', 1), ('b', 2), ('c', 3), ('d', 4)] P.S. If you want to have placeholders in your code that wont crash, use `pass`. def func(): pass a = input() if a < 5: pass else: print 'something'
anonymous
  • anonymous
Using the fact that 0 mean false can be dangerous, and can potentially make the code not portable. There can be security risk associated with doing so as well. For example, if someone were to alter 'some_list' variable to be True: accessSecureDataBase() stealData() elif ... They can potentially hack your code. while len(some_list) will always return an int, or raise an error. Either way, hacker won't gain access.
carlsmith
  • carlsmith
I don't see how changing the value of a variable from an innocent list to a function call is exploitable? If it evaluates to False, nothing happens, if it evaluates to True, the code you have written will run. We are really on the level of learning basic programming here so I'm not really focussed on the potential to inject code, and I still don't see how it would be done? Besides, everything I wrote above is generic Python. I've never seen a caveat offered, warning against testing if a list is empty by doing if some_list: It's standard Python.
carlsmith
  • carlsmith
Could you clarify what you meant when you said that using this type of test would affect portability? And how does a hacker, as a result of such a test, change the value of the variable to a function call? If they have that level of control, it's Game Over.
carlsmith
  • carlsmith
Just to be clear: I understand that allowing a 'hacker' to change my variable from a list to a function call could be dangerous, especially if they get to define the function. I didn't mean that this sounds perfectly fine. I meant that it makes no difference in the above example and that using a conditional on a list does nothing to increase the opportunity for a breach.
carlsmith
  • carlsmith
Would you do if len(some_list): which is what you seem to be warning against. Or if len(some_list) != 0: which seems to be no different. I'm at a total loss.
anonymous
  • anonymous
portability in the sense that if the python designers decide that 0 would no longer default to false, then this code would break. In terms of security, some_list can be an raw user input. So a hacker can actually insert hacks into your code. This is actually very common, and it's call code injection. http://en.wikipedia.org/wiki/Code_injection I'm just trying to point out some potential issues. Of course you can easily safeguard against it, but it's just something to keep in mind when people start working on more complex systems. For a beginner class, it's always easy to pick up good and bad habits.
anonymous
  • anonymous
doing if len(some_list) is safer but doesn't solve the portability issue. in addition, the extra comparison in the back would be another thing the hacker need to get correct before his/her injected code can run.
carlsmith
  • carlsmith
The chance of Python developers making 0 not evaluate to False is pretty slim. You'd have a pretty lengthy heads up, given that no one would like it, and you don't have to 'upgrade' to this new version either. You may as well say not to use len(), because it might be depreciated one day. I know what code injection means ~ I used the term earlier in this discussion. I don't see what you're getting at, but fair play, I'm only learning myself.
carlsmith
  • carlsmith
Just as an aside: You've used print statements throughout your code, which is depreciated in Python3. Assuming you've written Python2 code, then there is no chance of any language changes as Python2.7 is the last one. They aren't making any more versions.
anonymous
  • anonymous
yea but 2.7 is great and there are lots of libraries/packages out there for 2.x
carlsmith
  • carlsmith
I wasn't knocking 2.7?
anonymous
  • anonymous
:)
anonymous
  • anonymous
We're all learning, so it's good to hear what others have to say, especially when we're not in a traditional class setting. So our replies are usually a little delayed, but the point gets through. heehee.

Looking for something else?

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