A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

can anyone help with this? Write a program in python that splits a list of integers in a tuple of two list the first list is half the given integer value of the given integer list and the second is the second half of the integer list (if the list length is odd then the second half of the pair should be longer) eg . ([1,3,5,6,11,5,6]) => ([1,3,5], [6,11,5,6]0

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

    myTuple = ([1,3,5,6,11,5,6],) myTuple += ([],) index = len(myTuple[0] ) // 2 while( len( myTuple[0] ) > index ): myTuple[1].append( myTuple[0].pop( index ) ) print myTuple

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

    Should be as simple as. split = len(myTuple[])/2 firstlist = myTuple[0:split] secondlist myTuple[split + 1:]

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

    lando pretty much nailed it there, but I just wondered why there is an empty list on line one? split = len(myTuple[])/2 That'll throw a syntax error. It should read split = len(myTuple)/2 I would've thought.

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

    I'll admit that I may have overdone the loop, but neither of these two snippets will produce the output in the example from the input.

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

    This seems to do the job.

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

    Yes it does.

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

    Hmmm, I don't code in Python so the syntax is a bit off. It seems to me that the logic is correct and it runs in constant time vs O(n) time for your solution rsmith.

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

    Carls implementation is a nicer than mine of course.

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

    I just try to keep it Pythonic. Simple is beautiful, never optimise prematurely. I did like your solution ~ it just does the job, no nonsense ~ you just got the Python a bit off.

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

    I've got to get used to the fact that Python can return multiple values from a method. I'm used to only being able to return one value.

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