Ask your own question, for FREE!
Computer Science 26 Online
OpenStudy (anonymous):

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

OpenStudy (rsmith6559):

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

OpenStudy (anonymous):

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

OpenStudy (carlsmith):

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.

OpenStudy (rsmith6559):

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.

OpenStudy (carlsmith):

This seems to do the job.

OpenStudy (rsmith6559):

Yes it does.

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

Carls implementation is a nicer than mine of course.

OpenStudy (carlsmith):

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.

OpenStudy (anonymous):

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.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Latest Questions
kaelynw: who should I draw (anime characters)
29 seconds ago 7 Replies 2 Medals
kaelynw: should I post my art?
1 hour ago 11 Replies 3 Medals
danielfootball123: Question.... How much is questioncove worth if I were to buy it?
5 hours ago 5 Replies 0 Medals
HeyItsAlicia: Mits midnight!!! Happy 16th bday to me !!
2 hours ago 39 Replies 8 Medals
XShawtyX: Art
29 minutes ago 2 Replies 1 Medal
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!