r/dailyprogrammer • u/rya11111 3 1 • Mar 13 '12
[3/13/2012] Challenge #23 [easy]
Input: a list
Output: Return the two halves as different lists.
If the input list has an odd number, the middle item can go to any of the list.
Your task is to write the function that splits a list in two halves.
12
Upvotes
3
u/CarNiBore Mar 13 '12
JavaScript
4
u/stevelosh Mar 13 '12
Clojure:
3
u/prophile Mar 13 '12
Python:
2
u/pheonixblade9 Mar 14 '12
I'm really tempted to do all these challenges in assembly, but they're all OO problems...
2
u/drb226 0 0 Mar 14 '12
Haskell:
2
u/Yuushi Mar 15 '12
Java, with the addition of letting you split a list into any number of sublists:
1
u/luxgladius 0 0 Mar 13 '12
Perl:
Only issue is you have to return the lists as references due to Perl's return strategy.
1
u/mattryan Mar 13 '12
Java:
1
u/JerMenKoO 0 0 Mar 13 '12
Python 3:
1
u/nikoma Mar 13 '12
Python 3:
1
u/huck_cussler 0 0 Mar 13 '12
Java:
1
u/Devanon Mar 14 '12
Ruby:
1
u/school_throwaway Mar 14 '12
after many tries and fails in python
1
u/efermat Mar 14 '12
C (no input/malloc validation)
Any improvement suggestions?
1
u/Zardoz84 Mar 15 '12 edited Mar 15 '12
In D this do it :
I can use a tupla to return two arrays instead a array of arrays, but is a feature of D that I never used.
Edit: I tested now. It just works
1
u/callthepolice Mar 15 '12
Scala
val list = List(1, 2, 3, 4) val (first, second) = list.splitAt(list.size/2)
1
u/sanitizeyourhands Mar 15 '12
C# (still learning so I'm sure there are MUCH better ways to do this, but this one was fun):
1
u/namekuseijin Mar 18 '12
plain Scheme
1
u/Should_I_say_this Jun 24 '12 edited Jun 24 '12
Python 3.2
1
u/HazzyPls 0 0 Mar 14 '12
Been toying with Haskell lately. Not really sure how to best do this.
Using a built in function, it can be done in a single line.
, but that's no fun. So I made a more complicated version. It was a fun mental exercise since I'm so new to Haskell. Any little details I could improve upon? I'm sure there's a way to clean this up a bit.