T
Tobiah
[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]>>> elegant_solution([1,2,3,4,5,6,7,8,9,10])
[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]>>> elegant_solution([1,2,3,4,5,6,7,8,9,10])
[(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)][[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]elegant_solution([1,2,3,4,5,6,7,8,9,10])
--
seq = range(1,11)
seq [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
zip( seq[0::2],seq[1::2] )
[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]map(list, zip( seq[0::2],seq[1::2] ))
[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]elegant_solution([1,2,3,4,5,6,7,8,9,10])
Matimus said:[(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)][[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]elegant_solution([1,2,3,4,5,6,7,8,9,10])
--
seq = range(1,11)
seq [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
zip( seq[0::2],seq[1::2] )
if you _really_ need lists then...[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]map(list, zip( seq[0::2],seq[1::2] ))
John said:[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]elegant_solution([1,2,3,4,5,6,7,8,9,10])
What is your definition of "elegant"? What about other dimensions of
code quality like "robust" and "fast"?
What have you tried?
Here's one possibility:
zip(source[::2], source[1::2])
[I'm presuming you won't be upset by getting tuples instead of lists]
[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]elegant_solution([1,2,3,4,5,6,7,8,9,10])
Traceback (most recent call last):elegant_solution([1,2,3,4,5,6,7,8,9,10])
[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]elegant_solution([1,2,3,4,5,6,7,8,9,10])
Wow! That's impressive. What version of Python are you using? When I try
it, I get this:
elegant_solution([1,2,3,4,5,6,7,8,9,10])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'elegant_solution' is not defined
Tobiah said:[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]elegant_solution([1,2,3,4,5,6,7,8,9,10])
Tobiah said:[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]elegant_solution([1,2,3,4,5,6,7,8,9,10])
That's not an array, it's a list. See the array module for arrays
(fixed-length, unlike variable-length lists).
import array
fixed = array.array('b')
fixed.append(42)
fixed.extend([0, 1, 127])
fixed array('b', [42, 0, 1, 127])
fixed.append(2)
fixed array('b', [42, 0, 1, 127, 2])
fixed[2:4] = array.array('b', [8])
fixed array('b', [42, 0, 8, 2])
Tobiah said:[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]elegant_solution([1,2,3,4,5,6,7,8,9,10])
The OP has already confessed. Don't rub it in.
John said:That's not an array, it's a list. See the array module for arraysTobiah said:elegant_solution([1,2,3,4,5,6,7,8,9,10])
[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
(fixed-length, unlike variable-length lists).
You must have your very own definitions of "fixed-length" and
"unlike".
John said:Wow! That's impressive. What version of Python are you using? When I tryelegant_solution([1,2,3,4,5,6,7,8,9,10])
[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
it, I get this:
Traceback (most recent call last):elegant_solution([1,2,3,4,5,6,7,8,9,10])
File "<stdin>", line 1, in <module>
NameError: name 'elegant_solution' is not defined
The OP has already confessed. Don't rub it in.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.