A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

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

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

    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.

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

    poste your code to dpaste.com (:)) then post the link here

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

    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/

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

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

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

    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'

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

    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.

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

    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.

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

    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.

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

    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.

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

    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.

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

    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.

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

    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.

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

    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.

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

    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.

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

    yea but 2.7 is great and there are lots of libraries/packages out there for 2.x

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

    I wasn't knocking 2.7?

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

    :)

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

    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.

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