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
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
Should be as simple as. split = len(myTuple[])/2 firstlist = myTuple[0:split] secondlist myTuple[split + 1:]
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.
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.
This seems to do the job.
Yes it does.
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.
Carls implementation is a nicer than mine of course.
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.
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.
Join our real-time social learning platform and learn together with your friends!