A community for students.
Here's the question you clicked on:
 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
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

rsmith6559
 5 years ago
Best ResponseYou've already chosen the best response.0myTuple = ([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

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Should be as simple as. split = len(myTuple[])/2 firstlist = myTuple[0:split] secondlist myTuple[split + 1:]

carlsmith
 5 years ago
Best ResponseYou've already chosen the best response.2lando 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.

rsmith6559
 5 years ago
Best ResponseYou've already chosen the best response.0I'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.

carlsmith
 5 years ago
Best ResponseYou've already chosen the best response.2This seems to do the job.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Hmmm, 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.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Carls implementation is a nicer than mine of course.

carlsmith
 5 years ago
Best ResponseYou've already chosen the best response.2I 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.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0I'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.
Ask your own question
Sign UpFind more explanations on OpenStudy
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
 Engagement 19 Mad Hatter
 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.