r/dailyprogrammer • u/Coder_d00d 1 3 • Jun 30 '14
[6/30/2014] Challenge #169 [Easy] 90 Degree 2D Array Rotate
Description:
Given a NxN size 2D array of numbers. Develop a way to rotate the data as if you rotated the data by 90 degrees clockwise.
Example:
N = 3
Data:
1 2 3
4 5 6
7 8 9
Rotate 90 degrees:
7 4 1
8 5 2
9 6 3
Rotate it again 90 degrees:
9 8 7
6 5 4
3 2 1
Challenge Input:
N = 10
1 2 3 4 5 6 7 8 9 0
0 9 8 7 6 5 4 3 2 1
1 3 5 7 9 2 4 6 8 0
0 8 6 4 2 9 7 5 3 1
0 1 2 3 4 5 4 3 2 1
9 8 7 6 5 6 7 8 9 0
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
9 8 7 6 7 8 9 8 7 6
0 0 0 0 0 0 0 0 0 0
Optional:
Show the 2D array at 90, 180, 270 degree clockwise from the original position.
59
Upvotes
55
u/xhable Jul 01 '14
Is it cheating to use matlab?
7
u/Frigguggi 0 1 Jul 01 '14
Java:
Challenge output:
7
u/Edward_H Jul 01 '14 edited Jul 01 '14
Here's an APL function (hope you have a Unicode font!):
Explanation:
As APL is evaluated right-to-left, this means the matrix is flipped then transposed, which is equivalent to a 90° rotation.
Example use:
Where
EDIT:
Here's a complete version with user input:
where ⎕ indicates evaluated input (i.e. a valid APL expression).
16
u/squ_are Jun 30 '14
Befunge-98
5
u/KillerCodeMonky Jun 30 '14
C#: https://dotnetfiddle.net/tZoyVp
Handles non-square matrices, as shown with the initialized matrix. Just comment and uncomment to run challenge matrix. Borrowed method implemented in Octave.
5
u/dunnowins Jul 01 '14
Ruby:
26
u/[deleted] Jul 01 '14
This one can only do counterclockwise, but here is the shortest possible program for data rotation. It's zero lines of code.
4
u/jetRink Jun 30 '14 edited Jul 01 '14
Clojure:
Edit: Added 180 and 270 rotations.
7
u/eruonna Jun 30 '14
Haskell
7
u/Godspiral 3 3 Jul 01 '14
in J
1
u/tehroflknife Jul 02 '14
Kind of late on this, but I like to make things unnecessarily harder for myself. Hope its not a problem if the 2D array is represented as a 1D array, allocated with size2 space.
Solution in go : http://play.golang.org/p/N49YwnmpLE
3
u/jeaton Jun 30 '14
JavaScript:
3
u/dohaqatar7 1 1 Jul 01 '14
Java
2
u/toodim Jun 30 '14
Octave
2
u/ENoether Jun 30 '14 edited Jul 01 '14
Scheme (edit: now works on nonsquare matrices):
Output:
2
u/kalleand Jul 01 '14
Common Lisp
2
u/marchelzo Jul 01 '14
Haskell without importing transpose from Data.List
2
u/[deleted] Jul 01 '14
Python 2.7. I am looking for any suggestions! I tried very hard to get the list comprehension working instead of using the for loop, so maybe someone can help point out what I could have done. I tried for over an hour to get the list comprehension right. This was my first time working with numpy, so I it took a little time to figure out what I was doing. This was a fun exercise!
Here is a sample output:
2
u/mortenaa Jul 01 '14
Solution in Dart. The difficult part was keeping track of all the indices when figuring out how to rotate the matrix in place.
Challenge output
2
u/dangerbird2 Jul 01 '14 edited Jul 01 '14
Latecomer, but here's my Vala solution. I was going to demonstrate the class implementation in C, but I ended up just doing it in Vala
2
u/53697246617073414C6F Jul 06 '14
Java:
Challenge Output :
1
u/Lurker378 Jun 30 '14 edited Jun 30 '14
Scala, with the optional challenge readable edition.
Scala with optional challenge golfed edition:
1
u/swingtheory Jun 30 '14
My python solution, tested with the challenge input:
1
u/[deleted] Jun 30 '14 edited Jan 02 '16
1
u/Reverse_Skydiver 1 0 Jun 30 '14
My solution in Java:
public class C0169_Easy {
}
Output:
1
u/killedbythegrue Jul 01 '14
A solution in C a change from Erlang. And with 360 degree rotation.
Output:
1
u/davidwhitney Jul 01 '14
C# https://dotnetfiddle.net/30zsU1
Couple of loops to shift elements in the array around.
Output:
1
u/jnazario 2 0 Jul 01 '14
F#
took me a bit but i think i got it. i found a transpose equation online that was far more FP-ish but honestly i don't quite think like that yet.
1
u/euid Jul 01 '14
Python 3:
1
u/dredclaw Jul 01 '14
First submission. Python 3.4:
1
u/dp_account Jul 01 '14
Python3
With output:
Sample Output:
1
u/[deleted] Jul 01 '14 edited Mar 08 '24
Reddit Wants to Get Paid for Helping to Teach Big A.I. Systems The internet site has long been a forum for discussion on a huge variety of topics, and companies like Google and OpenAI have been using it in their A.I. projects. "The Reddit corpus of data is really valuable," Steve Huffman, founder and chief executive of Reddit, said in an interview. "But we don’t need to give all of that value to some of the largest companies in the world for free."
1
u/tally_in_da_houise Jul 01 '14
Python 2.7+/3.3+
Challenge Output:
1
u/square_zero Jul 01 '14
That's awesome! I was literally just thinking about how to do this. It must be a sign. Will return with code when I get a chance (maybe tomorrow?)
1
u/TheRoganupgrade Jul 01 '14
Python 2.7
""" Reddit Challenge #169 Easy 90 Degree 2D Array """
1
u/viciu88 Jul 01 '14 edited Jul 01 '14
Java. Rotate function uses no additional memory for rotating square 2d arrays.
1
u/leonardo_m Jul 01 '14
Basic D solution:
1
u/towbes Jul 01 '14
My solution in Ruby with challenge input: a = [[1,2,3],[4,5,6],[7,8,9]]
Output :
1
u/this_shall_pass Jul 01 '14
C++:
Output:
1
u/[deleted] Jul 01 '14
A bit late but here's my C# version. This version extends the array so you can just call array.Rotate on any array. Also overloads the toString function for nice printing
Output:
1
u/waltharv Jul 01 '14
C# Is able to handle any type of array as well as non-square arrays.
1
u/VerifiedMyEmail Jul 01 '14 edited Jul 01 '14
python 3.3 rotates once 90 degrees
1
u/iamtehcrispy Jul 01 '14
Powershell
Script Output:
1
u/allcentury Jul 01 '14
Here's mine in ruby:
Here are my tests:
1
u/gfixler Jul 01 '14
Python 2.x one-liner:
etc...
1
u/TiZ_EX1 Jul 01 '14 edited Jul 01 '14
node.js with underscore, includes optional and happens to work on non-square grids:
Output:
1
u/joeyGibson Jul 01 '14 edited Jul 01 '14
Here's my Clojure solution. As usual, a pretty-printed version is at https://github.com/joeygibson/dailyprogrammer
Note: this assume you pass in a file from the command line that has the matrix values is in.
And here's the output
1
u/chunes 1 2 Jul 01 '14
Java:
1
u/hutsboR 3 0 Jul 02 '14 edited Jul 02 '14
A little late. I also didn't read the challenge thoroughly (or title for that matter), failed to acknowledged the "2D", so I used a single dimensional array. Pretty interesting solution nonetheless.
Java:
Output:
For rotations greater than 90* rotate can be called multiple times.
Since I failed to do the challenge properly I made this cool little gif that shows my solution transposing the matrix, top to bottom, right to left. All I did was record Eclipse's console and add some text. Click here!
1
u/mdlcm Jul 02 '14
Used R!
create data
create a function call "rotate"
rotate 90 degrees clockwise
1
u/poltergeistt Jul 02 '14
Solution in Haxe. It's hacked together, really, but it works. I also cobbled together a way to output what is being printed on screen to a text file. Once again, comments take up about as much space as the code does.
1
u/[deleted] Jul 02 '14
Solution in Python. Feedback is Appreciated!
1
u/fvandepitte 0 0 Jul 02 '14
In C#
Usage
Result Challenge
Code
1
u/ekmeh Jul 02 '14
Python 2.7 https://gist.github.com/anonymous/4322dd9e39127480fd5a
1
u/Timidger Jul 02 '14
My (late) python submission. Wanted to alter the array without copying it, but found it too annoying to keep track of switching the values:
1
u/[deleted] Jul 02 '14
Hope I'm not too late to the party!
Java, and I haven't coded in a while, so feedback is much appreciated:
Output:
1
u/RipIt_From_Space Jul 02 '14
One of my first submissions (In Java):
Github Gist
Output:
Normal:
1 2 3 4 5 6 7 8 9 0
0 9 8 7 6 5 4 3 2 1
1 3 5 7 9 2 4 6 8 0
0 8 6 4 2 9 7 5 3 1
0 1 2 3 4 5 4 3 2 1
9 8 7 6 5 6 7 8 9 0
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
9 8 7 6 7 8 9 8 7 6
0 0 0 0 0 0 0 0 0 0
90:
0 9 2 1 9 0 0 1 0 1
0 8 2 1 8 1 8 3 9 2
0 7 2 1 7 2 6 5 8 3
0 6 2 1 6 3 4 7 7 4
0 7 2 1 5 4 2 9 6 5
0 8 2 1 6 5 9 2 5 6
0 9 2 1 7 4 7 4 4 7
0 8 2 1 8 3 5 6 3 8
0 7 2 1 9 2 3 8 2 9
0 6 2 1 0 1 1 0 1 0
180:
0 0 0 0 0 0 0 0 0 0
6 7 8 9 8 7 6 7 8 9
2 2 2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 1 1
0 9 8 7 6 5 6 7 8 9
1 2 3 4 5 4 3 2 1 0
1 3 5 7 9 2 4 6 8 0
0 8 6 4 2 9 7 5 3 1
1 2 3 4 5 6 7 8 9 0
0 9 8 7 6 5 4 3 2 1
270:
0 1 0 1 1 0 1 2 6 0
9 2 8 3 2 9 1 2 7 0
8 3 6 5 3 8 1 2 8 0
7 4 4 7 4 7 1 2 9 0
6 5 2 9 5 6 1 2 8 0
5 6 9 2 4 5 1 2 7 0
4 7 7 4 3 6 1 2 6 0
3 8 5 6 2 7 1 2 7 0
2 9 3 8 1 8 1 2 8 0
1 0 1 0 0 9 1 2 9 0
1
u/mvolling Jul 02 '14
C++
I've only taken 1 semester of C++ so there are probably better ways of doing things. Please feel free to leave feedback.
example output:
1
u/[deleted] Jul 02 '14
Java solution. Handles printing numbers with more than one digit, and negatives smoothly. I added a couple zeroes to the challenge array to test with.
OUTPUT
1
u/RangeruDangeru Jul 02 '14
Python 3.4
1
u/BryghtShadow Jul 03 '14
SWI-Prolog v6.6.6
Output:
1
u/-AMD- Jul 03 '14
Python 2.7, with arbitrary number of rotations possible:
1
u/[deleted] Jul 03 '14
First post here! Boy is this exciting. And yes, I do know I'm a day late.
Solution is in Java. You can't input an array, regardless, it will rotate the autogenerated 2D array. It will also solve the challenge input as well.
1
u/OllieShadbolt 1 0 Jul 03 '14
Python 3.2.5 Function (Please give feedback where you can as I'm an early learner <3);
1
u/VolitiveGibbon Jul 05 '14
First submission to a DailyProgrammer question. Written in Python 3. Any critique on my writing style is much appreciated!
http://ideone.com/OWbBtD
1
u/jkudria Jul 06 '14
Python 2. First tim posting, although I've been solving these for some time. Could probably be reduced to a few lines but that would be just messy. The meat of it is for loop in the rotate_array function, so... Grabbing the array from a file but could just as well be stdin. I did one rotation but to do more wouldn't be hard, just rotate_array on the new array. Comments and feedback appreciated!
Output:
1
u/Schmidget23 Jul 08 '14
ctrl + alt + right/left/down/up?
1
u/[deleted] Jul 08 '14
I saw this week old easy challenge and was an exercise I hadn't done before so I tackled it anyway.
This is an overly verbose solution, but I like to think it's nice and readable for people who (like me) aren't very practiced at working with matrices. I had to do some research in order to come up with anything that wasn't a brute force solution. On the positive side this works for NxM matrices as well.
Python3
Challenge output:
1
u/Gr3y4r34 Jul 09 '14 edited Jul 09 '14
ANSI C
Solution conforms to ANSI standards (with exception to the // style comments). Overtired, but enjoyed the experience. Let me know if y'all see anything noteworthy.
Input:
Output:
1
u/Heretar Jul 11 '14
Critique if you'd like.
Result:
1
u/chunes 1 2 Jul 12 '14
Haskell:
1
u/roaming111 Jul 13 '14
Here is my try via github.
1
u/Coplate Jul 14 '14
COBOL gist located here
I didn't feel like reading in from the file or ACCEPT this time, so I just hardcoded the arrays.
INPUT:
OUTPUT:
1
u/[deleted] Jul 15 '14
C. Tried to use functions but couldn't for the life of me pass a pointer to a 2D matrix.
Output:
1
u/harrychin2 Jul 15 '14
My C++ solution
1
u/healthycola Jul 16 '14 edited Jul 16 '14
My attempt at this: https://gist.github.com/healthycola/306a003718cfc28a0f2a
Tried to minimize time and space used. Time came out to be roughly O(n) and space turned out to be O(1) - unless I'm gravely mistaken! :)
Feedback appreciated. C++.
Edit: Sample output:
1
u/YouAreNotASlave Jul 30 '14
In python 2.7
1
u/kurtlocker Sep 17 '14 edited Sep 17 '14
Functional JavaScript (No FOR loops!). Equates roughly to the Haskell definition described on this thread. Namely:
Here is the definition:
I/O:
1
u/qlf00n Oct 20 '14 edited Oct 20 '14
Python and NumPy, feedback is welcome.
EDIT: gave up formatting the output, HERE is the link to gist with code and the output in the first comment.
0
u/[deleted] Jul 01 '14
[deleted] — view deleted comment
0
u/[deleted] Jun 30 '14
[deleted] — view deleted comment