r/dailyprogrammer • u/rya11111 3 1 • Feb 23 '12
[2/23/2012] Challenge #14 [easy]
Input: list of elements and a block size k or some other variable of your choice
Output: return the list of elements with every block of k elements reversed, starting from the beginning of the list.
For instance, given the list 12, 24, 32, 44, 55, 66 and the block size 2, the result is 24, 12, 44, 32, 66, 55.
12
Upvotes
5
u/funny_falcon Feb 24 '12
Ruby:
4
u/robin-gvx 0 2 Feb 23 '12
Déjà Vu: http://hastebin.com/raw/puloduvuga
Zero explicit variables (only a loop counter and function arguments)
for:is basically a compiler macro which means:It's a stack based version of
flatten, which I just discovered isn't yet in the stdlib.2
u/prophile Feb 23 '12
Python:
2
u/[deleted] Feb 23 '12
Javascript:
2
u/lukz 2 0 Feb 23 '12
Common Lisp
2
u/cooper6581 Feb 23 '12
C
2
u/hippibruder Feb 23 '12
Python:
2
u/this_space Feb 23 '12
Php:
2
u/mazzer Feb 23 '12
Java:
2
u/[deleted] Feb 24 '12
C++:
http://pastebin.com/RTrqyDSF
2
u/drb226 0 0 Feb 24 '12
Haskell:
2
u/namekuseijin Feb 24 '12
bare R5RS scheme:
2
u/Zamarok Feb 24 '12 edited Feb 24 '12
Haskell
And I thought of a non-recursive way too:
2
u/southof40 Feb 24 '12
Python:
Accomodates lists which are not an integer multiple of block size in length
2
u/javamonk Feb 24 '12
Ruby:
1
u/c64guy Feb 24 '12
Hardcoded the max number and asking for an argument for the block size.
Java: http://pastebin.com/hbCdSeYa
1
u/Gasten Feb 24 '12
C: http://pastebin.com/fkjfHFsD
1
u/Crystal_Cuckoo Feb 24 '12
Python:
I don't know how to convert the above into a list comprehension (instead of using a for loop). If anyone could enlighten me, I'd be very grateful.
1
u/[deleted] Feb 25 '12
1
u/kuzux 0 0 Feb 26 '12
clojure:
1
u/turboemu Feb 28 '12
Java: http://pastebin.com/WHzRRj8G
prints to console to show whats going on. was just for me but left it in anyway.
1
u/mhh91 Mar 01 '12
python: https://gist.github.com/1952724
Not really the most efficient, or the most elegant, but I'm open to suggestions.
1
u/Sarah132 Mar 02 '12 edited Mar 02 '12
C++
1
u/Yuushi Mar 19 '12
Haskell:
1
u/emcoffey3 0 0 May 18 '12
Solution in C#:
Note: If the length of the array is not evenly divisible by blocksize, trailing elements will be left unaltered.
1
u/ragtag_creature Dec 14 '22
R
0
u/[deleted] Feb 24 '12
[deleted] — view deleted comment